1

当我进行调试构建时,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]

提前致谢, 罗曼斯基

4

2 回答 2

1

我遇到了同样的问题,并为该项目提交了一个问题

同时,我使用以下代码段来解决此问题:

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 中。

于 2015-10-07T11:42:06.567 回答
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
    }
}
于 2014-09-23T13:18:49.593 回答