Search in sources :

Example 1 with GlobalScope

use of com.android.build.gradle.internal.scope.GlobalScope in project atlas by alibaba.

the class MergeAwbResourceConfigAction method execute.

@Override
public void execute(MergeResources mergeResourcesTask) {
    VariantScope scope = variantContext.getScope();
    final BaseVariantData<? extends BaseVariantOutputData> variantData = scope.getVariantData();
    final AndroidConfig extension = scope.getGlobalScope().getExtension();
    //final VariantConfiguration variantConfig = variantData.getVariantConfiguration();
    mergeResourcesTask.setMinSdk(variantData.getVariantConfiguration().getMinSdkVersion().getApiLevel());
    mergeResourcesTask.setAndroidBuilder(scope.getGlobalScope().getAndroidBuilder());
    mergeResourcesTask.setVariantName(scope.getVariantConfiguration().getFullName());
    GlobalScope globalScope = scope.getGlobalScope();
    mergeResourcesTask.setIncrementalFolder(scope.getIncrementalDir(getName()));
    try {
        FieldUtils.writeField(mergeResourcesTask, "variantScope", scope, true);
    } catch (IllegalAccessException e) {
        throw new GradleException("exception", e);
    }
    // where blame is useful, and once for packaging where it is not.
    if (includeDependencies) {
        mergeResourcesTask.setBlameLogFolder(scope.getResourceBlameLogDir());
    } else {
    //            if (variantContext instanceof AppVariantContext) {
    //                mergeResourcesTask.setBlameLogFolder(((AppVariantContext) variantContext).getAwbBlameLogFolder(awbBundle));
    //            }
    }
    mergeResourcesTask.setProcess9Patch(process9Patch);
    mergeResourcesTask.setCrunchPng(extension.getAaptOptions().getCruncherEnabled());
    VectorDrawablesOptions vectorDrawablesOptions = variantData.getVariantConfiguration().getMergedFlavor().getVectorDrawables();
    Set<String> generatedDensities = vectorDrawablesOptions.getGeneratedDensities();
    mergeResourcesTask.setGeneratedDensities(Objects.firstNonNull(generatedDensities, Collections.<String>emptySet()));
    mergeResourcesTask.setDisableVectorDrawables(vectorDrawablesOptions.getUseSupportLibrary() || mergeResourcesTask.getGeneratedDensities().isEmpty());
    //mergeResourcesTask.setUseNewCruncher(extension.getAaptOptions().getUseNewCruncher());
    final boolean validateEnabled = AndroidGradleOptions.isResourceValidationEnabled(scope.getGlobalScope().getProject());
    mergeResourcesTask.setValidateEnabled(validateEnabled);
    ConventionMappingHelper.map(mergeResourcesTask, "inputResourceSets", new Callable<List<ResourceSet>>() {

        @Override
        public List<ResourceSet> call() throws Exception {
            List<ResourceSet> resourceSets = Lists.newArrayList();
            List<? extends AndroidLibrary> bundleDeps = awbBundle.getLibraryDependencies();
            // the list of dependency must be reversed to use the right overlay order.
            for (int n = bundleDeps.size() - 1; n >= 0; n--) {
                AndroidLibrary dependency = bundleDeps.get(n);
                File resFolder = dependency.getResFolder();
                if (resFolder.isDirectory()) {
                    ResourceSet resourceSet = new ResourceSet(dependency.getFolder().getName(), true);
                    resourceSet.addSource(resFolder);
                    resourceSets.add(resourceSet);
                }
            }
            File awbResourceFolder = awbBundle.getResFolder();
            if (awbResourceFolder.isDirectory()) {
                ResourceSet resourceSet = new ResourceSet(awbBundle.getFolder().getName(), true);
                resourceSet.addSource(awbResourceFolder);
                resourceSets.add(resourceSet);
            }
            return resourceSets;
        }
    });
    mergeResourcesTask.setOutputDir(variantContext.getMergeResources(awbBundle));
    mergeResourcesTask.setGeneratedPngsOutputDir(variantContext.getPngsOutputDir(awbBundle));
//        if (variantContext instanceof AppVariantContext) {
//            AppVariantContext appVariantContext = (AppVariantContext) variantContext;
//            appVariantContext.getAwbMergeResourceTasks().put(awbBundle.getName(), mergeResourcesTask);
//        }
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) VariantScope(com.android.build.gradle.internal.scope.VariantScope) ResourceSet(com.android.ide.common.res2.ResourceSet) AndroidConfig(com.android.build.gradle.AndroidConfig) GradleException(org.gradle.api.GradleException) VectorDrawablesOptions(com.android.builder.model.VectorDrawablesOptions) AndroidLibrary(com.android.builder.model.AndroidLibrary) GradleException(org.gradle.api.GradleException) List(java.util.List) File(java.io.File)

Example 2 with GlobalScope

use of com.android.build.gradle.internal.scope.GlobalScope in project atlas by alibaba.

the class MergeAwbAssetConfigAction method execute.

@Override
public void execute(MergeSourceSetFolders task) {
    final VariantScope scope = variantContext.getScope();
    BaseVariantData<? extends BaseVariantOutputData> variantData = scope.getVariantData();
    final VariantConfiguration variantConfig = variantData.getVariantConfiguration();
    task.setAndroidBuilder(scope.getGlobalScope().getAndroidBuilder());
    task.setVariantName(variantConfig.getFullName());
    task.setIncrementalFolder(scope.getIncrementalDir(getName()));
    ConventionMappingHelper.map(task, "inputDirectorySets", new Callable<List<AssetSet>>() {

        @Override
        public List<AssetSet> call() throws Exception {
            List<AssetSet> assetSets = Lists.newArrayList();
            List<? extends AndroidLibrary> bundleDeps = awbBundle.getLibraryDependencies();
            // the list of dependency must be reversed to use the right overlay order.
            for (int n = bundleDeps.size() - 1; n >= 0; n--) {
                AndroidLibrary dependency = bundleDeps.get(n);
                File assetFolder = dependency.getAssetsFolder();
                if (assetFolder.isDirectory()) {
                    AssetSet assetSet = new AssetSet(dependency.getFolder().getName());
                    assetSet.addSource(assetFolder);
                    assetSets.add(assetSet);
                }
            }
            File awbAssetFolder = awbBundle.getAssetsFolder();
            if (awbAssetFolder.isDirectory()) {
                AssetSet awbAssetSet = new AssetSet(awbBundle.getFolder().getName());
                awbAssetSet.addSource(awbAssetFolder);
                assetSets.add(awbAssetSet);
            }
            return assetSets;
        }
    });
    ConventionMappingHelper.map(task, "outputDir", new Callable<File>() {

        @Override
        public File call() throws Exception {
            GlobalScope globalScope = scope.getGlobalScope();
            return variantContext.getMergeAssets(awbBundle);
        }
    });
//        if (variantContext instanceof AppVariantContext) {
//            AppVariantContext appVariantContext = (AppVariantContext) variantContext;
//            appVariantContext.getAwbMergeAssetTasks().put(awbBundle.getName(), task);
//        }
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) VariantScope(com.android.build.gradle.internal.scope.VariantScope) AndroidLibrary(com.android.builder.model.AndroidLibrary) AssetSet(com.android.ide.common.res2.AssetSet) List(java.util.List) File(java.io.File) VariantConfiguration(com.android.builder.core.VariantConfiguration)

Example 3 with GlobalScope

use of com.android.build.gradle.internal.scope.GlobalScope in project atlas by alibaba.

the class AwoJavaCompileConfigAction method execute.

/**
     * Awb的编译,暂不支持instantRun
     *
     * @param javacTask
     */
@Override
public void execute(JavaCompile javacTask) {
    //        libVariantContext.setJavacTask(javacTask);
    javacTask.setSource(libVariantContext.getSourceOutputDir());
    ConventionMappingHelper.map(javacTask, "classpath", new Callable<FileCollection>() {

        @Override
        public FileCollection call() {
            FileCollection classpath = scope.getJavaClasspath();
            return classpath;
        }
    });
    javacTask.setDestinationDir(libVariantContext.getJavaCDir());
    javacTask.setDependencyCacheDir(new File(scope.getGlobalScope().getIntermediatesDir(), "/awb-dependency-cache/" + scope.getVariantConfiguration().getFullName() + "/" + awbBundle.getName()));
    CompileOptions compileOptions = scope.getGlobalScope().getExtension().getCompileOptions();
    AbstractCompilesUtil.configureLanguageLevel(javacTask, compileOptions, scope.getGlobalScope().getExtension().getCompileSdkVersion(), false);
    javacTask.getOptions().setEncoding(compileOptions.getEncoding());
    javacTask.getOptions().setBootClasspath(Joiner.on(File.pathSeparator).join(scope.getGlobalScope().getAndroidBuilder().getBootClasspathAsStrings(false)));
    GlobalScope globalScope = scope.getGlobalScope();
    Project project = globalScope.getProject();
    boolean incremental;
    if (compileOptions.getIncremental() != null) {
        incremental = compileOptions.getIncremental();
    } else {
        // if (globalScope.getExtension().getDataBinding().isEnabled()
        // || project.getPlugins().hasPlugin("com.neenbedankt.android-apt")
        // || project.getPlugins().hasPlugin("me.tatarka.retrolambda")) {
        // incremental = false;
        // } else {
        // For now, default to false, irrespective of Instant Run.
        incremental = false;
    // }
    }
    if (AndroidGradleOptions.isJavaCompileIncrementalPropertySet(project)) {
        scope.getGlobalScope().getAndroidBuilder().getErrorReporter().handleSyncError(null, SyncIssue.TYPE_GENERIC, String.format("The %s property has been replaced by a DSL property. Please add the " + "following to your build.gradle instead:\n" + "android {\n" + "  compileOptions.incremental = false\n" + "}", AndroidGradleOptions.PROPERTY_INCREMENTAL_JAVA_COMPILE));
    }
    if (incremental) {
        LOG.info("Using incremental javac compilation.");
    } else {
        LOG.info("Not using incremental javac compilation.");
    }
    javacTask.getOptions().setIncremental(incremental);
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) Project(org.gradle.api.Project) CompileOptions(com.android.build.gradle.internal.CompileOptions) FileCollection(org.gradle.api.file.FileCollection) File(java.io.File)

Example 4 with GlobalScope

use of com.android.build.gradle.internal.scope.GlobalScope in project atlas by alibaba.

the class AwbJavaCompileConfigAction method execute.

/**
     * Awb的编译,暂不支持instantRun
     *
     * @param javacTask
     */
@Override
public void execute(JavaCompile javacTask) {
    appVariantOutputContext.getAwbJavacTasks().put(awbBundle.getName(), javacTask);
    ProcessAwbAndroidResources processAwbAndroidResources = appVariantOutputContext.getAwbAndroidResourcesMap().get(awbBundle.getName());
    assert null != processAwbAndroidResources;
    javacTask.source(processAwbAndroidResources.getSourceOutputDir());
    if (scope.getGlobalScope().getExtension().getDataBinding().isEnabled()) {
        javacTask.source(appVariantOutputContext.getVariantContext().getAwbClassOutputForDataBinding(awbBundle));
    }
    ConventionMappingHelper.map(javacTask, "classpath", new Callable<FileCollection>() {

        @Override
        public FileCollection call() {
            FileCollection classpath = scope.getVariantScope().getJavaClasspath();
            Set<File> dependencies = new HashSet<File>();
            dependencies.addAll(classpath.getFiles());
            //增加awb的依赖
            dependencies.addAll(awbBundle.getLibraryJars());
            FileCollection allClassPatch = appVariantOutputContext.getVariantContext().getProject().files(dependencies);
            return allClassPatch;
        }
    });
    javacTask.setDestinationDir(appVariantOutputContext.getJAwbavaOutputDir(awbBundle));
    javacTask.setDependencyCacheDir(appVariantOutputContext.getAwbJavaDependencyCache(awbBundle));
    CompileOptions compileOptions = scope.getGlobalScope().getExtension().getCompileOptions();
    AbstractCompilesUtil.configureLanguageLevel(javacTask, compileOptions, scope.getGlobalScope().getExtension().getCompileSdkVersion(), false);
    javacTask.getOptions().setEncoding(compileOptions.getEncoding());
    javacTask.getOptions().setBootClasspath(Joiner.on(File.pathSeparator).join(scope.getGlobalScope().getAndroidBuilder().getBootClasspathAsStrings(false)));
    GlobalScope globalScope = scope.getGlobalScope();
    Project project = globalScope.getProject();
    boolean incremental;
    if (compileOptions.getIncremental() != null) {
        incremental = compileOptions.getIncremental();
    } else {
        // if (globalScope.getExtension().getDataBinding().isEnabled()
        // || project.getPlugins().hasPlugin("com.neenbedankt.android-apt")
        // || project.getPlugins().hasPlugin("me.tatarka.retrolambda")) {
        // incremental = false;
        // } else {
        // For now, default to false, irrespective of Instant Run.
        incremental = false;
    // }
    }
    if (AndroidGradleOptions.isJavaCompileIncrementalPropertySet(project)) {
        scope.getGlobalScope().getAndroidBuilder().getErrorReporter().handleSyncError(null, SyncIssue.TYPE_GENERIC, String.format("The %s property has been replaced by a DSL property. Please add the " + "following to your build.gradle instead:\n" + "android {\n" + "  compileOptions.incremental = false\n" + "}", AndroidGradleOptions.PROPERTY_INCREMENTAL_JAVA_COMPILE));
    }
    if (incremental) {
        LOG.info("Using incremental javac compilation.");
    } else {
        LOG.info("Not using incremental javac compilation.");
    }
    javacTask.getOptions().setIncremental(incremental);
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) Project(org.gradle.api.Project) Set(java.util.Set) HashSet(java.util.HashSet) CompileOptions(com.android.build.gradle.internal.CompileOptions) FileCollection(org.gradle.api.file.FileCollection) File(java.io.File)

Example 5 with GlobalScope

use of com.android.build.gradle.internal.scope.GlobalScope in project atlas by alibaba.

the class AwbProguradHook method hookProguardTask.

/**
     * hook混淆的任务,加入awb的混淆配置
     *
     * @param appVariantContext
     */
public void hookProguardTask(final AppVariantContext appVariantContext) {
    final VariantScope variantScope = appVariantContext.getScope();
    List<TransformTask> proguaradTransformTasks = getTransformTaskByTransformType(appVariantContext, ProGuardTransform.class);
    for (TransformTask proguaradTransformTask : proguaradTransformTasks) {
        final ProGuardTransform proGuardTransform = (ProGuardTransform) proguaradTransformTask.getTransform();
        if (null != proGuardTransform) {
            proguaradTransformTask.doFirst(new Action<Task>() {

                @Override
                public void execute(Task task) {
                    GlobalScope globalScope = variantScope.getGlobalScope();
                    File proguardOut = new File(Joiner.on(File.separatorChar).join(String.valueOf(globalScope.getBuildDir()), FD_OUTPUTS, "mapping", variantScope.getVariantConfiguration().getDirName()));
                    //为了方便排查,先把configuration打印到目录
                    proGuardTransform.printconfiguration(new File(proguardOut, "tmp_config.cfg"));
                    final File outConfigFile = new File(proguardOut, "awb_config.cfg");
                    //增加awb的配置
                    AndroidDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName());
                    if (null == dependencyTree) {
                        throw new StopExecutionException("DependencyTree cannot be null!");
                    }
                    if (dependencyTree.getAwbBundles().size() > 0) {
                        BaseVariantOutputData vod = appVariantContext.getVariantData().getOutputs().get(0);
                        AppVariantOutputContext appVariantOutputContext = getAppVariantOutputContext(appVariantContext, vod);
                        File awbObfuscatedDir = new File(globalScope.getIntermediatesDir(), "/classes-proguard/" + variantScope.getVariantConfiguration().getDirName());
                        AwbProguardConfiguration awbProguardConfiguration = new AwbProguardConfiguration(appVariantOutputContext.getAwbTransformMap().values(), awbObfuscatedDir, appVariantOutputContext);
                        try {
                            awbProguardConfiguration.printConfigFile(outConfigFile);
                        } catch (IOException e) {
                            throw new GradleException("", e);
                        }
                        proGuardTransform.setConfigurationFiles(new Supplier<Collection<File>>() {

                            @Override
                            public Collection<File> get() {
                                Set<File> proguardFiles = new HashSet<File>();
                                ((HashSet<File>) proguardFiles).add(outConfigFile);
                                return proguardFiles;
                            }
                        });
                    }
                    File mappingFile = null;
                    if (null != appVariantContext.apContext.getApExploredFolder() && appVariantContext.apContext.getApExploredFolder().exists()) {
                        mappingFile = new File(appVariantContext.apContext.getApExploredFolder(), "mapping.txt");
                    } else {
                        mappingFile = new File(appVariantContext.getScope().getGlobalScope().getProject().getProjectDir(), "mapping.txt");
                    }
                    if (null != mappingFile && mappingFile.exists()) {
                        proGuardTransform.applyTestedMapping(mappingFile);
                    }
                }
            });
        }
    }
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) VariantScope(com.android.build.gradle.internal.scope.VariantScope) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) Task(org.gradle.api.Task) AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) BaseVariantOutputData(com.android.build.gradle.internal.variant.BaseVariantOutputData) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) IOException(java.io.IOException) ProGuardTransform(com.android.build.gradle.internal.transforms.ProGuardTransform) StopExecutionException(org.gradle.api.tasks.StopExecutionException) AwbProguardConfiguration(com.taobao.android.builder.tasks.app.bundle.AwbProguardConfiguration) GradleException(org.gradle.api.GradleException) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) Supplier(java.util.function.Supplier) File(java.io.File)

Aggregations

GlobalScope (com.android.build.gradle.internal.scope.GlobalScope)5 File (java.io.File)5 VariantScope (com.android.build.gradle.internal.scope.VariantScope)3 CompileOptions (com.android.build.gradle.internal.CompileOptions)2 AndroidLibrary (com.android.builder.model.AndroidLibrary)2 List (java.util.List)2 GradleException (org.gradle.api.GradleException)2 Project (org.gradle.api.Project)2 FileCollection (org.gradle.api.file.FileCollection)2 AndroidConfig (com.android.build.gradle.AndroidConfig)1 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)1 TransformTask (com.android.build.gradle.internal.pipeline.TransformTask)1 ProGuardTransform (com.android.build.gradle.internal.transforms.ProGuardTransform)1 BaseVariantOutputData (com.android.build.gradle.internal.variant.BaseVariantOutputData)1 VariantConfiguration (com.android.builder.core.VariantConfiguration)1 VectorDrawablesOptions (com.android.builder.model.VectorDrawablesOptions)1 AssetSet (com.android.ide.common.res2.AssetSet)1 ResourceSet (com.android.ide.common.res2.ResourceSet)1 AndroidDependencyTree (com.taobao.android.builder.dependency.AndroidDependencyTree)1 AwbProguardConfiguration (com.taobao.android.builder.tasks.app.bundle.AwbProguardConfiguration)1