build.gradle中的registerTransform之后缺少base.apk代码

时间:2019-01-12 08:24:59

标签: android gradle kotlin transform

github address

registerTransform(TestTransForm())

class TestTransForm : Transform() {
override fun getName(): String {
    return "test"
}

override fun getInputTypes(): MutableSet<QualifiedContent.ContentType> {
    return mutableSetOf(QualifiedContent.DefaultContentType.CLASSES)
}

override fun isIncremental(): Boolean {
    return false
}

override fun getScopes(): MutableSet<in QualifiedContent.Scope> {
    return TransformManager.SCOPE_FULL_PROJECT
}

override fun transform(transformInvocation: TransformInvocation?) {
}}

我只是尝试一个空的Transform,也不起作用。我检查输出中的.class文件,该.class文件实际上已按预期更改。但是在安装apk时出错,日志:

Failed to commit install session 1402092649 with command cmd package install-commit 1402092649. Error: INSTALL_FAILED_INVALID_APK: Package couldn't be installed in /data/app/com.haku.aaa-3aSlvL5yaZujUqYNmZlzgQ==: Package /data/app/com.haku.aaa-3aSlvL5yaZujUqYNmZlzgQ==/base.apk code is missing

然后我尝试了一些示例,例如Example,它很好用。我以为Gradle和Gradle工具的版本是原因,但更改工具版本和包装器版本后仍然报告错误。

Gradle工具版本:3.1.4 包装器版本:gradle-5.1

1 个答案:

答案 0 :(得分:1)

您必须负责使用方法transform将输入写到输出中,

@Override
void transform(
        @NonNull TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
    def inputs = transformInvocation.inputs
    def outputProvider = transformInvocation.outputProvider
    inputs.each { TransformInput input ->
        input.directoryInputs.each {DirectoryInput dirInput ->
            println("transform DirectoryInput:" + directoryInput.getFile().absolutePath);

            //do your work here,modify classes in the dirInput.

            //Make sure to copy every input to output,no matter modified or not.
            File dest = outputProvider.getContentLocation(directoryInput.getName(),
                    directoryInput.getContentTypes(), directoryInput.getScopes(), Format.DIRECTORY);
            println("transform DirectoryOutput:" + dest.getAbsolutePath());
            FileUtils.copyDirectory(directoryInput.getFile(), dest);
        }
        input.jarInputs.each{ JarInput jarInput ->
            println("transform jarInput:" + jarInput.getFile().absolutePath);
            String destName = jarInput.getName();

            //Do your work here,modify the jarInput.

            //Make sure to copy every input to output,no matter modified or not.
            File dest = outputProvider.getContentLocation(destName,
                    jarInput.getContentTypes(), jarInput.getScopes(), Format.JAR);
            println("transform jarOutput:" + dest.getAbsolutePath());
            FileUtils.copyFile(jarInput.getFile(), dest);
        }

    }
}

示例代码片段很时髦,可以轻松转换为kotlin。

阅读document了解详细说明。