当我进行调试构建时,java资源被打包到jar而没有任何问题。但是,它不适用于发布版本。我注意到gradle任务processReleaseJavaRes仅针对调试版本执行。我搜索了android gradle代码并找到了以下lines of code:
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]
提前致谢, Romanski
答案 0 :(得分:1)
我遇到了同样的问题,filed an issue for the project
与此同时,我使用以下代码片段来解决此问题:
android.libraryVariants.all { variant ->
def proguardTask = variant.obfuscation
if (proguardTask != null) {
proguardTask.doFirst {
copy {
from variant.processJavaResources.destinationDir
into variant.javaCompile.destinationDir
}
}
}
}
它会将java res文件夹复制到classes
文件夹,它将包含在最终的jar中。
答案 1 :(得分:0)
我还没有找到解决方案,但我解决了这个问题:我只是手动运行任务,然后从java资源和模糊的jar文件中创建一个jar:
android.libraryVariants.all { variant ->
variant.outputFile = file("${project.buildDir}/outputs/aar/${project.archivesBaseName}-${variant.baseName}.aar");
if(variant.buildType.name.matches("release")) {
tasks.create(name: "enriched${variant.name.capitalize()}Jar", type:Jar, dependsOn:variant.processJavaResources) {
from zipTree("${project.buildDir}/intermediates/bundles/${variant.dirName}/classes.jar") // add original content
from fileTree("${project.buildDir}/intermediates/javaResources/${variant.dirName}/")
destinationDir(file("${project.buildDir}/intermediates/bundles/${variant.dirName}/"))
includeEmptyDirs false
archiveName 'classes_full.jar'
}
tasks.create(name: "fix${variant.name.capitalize()}Files", type:Delete, dependsOn:"enriched${variant.name.capitalize()}Jar") {
delete "${project.buildDir}/intermediates/bundles/${variant.dirName}/classes.jar"
doLast{
file("${project.buildDir}/intermediates/bundles/${variant.dirName}/classes_full.jar").renameTo(file("${project.buildDir}/intermediates/bundles/${variant.dirName}/classes.jar"))
}
}
variant.packageLibrary.dependsOn "fix${variant.name.capitalize()}Files"
tasks["fix${variant.name.capitalize()}Files"].mustRunAfter variant.obfuscation
}
}