Search in sources :

Example 6 with BaseVariantOutput

use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.

the class MainDexLister method getMainDexList.

public List<String> getMainDexList(Collection<File> files, File mainDexListFile) {
    GradleVariantConfiguration config = appVariantContext.getVariantConfiguration();
    Set<String> mainDexList = new HashSet<String>();
    // Confusion of the map
    // Map<String, String> classMap = getClassObfMap(config);
    Collection<BaseVariantOutput> collection = appVariantContext.getVariantOutputData();
    File manifest = com.android.utils.FileUtils.join(collection.iterator().next().getProcessManifest().getManifestOutputDirectory(), new String[] { collection.iterator().next().getDirName(), "AndroidManifest.xml" });
    if (appVariantContext.getScope().getInstantRunBuildContext().isInInstantRunMode()) {
        manifest = com.android.utils.FileUtils.join(collection.iterator().next().getProcessManifest().getInstantRunManifestOutputDirectory(), new String[] { collection.iterator().next().getDirName(), "AndroidManifest.xml" });
    }
    String applicationName = ManifestFileUtils.getApplicationName(manifest);
    ClassPool classPool = new ClassPool();
    try {
        for (File file : files) {
            if (file.isFile()) {
                classPool.insertClassPath(file.getAbsolutePath());
            } else {
                classPool.appendClassPath(file.getAbsolutePath());
            }
        }
    } catch (NotFoundException e) {
        throw new GradleException(e.getMessage(), e);
    }
    TBuildConfig tBuildConfig = appVariantContext.getAtlasExtension().getTBuildConfig();
    HashSet handleList = new HashSet<String>();
    Set<String> headClasses = new LinkedHashSet<>();
    headClasses.add(applicationName);
    headClasses.add("android.taobao.atlas.bridge.BridgeApplicationDelegate");
    headClasses.addAll(multiDexConfig.getFirstDexClasses());
    List<String> maindexListClazz = new ArrayList<String>();
    String preLaunchStr = tBuildConfig.getPreLaunch();
    if (!org.apache.commons.lang3.StringUtils.isEmpty(preLaunchStr)) {
        String[] launchArray = preLaunchStr.split("\\|");
        if (launchArray.length > 0) {
            for (String launchItem : launchArray) {
                String[] launchInfo = launchItem.split(":");
                String clazzName = launchInfo[0];
                headClasses.add(clazzName);
            }
        }
    }
    for (String clazz : headClasses) {
        clazz = clazz.replaceAll("\\.", "/") + ".class";
        maindexListClazz.add(clazz);
    }
    for (String headClass : headClasses) {
        addRefClazz(classPool, headClass, mainDexList, handleList, "");
    }
    // get manifest
    for (String newLine : mainDexList) {
        newLine = newLine.replaceAll("\\.", "/") + ".class";
        maindexListClazz.add(newLine);
    }
    for (String className : headClasses) {
        className = className.replaceAll("\\.", "/") + ".class";
        maindexListClazz.add(className);
    }
    if (multiDexConfig.getMainDexListCount() != 0) {
        maindexListClazz = maindexListClazz.subList(0, multiDexConfig.getMainDexListCount());
    }
    try {
        FileUtils.writeLines(mainDexListFile, maindexListClazz);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return maindexListClazz;
}
Also used : ClassPool(javassist.ClassPool) GradleVariantConfiguration(com.android.build.gradle.internal.core.GradleVariantConfiguration) NotFoundException(javassist.NotFoundException) TBuildConfig(com.taobao.android.builder.extension.TBuildConfig) IOException(java.io.IOException) BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) GradleException(org.gradle.api.GradleException) File(java.io.File)

Example 7 with BaseVariantOutput

use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.

the class AtlasProguardHelper method applyBundleInOutConfigration.

public static File applyBundleInOutConfigration(final AppVariantContext appVariantContext, ProGuardTransform proGuardTransform) {
    VariantScope variantScope = appVariantContext.getScope();
    GlobalScope globalScope = variantScope.getGlobalScope();
    File proguardOut = new File(Joiner.on(File.separatorChar).join(String.valueOf(globalScope.getBuildDir()), FD_OUTPUTS, "mapping", variantScope.getVariantConfiguration().getDirName()));
    File awbInOutConfig = new File(proguardOut, "awb_inout_config.cfg");
    // Add awb configuration
    AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName());
    if (dependencyTree.getAwbBundles().size() > 0) {
        BaseVariantOutput vod = (BaseVariantOutput) appVariantContext.getVariantOutputData().iterator().next();
        AppVariantOutputContext appVariantOutputContext = appVariantContext.getAppVariantOutputContext(ApkDataUtils.get(vod));
        File awbObfuscatedDir = new File(globalScope.getIntermediatesDir(), "/classes-proguard/" + variantScope.getVariantConfiguration().getDirName());
        AwbProguardConfiguration awbProguardConfiguration = new AwbProguardConfiguration(appVariantOutputContext.getAwbTransformMap().values(), awbObfuscatedDir, appVariantOutputContext);
        try {
            configs = awbProguardConfiguration.printConfigFile(awbInOutConfig);
        } catch (IOException e) {
            throw new GradleException("", e);
        }
        proGuardTransform.setConfigurationFiles(appVariantContext.getScope().getGlobalScope().getProject().files(awbInOutConfig));
    }
    return awbInOutConfig;
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) VariantScope(com.android.build.gradle.internal.scope.VariantScope) GradleException(org.gradle.api.GradleException) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree)

Example 8 with BaseVariantOutput

use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.

the class AtlasTransformUtils method getTransformInputs.

public static Collection<File> getTransformInputs(AppVariantContext appVariantContext, TransformInvocation invocation) {
    ImmutableSet.Builder<File> builder = ImmutableSet.builder();
    Collection<File> transformInputs = TransformInputUtil.getAllFiles(invocation.getInputs());
    builder.addAll(transformInputs);
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(appVariantContext.getVariantConfiguration().getFullName());
    if (atlasDependencyTree.getAwbBundles().size() > 0) {
        BaseVariantOutput vod = (BaseVariantOutput) appVariantContext.getVariantOutputData().iterator().next();
        AppVariantOutputContext appVariantOutputContext = appVariantContext.getAppVariantOutputContext(ApkDataUtils.get(vod));
        Collection<AwbTransform> awbTransforms = appVariantOutputContext.getAwbTransformMap().values();
        awbTransforms.forEach(awbTransform -> {
            builder.addAll(awbTransform.getInputLibraries());
        });
    }
    return builder.build();
}
Also used : BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) ImmutableSet(com.google.common.collect.ImmutableSet) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) File(java.io.File) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree)

Example 9 with BaseVariantOutput

use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.

the class MtlTransformInjector method injectTasks.

public void injectTasks(List<MtlTransformContext> mtlTaskContexts) {
    for (final Object vod : appVariantContext.getVariantOutputData()) {
        for (MtlTransformContext mtlTransformContext : mtlTaskContexts) {
            TransformTask transformTask = findTransformTask(mtlTransformContext);
            TransformTask injectedTask = appVariantContext.getInjectTransformManager().addInjectTransformBeforeTransform(transformTask.getTransform().getClass(), TransformManager.createTransform(appVariantContext, mtlTransformContext.getTransformTask(), (BaseVariantOutput) vod), appVariantContext.getScope());
            injectedTask.dependsOn(appVariantContext.getVariantData().compileTask);
        }
    }
}
Also used : BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask)

Example 10 with BaseVariantOutput

use of com.android.build.gradle.api.BaseVariantOutput in project atlas by alibaba.

the class AtlasProguardHelper method doBundleProguard.

public static void doBundleProguard(final AppVariantContext appVariantContext, List<File> mainDexJars) throws Exception {
    Profiler.enter("preparebundleproguard");
    VariantScope variantScope = appVariantContext.getScope();
    AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName());
    if (dependencyTree.getAwbBundles().isEmpty()) {
        return;
    }
    List<File> libs = new ArrayList<>(appVariantContext.getScope().getGlobalScope().getAndroidBuilder().getBootClasspath(true));
    // All dependent classes
    Profiler.enter("getDefaultClasses");
    Set<String> defaultLibClasses = getClassList(libs);
    Profiler.release();
    libs.addAll(mainDexJars);
    // Get the basic proguard configuration
    List<File> defaultProguardFiles = new ArrayList<>(appVariantContext.getVariantConfiguration().getBuildType().getProguardFiles());
    Collections.sort(defaultProguardFiles);
    BaseVariantOutput vod = (BaseVariantOutput) appVariantContext.getVariantOutputData().iterator().next();
    AppVariantOutputContext appVariantOutputContext = appVariantContext.getAppVariantOutputContext(ApkDataUtils.get(vod));
    int parallelCount = appVariantContext.getAtlasExtension().getTBuildConfig().getProguardParallelCount();
    // //FIXME
    // parallelCount = 1;
    Map<BundleInfo, AwbTransform> bundleInfoAwbTransformMap = new HashMap<>();
    for (AwbTransform awbTransform : appVariantOutputContext.getAwbTransformMap().values()) {
        bundleInfoAwbTransformMap.put(awbTransform.getAwbBundle().bundleInfo, awbTransform);
    }
    Profiler.release();
    BundleGraphExecutor.execute(new ArrayList<>(bundleInfoAwbTransformMap.keySet()), parallelCount, new BundleItemRunner() {

        @Override
        public void execute(BundleItem bundleItem) {
            try {
                Input input = new Input();
                AwbTransform awbTransform = bundleInfoAwbTransformMap.get(bundleItem.bundleInfo);
                input.getAwbBundles().add(awbTransform);
                for (BundleInfo sub : bundleItem.circles) {
                    input.getAwbBundles().add(bundleInfoAwbTransformMap.get(sub));
                }
                input.getDefaultProguardFiles().addAll(defaultProguardFiles);
                input.getLibraries().addAll(libs);
                input.getDefaultLibraryClasses().addAll(defaultLibClasses);
                input.printConfiguration = new File(appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle()), "proguard.cfg");
                input.printUsage = new File(appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle()), "usage.cfg");
                addLibraryProguardFiles(appVariantContext, input);
                addChildDependency(bundleItem, input, bundleInfoAwbTransformMap);
                addParentKeeps(bundleItem, input, awbTransform, bundleInfoAwbTransformMap, appVariantContext);
                BundleProguarder.execute(appVariantContext, input);
            } catch (Exception e) {
                e.printStackTrace();
                throw new GradleException("proguard " + bundleItem.bundleInfo.getPkgName(), e);
            }
        }
    });
}
Also used : VariantScope(com.android.build.gradle.internal.scope.VariantScope) BundleItemRunner(com.taobao.android.builder.tools.bundleinfo.BundleItemRunner) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) Input(com.taobao.android.builder.tools.proguard.domain.Input) BundleInfo(com.taobao.android.builder.tools.bundleinfo.model.BundleInfo) BundleItem(com.taobao.android.builder.tools.bundleinfo.BundleItem) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) GradleException(org.gradle.api.GradleException) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)10 IOException (java.io.IOException)7 File (java.io.File)6 GradleException (org.gradle.api.GradleException)6 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)5 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)4 VariantScope (com.android.build.gradle.internal.scope.VariantScope)4 TransformTask (com.android.build.gradle.internal.pipeline.TransformTask)3 LibraryVariantOutput (com.android.build.gradle.api.LibraryVariantOutput)2 TaskContainerAdaptor (com.android.build.gradle.internal.TaskContainerAdaptor)2 AwbTransform (com.android.build.gradle.internal.api.AwbTransform)2 LibVariantContext (com.android.build.gradle.internal.api.LibVariantContext)2 GlobalScope (com.android.build.gradle.internal.scope.GlobalScope)2 TBuildType (com.taobao.android.builder.extension.TBuildType)2 AwbGenerator (com.taobao.android.builder.tasks.library.AwbGenerator)2 JarExtractTask (com.taobao.android.builder.tasks.library.JarExtractTask)2 AwoPropHandler (com.taobao.android.builder.tools.ideaplugin.AwoPropHandler)2 Task (org.gradle.api.Task)2 FileCollection (org.gradle.api.file.FileCollection)2 Zip (org.gradle.api.tasks.bundling.Zip)2