Search in sources :

Example 1 with DirectoryInput

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

Example 2 with DirectoryInput

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;
}
Also used : StopExecutionException(org.gradle.api.tasks.StopExecutionException) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) JarInput(com.android.build.api.transform.JarInput) File(java.io.File) DirectoryInput(com.android.build.api.transform.DirectoryInput)

Aggregations

DirectoryInput (com.android.build.api.transform.DirectoryInput)2 JarInput (com.android.build.api.transform.JarInput)2 File (java.io.File)2 ClassPool (javassist.ClassPool)2 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 NotFoundException (javassist.NotFoundException)1 DocumentException (org.dom4j.DocumentException)1 StopExecutionException (org.gradle.api.tasks.StopExecutionException)1