从 Gradle 中的 Android 库 jar 中排除 BuildConfig.class 和 R.class
Exclude BuildConfig.class and R.class from Android library jar in Gradle
使用 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()
}
}
这对我有用
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()
}
}
可能最优雅的解决方案是在库的末尾添加 build.gradle
:
afterEvaluate {
generateReleaseBuildConfig.enabled = false
}
这是我的代码,添加bundle task后,我们可以在这里hook它。
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
}
不要忘记在构建之前清理项目
使用 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()
}
}
这对我有用
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()
}
}
可能最优雅的解决方案是在库的末尾添加 build.gradle
:
afterEvaluate {
generateReleaseBuildConfig.enabled = false
}
这是我的代码,添加bundle task后,我们可以在这里hook它。
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
}
不要忘记在构建之前清理项目