use of com.android.build.api.transform.DirectoryInput in project atlas by alibaba.
the class ClassInjectTransform method transform.
@Override
public void transform(TransformInvocation transformInvocation) throws TransformException, IOException, InterruptedException {
TransformOutputProvider outputProvider = transformInvocation.getOutputProvider();
checkNotNull(outputProvider, "Missing output object for transform " + getName());
// Gather a full list of all inputs.
List<JarInput> jarInputs = Lists.newArrayList();
List<DirectoryInput> directoryInputs = Lists.newArrayList();
for (TransformInput input : transformInvocation.getInputs()) {
jarInputs.addAll(input.getJarInputs());
directoryInputs.addAll(input.getDirectoryInputs());
}
outputProvider.deleteAll();
ClassPool classPool = initClassPool(jarInputs, directoryInputs);
Set<String> outFileNames = Sets.newHashSet();
InjectParam injectParam = null;
try {
injectParam = AtlasBuildContext.sApkInjectInfoCreator.creteInjectParam(appVariantContext);
} catch (DocumentException e) {
throw new TransformException(e);
}
for (JarInput jarInput : jarInputs) {
if (null != logger) {
logger.debug("[ClassInject]" + jarInput.getFile().getAbsolutePath());
}
String jarFileName = jarInput.getFile().getName();
if (jarFileName.equalsIgnoreCase("classes.jar")) {
jarFileName = jarInput.getFile().getParentFile().getParentFile().getParentFile().getName() + "-" + jarInput.getFile().getParentFile().getParentFile().getName() + DOT_JAR;
}
String outFileName = jarFileName.substring(0, jarFileName.length() - DOT_JAR.length());
String fileName = outFileName;
int index = 1;
while (outFileNames.contains(fileName)) {
fileName = outFileName + "-" + index;
index++;
}
outFileNames.add(fileName);
File to = outputProvider.getContentLocation(fileName, jarInput.getContentTypes(), getScopes(), Format.JAR);
//只对 atlas 做代码注入, 没有做多jarmerge
if (injectParam.removePreverify && !jarFileName.contains("atlas") && jarInputs.size() > 1) {
FileUtils.copyFile(jarInput.getFile(), to);
} else {
CodeInjectByJavassist.inject(classPool, jarInput.getFile(), to, injectParam);
}
}
for (DirectoryInput directoryInput : directoryInputs) {
if (null != logger) {
logger.debug("[ClassInject]" + directoryInput.getFile().getAbsolutePath());
}
String folderName = directoryInput.getFile().getName();
File to = outputProvider.getContentLocation(folderName, getOutputTypes(), getScopes(), Format.DIRECTORY);
if (!injectParam.removePreverify) {
CodeInjectByJavassist.injectFolder(classPool, directoryInput.getFile(), to, injectParam);
} else {
FileUtils.copyDirectory(directoryInput.getFile(), to);
}
}
}
use of com.android.build.api.transform.DirectoryInput in project atlas by alibaba.
the class ClassInjectTransform method initClassPool.
private ClassPool initClassPool(List<JarInput> jarInputs, List<DirectoryInput> directoryInputs) {
if (((VariantScopeImpl) this.appVariantContext.getScope()).getVariantData().getName().toLowerCase().contains("debug")) {
try {
FieldUtils.writeStaticField(ClassPool.class, "defaultPool", null, true);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
logger.warn(">>> 请勿开启daemon <<<<");
}
final ClassPool pool = ClassPool.getDefault();
try {
// pool.insertClassPath(verifyFile.getAbsolutePath());
for (File file : appVariantContext.getScope().getJavaClasspath()) {
if (file.isFile()) {
pool.insertClassPath(file.getAbsolutePath());
} else {
pool.appendClassPath(file.getAbsolutePath());
}
}
String path = Joiner.on(File.pathSeparator).join(scope.getGlobalScope().getAndroidBuilder().getBootClasspathAsStrings(false));
pool.appendPathList(path);
for (JarInput jarInput : jarInputs) {
pool.insertClassPath(jarInput.getFile().getAbsolutePath());
}
for (DirectoryInput directoryInput : directoryInputs) {
pool.appendClassPath(directoryInput.getFile().getAbsolutePath());
}
} catch (NotFoundException e) {
throw new StopExecutionException(e.getMessage());
}
return pool;
}
Aggregations