Search in sources :

Example 1 with TransformInput

use of com.android.build.api.transform.TransformInput 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);
        }
    }
}
Also used : TransformOutputProvider(com.android.build.api.transform.TransformOutputProvider) ClassPool(javassist.ClassPool) TransformException(com.android.build.api.transform.TransformException) TransformInput(com.android.build.api.transform.TransformInput) DocumentException(org.dom4j.DocumentException) JarInput(com.android.build.api.transform.JarInput) File(java.io.File) InjectParam(com.taobao.android.builder.tools.classinject.InjectParam) DirectoryInput(com.android.build.api.transform.DirectoryInput)

Aggregations

DirectoryInput (com.android.build.api.transform.DirectoryInput)1 JarInput (com.android.build.api.transform.JarInput)1 TransformException (com.android.build.api.transform.TransformException)1 TransformInput (com.android.build.api.transform.TransformInput)1 TransformOutputProvider (com.android.build.api.transform.TransformOutputProvider)1 InjectParam (com.taobao.android.builder.tools.classinject.InjectParam)1 File (java.io.File)1 ClassPool (javassist.ClassPool)1 DocumentException (org.dom4j.DocumentException)1