Search in sources :

Example 1 with ClazzReplacer

use of com.taobao.android.builder.tools.asm.ClazzReplacer 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)

Aggregations

AwbTransform (com.android.build.gradle.internal.api.AwbTransform)1 WaitableExecutor (com.android.ide.common.internal.WaitableExecutor)1 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)1 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)1 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)1 ClazzReplacer (com.taobao.android.builder.tools.asm.ClazzReplacer)1 ExecutorServicesHelper (com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 GradleException (org.gradle.api.GradleException)1 TaskAction (org.gradle.api.tasks.TaskAction)1