当我进行调试构建时,java资源被打包到jar中没有任何问题。但是,它不适用于发布版本。我注意到 gradle 任务 processReleaseJavaRes 仅针对调试版本执行。我搜索了android gradle代码,发现了以下几行代码:
if (variantConfig.buildType.runProguard) {
// run proguard on output of compile task
createProguardTasks(variantData, null)
// hack since bundle can't depend on variantData.proguardTask
mergeProGuardFileTask.dependsOn variantData.proguardTask
bundle.dependsOn packageRes, packageAidl, packageRenderscript, mergeProGuardFileTask, lintCopy, variantData.ndkCompileTask
} else {
Sync packageLocalJar = project.tasks.create("package${variantData.variantConfiguration.fullName.capitalize()}LocalJar",Sync)
packageLocalJar.from(getLocalJarFileList(variantData.variantDependency))
packageLocalJar.into(project.file("$project.buildDir/$DIR_BUNDLES/${variantData.variantConfiguration.dirName}/$SdkConstants.LIBS_FOLDER"))
// jar the classes.
Jar jar = project.tasks.create("package${buildType.name.capitalize()}Jar", Jar);
jar.dependsOn variantData.javaCompileTask, variantData.processJavaResourcesTask
jar.from(variantData.javaCompileTask.outputs);
jar.from(variantData.processJavaResourcesTask.destinationDir)
...
bundle.dependsOn packageRes, jar, packageAidl, packageRenderscript, packageLocalJar, mergeProGuardFileTask, lintCopy, variantData.ndkCompileTask
...
}
然后我尝试将任务添加为 proguardRelease 的依赖项:
tasks.whenTaskAdded { task ->
if (task.name.matches("proguardRelease")) {
task.dependsOn('processReleaseJavaRes')
}
}
这很好用,并且 java 资源是在 build/intermediates/javaResources/ 中创建的——但随后出现了下一个问题。我如何告诉 proguard 它应该使用 build/intermediates/classes 中的类构建 jar,并且还包括 build/intermediates/javaResources 中的资源?我尝试使用 -injars 设置 javaResources 路径。现在它们被添加到 proguard(参见下面的日志输出),但没有添加到 build/intermediate/bundles/release/classes.jar 中的 classes.jar。
13:32:30.731 [INFO] [system.out] Reading program directory [/path/to/my/project/build/intermediates/classes/release] (filtered)
13:32:30.913 [INFO] [system.out] Reading program directory [/path/to/my/project/build/intermediates/javaResources/release]
提前致谢, 罗曼斯基