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);
}
Aggregations