从Gradle中的Android库jar中排除BuildConfig.class和R.class

时间:2015-03-02 08:43:52

标签: android gradle android-gradle

使用Gradle构建Android库项目时,从生成的.jar中排除BuildConfig.class和R.class的正确方法是什么?

6 个答案:

答案 0 :(得分:6)

添加自定义任务:

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

答案 1 :(得分:4)

你应该在CompileReleaseSources步骤之后得到它。

这对我有用:

task removeBuildConfig(dependsOn: "compileReleaseSources") {
    doFirst {
        file("$buildDir/intermediates/classes/release/pathToFile/BuildConfig.class").delete()
    }
} 

答案 2 :(得分:3)

最优雅的解决方案可能是在库的build.gradle的末尾添加:

afterEvaluate {
    generateReleaseBuildConfig.enabled = false
}

答案 3 :(得分:2)

要使用R和BuildConfig,请使用此

afterEvaluate {
    generateReleaseBuildConfig.enabled = false
    generateDebugBuildConfig.enabled = false
    generateReleaseResValues.enabled = false
    generateDebugResValues.enabled = false
}

在建造之前不要输给清洁项目

答案 4 :(得分:0)

这对我有用

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()
    }
} 

答案 5 :(得分:0)

这是我的代码,添加捆绑任务后,我们可以在此处将其挂接。

tasks.whenTaskAdded {
    if (it.name == 'bundleRelease' || it.name == 'bundleDebug') {
        // bundle task is Zip
        it.exclude '**/BuildConfig.class', '**/R.class', '**/R$*.class'
    }
}