使用Gradle构建Android库项目时,从生成的.jar中排除BuildConfig.class和R.class的正确方法是什么?
添加自定义任务:
task androidReleaseJar(type: Jar, dependsOn: assembleRelease) {
from "$buildDir/intermediates/classes/release/"
exclude '**/BuildConfig.class'
exclude '**/R.class'
exclude '**/R$*.class'
}
参考: 1.https://github.com/facebook/rebound/blob/master/build.gradle 2.https://github.com/keyboardsurfer/Crouton/blob/master/library/build.gradle 3.https://github.com/SnowdreamFramework/android-log/commit/4297a0244c972e3fcb9042b5e12181b21c33b524
你应该在CompileReleaseSources步骤之后得到它。
这对我有用:
task removeBuildConfig(dependsOn: "compileReleaseSources") {
doFirst {
file("$buildDir/intermediates/classes/release/pathToFile/BuildConfig.class").delete()
}
}
可能最优雅的解决方案是在库的build.gradle
末尾添加:
afterEvaluate {
generateReleaseBuildConfig.enabled = false
}
这适合我
afterEvaluate {
bundleDebug.dependsOn "removeBuildConfigDebug"
bundleRelease.dependsOn "removeBuildConfigRelease"
}
task removeBuildConfigDebug(dependsOn: "generateDebugBuildConfig") {
doFirst {
file("$buildDir/intermediates/classes/debug/com/amazon/geo/routing/BuildConfig.class").delete()
file("$buildDir/generated/source/buildConfig/debug/com/amazon/geo/routing/BuildConfig.java").delete()
}
}
task removeBuildConfigRelease(dependsOn: "generateReleaseBuildConfig") {
doFirst {
file("$buildDir/intermediates/classes/release/com/amazon/geo/routing/BuildConfig.class").delete()
file("$buildDir/generated/source/buildConfig/release/com/amazon/geo/routing/BuildConfig.java").delete()
}
}
这是我的代码,当添加bundle任务时,我们可以在这里挂钩。
tasks.whenTaskAdded {
if (it.name == 'bundleRelease' || it.name == 'bundleDebug') {
// bundle task is Zip
it.exclude '**/BuildConfig.class', '**/R.class', '**/R$*.class'
}
}
要放弃R和BuildConfig使用它
afterEvaluate {
generateReleaseBuildConfig.enabled = false
generateDebugBuildConfig.enabled = false
generateReleaseResValues.enabled = false
generateDebugResValues.enabled = false
}
在建造之前不要丢失清洁项目