Search in sources :

Example 1 with JavaLibrary

use of com.android.builder.model.JavaLibrary 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 JavaLibrary

use of com.android.builder.model.JavaLibrary in project atlas by alibaba.

the class DiffDependencyTask method doTask.

@TaskAction
public void doTask() throws IOException {
    apDependenciesFile = getApDependenciesFile();
    diffOutFile = getDiffOutFile();
    DependencyJson apDependencyJson = JSON.parseObject(FileUtils.readFileToString(apDependenciesFile), DependencyJson.class);
    Set<String> apMainDependencies = Sets.newHashSet();
    for (String mainDep : apDependencyJson.getMainDex()) {
        String name = mainDep.substring(0, mainDep.lastIndexOf(":"));
        apMainDependencies.add(name);
    }
    AwbBundle awbBundle = libVariantContext.getAwbBundle();
    //aars
    if (null != awbBundle.getLibraryDependencies()) {
        for (int index = 0; index < awbBundle.getLibraryDependencies().size(); index++) {
            AndroidLibrary libraryDependency = awbBundle.getLibraryDependencies().get(index);
            MavenCoordinates mavenCoordinates = libraryDependency.getResolvedCoordinates();
            String name = getMavenName(mavenCoordinates);
            if (apMainDependencies.contains(name)) {
                getLogger().info("[Remove]" + name);
                awbBundle.getLibraryDependencies().remove(index);
            } else {
                inAwbDependencies.add(name);
            }
        }
    }
    //solibs
    if (null != awbBundle.getSoLibraries()) {
        for (int index = 0; index < awbBundle.getSoLibraries().size(); index++) {
            SoLibrary soLibrary = awbBundle.getSoLibraries().get(index);
            MavenCoordinates mavenCoordinates = soLibrary.getResolvedCoordinates();
            String name = getMavenName(mavenCoordinates);
            if (apMainDependencies.contains(name)) {
                getLogger().info("[Remove]" + name);
                awbBundle.getSoLibraries().remove(index);
            } else {
                inAwbDependencies.add(name);
            }
        }
    }
    // jars
    if (null != awbBundle.getJavaDependencies()) {
        Iterator<? extends JavaLibrary> iterator = awbBundle.getJavaDependencies().iterator();
        while (iterator.hasNext()) {
            JavaLibrary jarInfo = iterator.next();
            MavenCoordinates mavenCoordinates = jarInfo.getResolvedCoordinates();
            String name = getMavenName(mavenCoordinates);
            if (apMainDependencies.contains(name)) {
                getLogger().info("[Remove]" + name);
                iterator.remove();
            } else {
                inAwbDependencies.add(name);
            }
        }
    }
    FileUtils.writeStringToFile(diffOutFile, StringUtils.join(inAwbDependencies, "\n"));
}
Also used : MavenCoordinates(com.android.builder.model.MavenCoordinates) JavaLibrary(com.android.builder.model.JavaLibrary) AndroidLibrary(com.android.builder.model.AndroidLibrary) DependencyJson(com.taobao.android.builder.dependency.output.DependencyJson) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) SoLibrary(com.taobao.android.builder.dependency.model.SoLibrary) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 3 with JavaLibrary

use of com.android.builder.model.JavaLibrary in project atlas by alibaba.

the class DependencyTree method toAndroidDependency.

/**
     * 转换为Android的依赖
     *
     * @return
     */
public AndroidDependencyTree toAndroidDependency() {
    AndroidDependencyTree androidDependencyTree = new AndroidDependencyTree(mResolvedDependencies);
    for (ResolvedDependencyInfo dependencyInfo : mResolvedDependencies) {
        switch(DependencyConvertUtils.Type.getType(dependencyInfo.getType())) {
            case AAR:
                AarBundle aarBundle = DependencyConvertUtils.toAarBundle(dependencyInfo);
                androidDependencyTree.getAarBundles().add(aarBundle);
                for (AndroidLibrary libraryDependency : aarBundle.getLibraryDependencies()) {
                    if (libraryDependency instanceof AarBundle) {
                        androidDependencyTree.getAarBundles().add((AarBundle) libraryDependency);
                    }
                }
                androidDependencyTree.getJars().addAll(aarBundle.getJavaDependencies());
                androidDependencyTree.getSoLibraries().addAll(aarBundle.getSoLibraries());
                break;
            case AWB:
                AwbBundle awbBundle = DependencyConvertUtils.toAwbBundle(dependencyInfo);
                androidDependencyTree.getAwbBundles().add(awbBundle);
                break;
            case JAR:
                JavaLibrary jarInfo = DependencyConvertUtils.toJarDependency(dependencyInfo);
                androidDependencyTree.getJars().add(jarInfo);
                androidDependencyTree.getJars().addAll(jarInfo.getDependencies());
                break;
            case SOLIB:
                SoLibrary soLibrary = DependencyConvertUtils.toSoLibrary(dependencyInfo);
                androidDependencyTree.getSoLibraries().add(soLibrary);
                break;
            case APK:
                ApkLibrary apkLibrary = DependencyConvertUtils.toApkLibrary(dependencyInfo);
                androidDependencyTree.getApkLibraries().add(apkLibrary);
                break;
            case AP:
                ApLibrary apLibrary = DependencyConvertUtils.toApLibrary(dependencyInfo);
                androidDependencyTree.getApLibraries().add(apLibrary);
                break;
            default:
                break;
        }
    }
    return androidDependencyTree;
}
Also used : JavaLibrary(com.android.builder.model.JavaLibrary) AndroidLibrary(com.android.builder.model.AndroidLibrary) AarBundle(com.taobao.android.builder.dependency.model.AarBundle) ApLibrary(com.taobao.android.builder.dependency.model.ApLibrary) ApkLibrary(com.taobao.android.builder.dependency.model.ApkLibrary) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) SoLibrary(com.taobao.android.builder.dependency.model.SoLibrary)

Example 4 with JavaLibrary

use of com.android.builder.model.JavaLibrary in project gradle by gradle.

the class Inspector method inspect.

private void inspect(JavaLibrary javaLibrary) {
    javaLibsByEquality.add(javaLibrary);
    if (!javaLibsByFile.containsKey(javaLibrary.getJarFile())) {
        javaLibsByFile.put(javaLibrary.getJarFile(), javaLibrary);
    }
    if (!javaLibsByIdentity.containsKey(javaLibrary)) {
        javaLibsByIdentity.put(javaLibrary, javaLibrary);
    }
    unpack(javaLibrary, javaLibsBackingByIdentity);
    for (JavaLibrary library : javaLibrary.getDependencies()) {
        inspect(library);
    }
}
Also used : JavaLibrary(com.android.builder.model.JavaLibrary)

Example 5 with JavaLibrary

use of com.android.builder.model.JavaLibrary in project meghanada-server by mopemope.

the class AndroidSupport method getAndroidDependencies.

private Set<ProjectDependency> getAndroidDependencies(AndroidProject androidProject) {
    Set<ProjectDependency> dependencies = new HashSet<>(16);
    Collection<String> bootClasspath = androidProject.getBootClasspath();
    for (String cp : bootClasspath) {
        File file = new File(cp);
        addDependencies(dependencies, file);
    }
    Collection<Variant> variants = androidProject.getVariants();
    for (Variant variant : variants) {
        String buildType = variant.getBuildType();
        boolean debugBuild = buildType.equals(DEBUG_BUILD);
        AndroidArtifact mainArtifact = variant.getMainArtifact();
        if (debugBuild) {
            Collection<File> generatedSourceFolders = mainArtifact.getGeneratedSourceFolders();
            for (File src : generatedSourceFolders) {
                this.project.getSources().add(src);
            }
            Collection<File> generatedResourceFolders = mainArtifact.getGeneratedResourceFolders();
            for (File src : generatedResourceFolders) {
                this.project.getResources().add(src);
            }
        }
        Dependencies compileDependencies = mainArtifact.getDependencies();
        Collection<AndroidLibrary> libraries = compileDependencies.getLibraries();
        for (AndroidLibrary androidLibrary : libraries) {
            String project = androidLibrary.getProject();
            if (nonNull(project) && !androidLibrary.isProvided()) {
                if (project.startsWith(":")) {
                    project = project.substring(1);
                }
                if (this.project.allModules.containsKey(project)) {
                    File root = this.project.allModules.get(project);
                    final ProjectDependency projectDependency = new ProjectDependency(project, "COMPILE", "1.0.0", root, ProjectDependency.Type.PROJECT);
                    dependencies.add(projectDependency);
                }
            }
            Collection<File> localJars = androidLibrary.getLocalJars();
            for (File jar : localJars) {
                addDependencies(dependencies, jar);
            }
        }
        Collection<JavaLibrary> javaLibraries = compileDependencies.getJavaLibraries();
        for (JavaLibrary javaLibrary : javaLibraries) {
            File file = javaLibrary.getJarFile();
            addDependencies(dependencies, file);
        }
        parseExtraAndroidArtifacts(dependencies, variant);
        parseExtraJavaArtifacts(dependencies, variant);
    }
    return dependencies;
}
Also used : ProjectDependency(meghanada.project.ProjectDependency) AndroidArtifact(com.android.builder.model.AndroidArtifact) Variant(com.android.builder.model.Variant) JavaLibrary(com.android.builder.model.JavaLibrary) AndroidLibrary(com.android.builder.model.AndroidLibrary) Dependencies(com.android.builder.model.Dependencies) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

JavaLibrary (com.android.builder.model.JavaLibrary)14 AndroidLibrary (com.android.builder.model.AndroidLibrary)11 SoLibrary (com.taobao.android.builder.dependency.model.SoLibrary)5 File (java.io.File)5 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)4 Dependencies (com.android.builder.model.Dependencies)3 ArrayList (java.util.ArrayList)3 AndroidArtifact (com.android.builder.model.AndroidArtifact)2 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)2 HashSet (java.util.HashSet)2 TaskAction (org.gradle.api.tasks.TaskAction)2 NonNull (com.android.annotations.NonNull)1 Nullable (com.android.annotations.Nullable)1 TransformException (com.android.build.api.transform.TransformException)1 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)1 TaskContainerAdaptor (com.android.build.gradle.internal.TaskContainerAdaptor)1 AppVariantContext (com.android.build.gradle.internal.api.AppVariantContext)1 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)1 AwbTransform (com.android.build.gradle.internal.api.AwbTransform)1 AtlasAndroidLibraryImpl (com.android.build.gradle.internal.ide.AtlasAndroidLibraryImpl)1