Search in sources :

Example 21 with AwbTransform

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

the class AwbDataBindingRenameTask method createAwbPackages.

/**
 * Directory of so
 */
@TaskAction
void createAwbPackages() throws ExecutionException, InterruptedException {
    WaitableExecutor workerExecutor = WaitableExecutor.useGlobalSharedThreadPool();
    AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    if (null == atlasDependencyTree) {
        return;
    }
    ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper(taskName, getLogger(), 0);
    for (final AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
        if (!appVariantContext.getAtlasExtension().getTBuildConfig().getDataBindingBundles().contains(awbBundle.getPackageName())) {
            continue;
        }
        if (!awbBundle.isDataBindEnabled() || awbBundle.isMBundle) {
            continue;
        }
        workerExecutor.execute(new Callable() {

            @Override
            public Object call() {
                try {
                    File dataBindingClazzFolder = appVariantOutputContext.getVariantContext().getJAwbavaOutputDir(awbBundle);
                    String packageName = awbBundle.getPackageName();
                    String appName = awbBundle.getPackageName() + "._bundleapp_";
                    // Remove classes that already exist
                    File dataMapperClazz = new File(dataBindingClazzFolder, "android/databinding/DataBinderMapper.class");
                    if (!dataMapperClazz.exists()) {
                        throw new GradleException("missing datamapper class");
                    }
                    File dataBindComponentClazz = new File(dataBindingClazzFolder, "android/databinding/DataBindingComponent.class");
                    if (!dataBindComponentClazz.exists()) {
                        throw new GradleException("missing dataBindComponent.class");
                    }
                    File dataBindDynamicUtilsClazz = new File(dataBindingClazzFolder, "android/databinding/DynamicUtil.class");
                    if (!dataBindDynamicUtilsClazz.exists()) {
                        throw new GradleException("missing dataBindDynamicUtils.class");
                    }
                    ClassNameRenamer.rewriteDataBinderMapper(dataBindingClazzFolder, "android/databinding/DataBinderMapper", packageName.replace(".", "/") + "/DataBinderMapper", dataMapperClazz);
                    ClassNameRenamer.rewriteDataBinderMapper(dataBindingClazzFolder, "android/databinding/DataBindingComponent", packageName.replace(".", "/") + "/DataBindingComponent", dataBindComponentClazz);
                    ClassNameRenamer.rewriteDataBinderMapper(dataBindingClazzFolder, "android/databinding/dataBindDynamicUtils", packageName.replace(".", "/") + "/dataBindDynamicUtils", dataBindDynamicUtilsClazz);
                    FileUtils.deleteDirectory(new File(dataBindingClazzFolder, "android/databinding"));
                    // FileUtils.deleteDirectory(new File(dataBindingClazzFolder, packageName.replace(".", "/") +
                    // "/_bundleapp_" ));
                    File appDir = new File(dataBindingClazzFolder, appName.replace(".", "/"));
                    if (appDir.exists()) {
                        File[] files = appDir.listFiles(new FileFilter() {

                            @Override
                            public boolean accept(File pathname) {
                                return pathname.isFile() && !pathname.isDirectory();
                            }
                        });
                        for (File tmp : files) {
                            FileUtils.forceDelete(tmp);
                        }
                    }
                    // rename DataBindUtils
                    AwbTransform awbTransform = appVariantOutputContext.getAwbTransformMap().get(awbBundle.getName());
                    List<File> files = awbTransform.getInputLibraries();
                    Map<String, String> replaceMap = new HashMap<>();
                    replaceMap.put("android/databinding/DataBindingUtil", "android/databinding/AtlasDataBindingUtil");
                    List<File> newLibrarys = new ArrayList<>();
                    for (File inputJar : files) {
                        File outputJar = new File(appVariantContext.getAwbLibraryDirForDataBinding(awbBundle), FileNameUtils.getUniqueJarName(inputJar) + ".jar");
                        outputJar.delete();
                        outputJar.getParentFile().mkdirs();
                        outputJar.createNewFile();
                        new ClazzReplacer(inputJar, outputJar, replaceMap).execute();
                        newLibrarys.add(outputJar);
                        awbTransform.getFileTransform().put(inputJar, outputJar);
                    }
                    awbTransform.setInputLibraries(newLibrarys);
                } catch (Throwable e) {
                    e.printStackTrace();
                    throw new GradleException("databinding awb failed", e);
                }
                return null;
            }
        });
    }
    workerExecutor.waitForTasksWithQuickFail(true);
}
Also used : ClazzReplacer(com.taobao.android.builder.tools.asm.ClazzReplacer) WaitableExecutor(com.android.ide.common.internal.WaitableExecutor) AtlasDependencyTree(com.taobao.android.builder.dependency.AtlasDependencyTree) Callable(java.util.concurrent.Callable) ExecutorServicesHelper(com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper) GradleException(org.gradle.api.GradleException) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) ArrayList(java.util.ArrayList) List(java.util.List) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) FileFilter(java.io.FileFilter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 22 with AwbTransform

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

the class TaobaoExtractJarsTransform method transform.

@Override
public void transform(TransformInvocation transformInvocation) throws IOException, TransformException, InterruptedException {
    TransformOutputProvider outputProvider = transformInvocation.getOutputProvider();
    boolean isIncremental = transformInvocation.isIncremental();
    checkNotNull(outputProvider, "Missing output object for transform " + getName());
    // as_input transform and no referenced scopes, all the inputs will in InputOutputStreams.
    final boolean extractCode = contentTypes.contains(QualifiedContent.DefaultContentType.CLASSES);
    if (!isIncremental) {
        outputProvider.deleteAll();
    }
    try {
        WaitableExecutor executor = WaitableExecutor.useGlobalSharedThreadPool();
        AtlasBuildContext.atlasMainDexHelperMap.get(variantContext.getVariantName()).getInputDirs().clear();
        for (File jarFile : AtlasBuildContext.atlasMainDexHelperMap.get(variantContext.getVariantName()).getAllMainDexJars()) {
            // final File jarFile = jarInput.getFile();
            JarInput jarInput = makeJarInput(jarFile);
            LOGGER.warn("input JarFile:" + jarFile.getAbsolutePath());
            // create an output folder for this jar, keeping its type and scopes.
            final File outJarFolder = outputProvider.getContentLocation(jarInput.getName(), jarInput.getContentTypes(), jarInput.getScopes(), Format.DIRECTORY);
            FileUtils.mkdirs(outJarFolder);
            LOGGER.warn("outJar Folder:" + outJarFolder.getAbsolutePath());
            AtlasBuildContext.atlasMainDexHelperMap.get(variantContext.getVariantName()).getInputDirs().add(outJarFolder);
            if (!isIncremental) {
                executor.execute(() -> {
                    extractJar(outJarFolder, jarFile, extractCode);
                    return null;
                });
            } else {
                switch(jarInput.getStatus()) {
                    case CHANGED:
                        executor.execute(() -> {
                            FileUtils.cleanOutputDir(outJarFolder);
                            extractJar(outJarFolder, jarFile, extractCode);
                            return null;
                        });
                        break;
                    case ADDED:
                        executor.execute(() -> {
                            extractJar(outJarFolder, jarFile, extractCode);
                            return null;
                        });
                        break;
                    case REMOVED:
                        executor.execute(() -> {
                            FileUtils.cleanOutputDir(outJarFolder);
                            return null;
                        });
                        break;
                    case NOTCHANGED:
                        break;
                }
            }
        }
        AtlasBuildContext.atlasMainDexHelperMap.get(variantContext.getVariantName()).addMainDexJars(Sets.newHashSet());
        for (AwbTransform awbTransform : variantOutputContext.getAwbTransformMap().values()) {
            File outJarFolder = variantOutputContext.getAwbExtractJarsFolder(awbTransform.getAwbBundle());
            awbTransform.getInputFiles().forEach(file -> executor.execute(() -> {
                LOGGER.warn("ExtractAwbJar[" + awbTransform.getAwbBundle().getPackageName() + "]---------------------" + file.getAbsolutePath());
                extractJar(outJarFolder, file, extractCode);
                return null;
            }));
            awbTransform.getInputLibraries().forEach(file -> executor.execute(() -> {
                LOGGER.warn("ExtractAwbJar[" + awbTransform.getAwbBundle().getPackageName() + "]---------------------" + file.getAbsolutePath());
                extractJar(outJarFolder, file, extractCode);
                return null;
            }));
            awbTransform.addDir(outJarFolder);
            awbTransform.getInputFiles().clear();
        }
        executor.waitForTasksWithQuickFail(true);
    } catch (InterruptedException e) {
        throw e;
    } catch (Exception e) {
        throw new TransformException(e);
    }
}
Also used : WaitableExecutor(com.android.ide.common.internal.WaitableExecutor) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) JarFile(java.util.jar.JarFile)

Example 23 with AwbTransform

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

the class AwbProguardConfiguration method printConfigFile.

/**
     * 打印proguard的config文件到指定文件
     * @param outConfigFile
     */
public void printConfigFile(File outConfigFile) throws IOException {
    List<String> configs = Lists.newArrayList();
    for (AwbTransform awbTransform : awbTransforms) {
        String name = awbTransform.getAwbBundle().getName();
        File obuscateFile = new File(awbObfuscatedDir, awbTransform.getAwbBundle().getName() + File.separator + OBUSCATED_JAR);
        obuscateFile.getParentFile().mkdirs();
        //            configs.add();
        if (null != awbTransform.getInputDir() && awbTransform.getInputDir().exists()) {
            configs.add(INJARS_OPTION + " " + awbTransform.getInputDir().getAbsolutePath());
        }
        for (File inputLibrary : awbTransform.getInputLibraries()) {
            configs.add(INJARS_OPTION + " " + inputLibrary.getAbsolutePath());
        }
        //            configs.add();
        configs.add(OUTJARS_OPTION + " " + obuscateFile.getAbsolutePath());
        List<File> inputLibraries = Lists.newArrayList();
        inputLibraries.add(obuscateFile);
        awbTransform.setInputFiles(inputLibraries);
        awbTransform.setInputDir(null);
        awbTransform.getInputLibraries().clear();
        appVariantOutputContext.getAwbTransformMap().put(name, awbTransform);
    }
    FileUtils.writeLines(outConfigFile, configs);
}
Also used : AwbTransform(com.android.build.gradle.internal.api.AwbTransform) File(java.io.File)

Aggregations

AwbTransform (com.android.build.gradle.internal.api.AwbTransform)23 File (java.io.File)16 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)12 JarFile (java.util.jar.JarFile)8 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)7 ArrayList (java.util.ArrayList)6 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)5 WaitableExecutor (com.android.ide.common.internal.WaitableExecutor)4 GradleException (org.gradle.api.GradleException)4 Nullable (com.android.annotations.Nullable)3 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)3 AppVariantContext (com.android.build.gradle.internal.api.AppVariantContext)3 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)3 IOException (java.io.IOException)3 TaskAction (org.gradle.api.tasks.TaskAction)3 NonNull (com.android.annotations.NonNull)2 AndroidLibrary (com.android.builder.model.AndroidLibrary)2 AtlasBuildContext (com.taobao.android.builder.AtlasBuildContext)2 BuildAtlasEnvTask (com.taobao.android.builder.tasks.app.BuildAtlasEnvTask)2 BundleItem (com.taobao.android.builder.tools.bundleinfo.BundleItem)2