Search in sources :

Example 1 with CircleDependencyCheck

use of com.taobao.android.builder.dependency.CircleDependencyCheck in project atlas by alibaba.

the class TDependencyManager method resolveDependencyForApplicationConfig.

private void resolveDependencyForApplicationConfig(@NonNull final VariantDependencies variantDeps, @Nullable VariantDependencies testedVariantDeps, @Nullable String testedProjectPath, @NonNull Multimap<AndroidLibrary, Configuration> reverseLibMap) {
    boolean needPackageScope = true;
    if (AndroidGradleOptions.buildModelOnly(project)) {
        // if we're only syncing (building the model), then we only need the package
        // scope if we will actually pass it to the IDE.
        Integer modelLevelInt = AndroidGradleOptions.buildModelOnlyVersion(project);
        int modelLevel = AndroidProject.MODEL_LEVEL_0_ORIGNAL;
        if (modelLevelInt != null) {
            modelLevel = modelLevelInt;
        }
        needPackageScope = modelLevel >= AndroidProject.MODEL_LEVEL_2_DEP_GRAPH;
    }
    Configuration compileClasspath = variantDeps.getCompileConfiguration();
    Configuration packageClasspath = variantDeps.getPackageConfiguration();
    if (DEBUG_DEPENDENCY) {
        System.out.println(">>>>>>>>>>");
        System.out.println(project.getName() + ":" + compileClasspath.getName() + "/" + packageClasspath.getName());
    }
    Set<String> resolvedModules = Sets.newHashSet();
    Map<ModuleVersionIdentifier, List<ResolvedArtifact>> artifacts = Maps.newHashMap();
    collectArtifacts(compileClasspath, artifacts);
    collectArtifacts(packageClasspath, artifacts);
    // 不使用官方的扁平化的依赖处理,改用自己处理树状的依赖关系;对于application的依赖,我们只取compile的依赖
    ResolvedDependencyContainer compileResolvedDependencyContainer = new ResolvedDependencyContainer(project);
    Set<ModuleVersionIdentifier> directDependencies = new HashSet<ModuleVersionIdentifier>();
    Set<? extends DependencyResult> projectDependencies = compileClasspath.getIncoming().getResolutionResult().getRoot().getDependencies();
    for (DependencyResult dependencyResult : projectDependencies) {
        if (dependencyResult instanceof ResolvedDependencyResult) {
            ModuleVersionIdentifier moduleVersion = ((ResolvedDependencyResult) dependencyResult).getSelected().getModuleVersion();
            CircleDependencyCheck circleDependencyCheck = new CircleDependencyCheck(moduleVersion);
            if (!directDependencies.contains(moduleVersion)) {
                directDependencies.add(moduleVersion);
                resolveDependency(compileResolvedDependencyContainer, null, ((ResolvedDependencyResult) dependencyResult).getSelected(), artifacts, variantDeps, 0, circleDependencyCheck, circleDependencyCheck.getRootDependencyNode(), resolvedModules);
            }
        }
    }
    AndroidDependencyTree androidDependencyTree = compileResolvedDependencyContainer.reslovedDependencies().toAndroidDependency();
    AtlasBuildContext.androidDependencyTrees.put(variantDeps.getName(), androidDependencyTree);
    //output tree file only once
    if (project.getLogger().isInfoEnabled()) {
        project.getLogger().info("[dependencyTree" + variantDeps.getName() + "]" + JSON.toJSONString(androidDependencyTree.getDependencyJson(), true));
    }
    // 设置reverseMap
    for (AndroidLibrary libInfo : androidDependencyTree.getAarBundles()) {
        reverseLibMap.put(libInfo, variantDeps.getCompileConfiguration());
    }
    Set<String> currentUnresolvedDependencies = Sets.newHashSet();
    // records the artifact we find during package, to detect provided only dependencies.
    Set<String> artifactSet = Sets.newHashSet();
    // start with package dependencies, record the artifacts
    DependencyContainer packagedDependencies;
    if (needPackageScope) {
        packagedDependencies = gatherDependencies(packageClasspath, variantDeps, reverseLibMap, currentUnresolvedDependencies, testedProjectPath, artifactSet, ScopeType.PACKAGE);
    } else {
        packagedDependencies = DependencyContainerImpl.getEmpty();
    }
    // then the compile dependencies, comparing against the record package dependencies
    // to set the provided flag.
    // if we have not compute the package scope, we disable the computation of
    // provided bits. This disables the checks on impossible provided libs (provided aar in
    // apk project).
    ScopeType scopeType = needPackageScope ? ScopeType.COMPILE : ScopeType.COMPILE_ONLY;
    DependencyContainer compileDependencies = gatherDependencies(compileClasspath, variantDeps, reverseLibMap, currentUnresolvedDependencies, testedProjectPath, artifactSet, scopeType);
    if (extraModelInfo.getMode() != STANDARD && compileClasspath.getResolvedConfiguration().hasError()) {
        for (String dependency : currentUnresolvedDependencies) {
            extraModelInfo.handleSyncError(dependency, SyncIssue.TYPE_UNRESOLVED_DEPENDENCY, String.format("Unable to resolve dependency '%s'", dependency));
        }
    }
    // validate the dependencies.
    if (needPackageScope) {
        variantDeps.getChecker().validate(compileDependencies, packagedDependencies, testedVariantDeps);
    }
    if (DEBUG_DEPENDENCY) {
        System.out.println("*** COMPILE DEPS ***");
        for (AndroidLibrary lib : compileDependencies.getAndroidDependencies()) {
            System.out.println("LIB: " + lib);
        }
        for (JavaLibrary jar : compileDependencies.getJarDependencies()) {
            System.out.println("JAR: " + jar);
        }
        for (JavaLibrary jar : compileDependencies.getLocalDependencies()) {
            System.out.println("LOCAL-JAR: " + jar);
        }
        System.out.println("*** PACKAGE DEPS ***");
        for (AndroidLibrary lib : packagedDependencies.getAndroidDependencies()) {
            System.out.println("LIB: " + lib);
        }
        for (JavaLibrary jar : packagedDependencies.getJarDependencies()) {
            System.out.println("JAR: " + jar);
        }
        for (JavaLibrary jar : packagedDependencies.getLocalDependencies()) {
            System.out.println("LOCAL-JAR: " + jar);
        }
        System.out.println("***");
    }
    variantDeps.setDependencies(compileDependencies, packagedDependencies);
    configureBuild(variantDeps);
    if (DEBUG_DEPENDENCY) {
        System.out.println(project.getName() + ":" + compileClasspath.getName() + "/" + packageClasspath.getName());
        System.out.println("<<<<<<<<<<");
    }
}
Also used : Configuration(org.gradle.api.artifacts.Configuration) DependencyResult(org.gradle.api.artifacts.result.DependencyResult) UnresolvedDependencyResult(org.gradle.api.artifacts.result.UnresolvedDependencyResult) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) ResolvedDependencyContainer(com.taobao.android.builder.dependency.ResolvedDependencyContainer) DependencyContainer(com.android.builder.dependency.DependencyContainer) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) ResolvedDependencyContainer(com.taobao.android.builder.dependency.ResolvedDependencyContainer) JavaLibrary(com.android.builder.model.JavaLibrary) AndroidLibrary(com.android.builder.model.AndroidLibrary) CircleDependencyCheck(com.taobao.android.builder.dependency.CircleDependencyCheck) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) HashSet(java.util.HashSet)

Example 2 with CircleDependencyCheck

use of com.taobao.android.builder.dependency.CircleDependencyCheck in project atlas by alibaba.

the class TDependencyManager method resolveDependency.

/**
     * 解析依赖
     *
     * @param resolvedDependencyContainer
     * @param parent
     * @param resolvedComponentResult
     * @param artifacts
     * @param configDependencies
     * @param indent
     */
private void resolveDependency(ResolvedDependencyContainer resolvedDependencyContainer, ResolvedDependencyInfo parent, ResolvedComponentResult resolvedComponentResult, Map<ModuleVersionIdentifier, List<ResolvedArtifact>> artifacts, VariantDependencies configDependencies, int indent, CircleDependencyCheck circleDependencyCheck, CircleDependencyCheck.DependencyNode node, Set<String> resolvedModules) {
    ModuleVersionIdentifier moduleVersion = resolvedComponentResult.getModuleVersion();
    if (configDependencies.getChecker().checkForExclusion(moduleVersion)) {
        return;
    }
    if (moduleVersion.getName().equals("support-annotations") && moduleVersion.getGroup().equals("com.android.support")) {
        configDependencies.setAnnotationsPresent(true);
    }
    // now loop on all the artifact for this modules.
    List<ResolvedArtifact> moduleArtifacts = artifacts.get(moduleVersion);
    if (null == moduleArtifacts) {
        return;
    }
    ComponentIdentifier id = resolvedComponentResult.getId();
    String gradlePath = (id instanceof ProjectComponentIdentifier) ? ((ProjectComponentIdentifier) id).getProjectPath() : null;
    // 如果同时找到多个依赖,暂时没法判断是那个真正有用
    for (ResolvedArtifact resolvedArtifact : moduleArtifacts) {
        ResolvedDependencyInfo resolvedDependencyInfo = new ResolvedDependencyInfo(moduleVersion.getVersion(), moduleVersion.getGroup(), moduleVersion.getName(), resolvedArtifact.getType(), resolvedArtifact.getClassifier());
        resolvedDependencyInfo.setIndent(indent);
        resolvedDependencyInfo.setGradlePath(gradlePath);
        resolvedDependencyInfo.setResolvedArtifact(resolvedArtifact);
        //String parentVersionString = parent.getType();
        String moduleVersonString = moduleVersion.toString() + "." + resolvedArtifact.getType() + "." + resolvedArtifact.getClassifier() + "." + indent;
        if (null != parent) {
            if ("awb".equals(parent.getType())) {
                moduleVersonString = parent.toString() + "->" + moduleVersonString;
            }
        }
        if (resolvedModules.contains(moduleVersonString)) {
            logger.info(moduleVersonString);
            continue;
        } else {
            resolvedModules.add(moduleVersonString);
        }
        String path = computeArtifactPath(moduleVersion, resolvedArtifact);
        String name = computeArtifactName(moduleVersion, resolvedArtifact);
        File explodedDir = project.file(project.getBuildDir() + "/" + FD_INTERMEDIATES + "/exploded-" + resolvedArtifact.getType().toLowerCase() + "/" + path);
        resolvedDependencyInfo.setExplodedDir(explodedDir);
        resolvedDependencyInfo.setDependencyName(name);
        if (null == parent) {
            parent = resolvedDependencyInfo;
        } else {
            if (null == resolvedDependencyInfo.getParent()) {
                resolvedDependencyInfo.setParent(parent);
            }
            parent.getChildren().add(resolvedDependencyInfo);
        }
        Set<? extends DependencyResult> dependencies = resolvedComponentResult.getDependencies();
        for (DependencyResult dep : dependencies) {
            if (dep instanceof ResolvedDependencyResult) {
                ResolvedComponentResult childResolvedComponentResult = ((ResolvedDependencyResult) dep).getSelected();
                CircleDependencyCheck.DependencyNode childNode = circleDependencyCheck.addDependency(childResolvedComponentResult.getModuleVersion(), node, indent + 1);
                CircleDependencyCheck.CircleResult circleResult = circleDependencyCheck.checkCircle(logger);
                if (circleResult.hasCircle) {
                    logger.warning("[CircleDependency]" + StringUtils.join(circleResult.detail, ";"));
                } else {
                    resolveDependency(resolvedDependencyContainer, parent, ((ResolvedDependencyResult) dep).getSelected(), artifacts, configDependencies, indent + 1, circleDependencyCheck, childNode, resolvedModules);
                }
            }
        }
        resolvedDependencyContainer.addDependency(resolvedDependencyInfo);
    }
}
Also used : ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) ResolvedDependencyInfo(com.taobao.android.builder.dependency.ResolvedDependencyInfo) DependencyResult(org.gradle.api.artifacts.result.DependencyResult) UnresolvedDependencyResult(org.gradle.api.artifacts.result.UnresolvedDependencyResult) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) CircleDependencyCheck(com.taobao.android.builder.dependency.CircleDependencyCheck) ResolvedComponentResult(org.gradle.api.artifacts.result.ResolvedComponentResult) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) File(java.io.File) ResolvedDependencyResult(org.gradle.api.artifacts.result.ResolvedDependencyResult)

Aggregations

CircleDependencyCheck (com.taobao.android.builder.dependency.CircleDependencyCheck)2 ModuleVersionIdentifier (org.gradle.api.artifacts.ModuleVersionIdentifier)2 DependencyResult (org.gradle.api.artifacts.result.DependencyResult)2 ResolvedDependencyResult (org.gradle.api.artifacts.result.ResolvedDependencyResult)2 UnresolvedDependencyResult (org.gradle.api.artifacts.result.UnresolvedDependencyResult)2 DependencyContainer (com.android.builder.dependency.DependencyContainer)1 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 JavaLibrary (com.android.builder.model.JavaLibrary)1 ImmutableList (com.google.common.collect.ImmutableList)1 AndroidDependencyTree (com.taobao.android.builder.dependency.AndroidDependencyTree)1 ResolvedDependencyContainer (com.taobao.android.builder.dependency.ResolvedDependencyContainer)1 ResolvedDependencyInfo (com.taobao.android.builder.dependency.ResolvedDependencyInfo)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Configuration (org.gradle.api.artifacts.Configuration)1 ResolvedArtifact (org.gradle.api.artifacts.ResolvedArtifact)1 ComponentIdentifier (org.gradle.api.artifacts.component.ComponentIdentifier)1 ProjectComponentIdentifier (org.gradle.api.artifacts.component.ProjectComponentIdentifier)1