首页 > 解决方案 > generateReleaseBuildConfig errors out in a library

问题描述

Android Studio project. I have two library modules, neither of them has any Java in it. I'm trying to suppress BuildConfig generation, so that there's no generated Java either. The gradle file for the first one goes:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26

    defaultConfig {
        minSdkVersion 3
        targetSdkVersion 26

        externalNativeBuild {
            ndkBuild {
                abiFilters "armeabi", "x86", "x86_64", "armeabi-v7a", "mips", "arm64-v8a"
                targets "pad", "gif"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
        }
        debug {
            jniDebuggable true
        }
    }

    externalNativeBuild {
        ndkBuild {
            path file("src/main/jni/Android.mk")
        }
    }
}

dependencies {}

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

For the other:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26

    defaultConfig {
        minSdkVersion 3
        targetSdkVersion 26

        externalNativeBuild {
            ndkBuild {
                abiFilters "armeabi", "x86", "x86_64", "armeabi-v7a", "mips", "arm64-v8a"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
        }
        debug {
            jniDebuggable true
        }
    }

    externalNativeBuild {
        ndkBuild {
            path file("src/main/jni/Android.mk")
        }
    }

    flavorDimensions 'Color'
    productFlavors {
        Blue {
            dimension "Color"
            externalNativeBuild {
                ndkBuild {
                    cppFlags "-DBLUE"
                }
            }
        }
        Red {
            dimension "Color"
            externalNativeBuild {
                ndkBuild {
                    cppFlags "-DRED"
                }
            }
        }
    }
}

dependencies {
    implementation project(':Foo')
}

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

The first one worked for a while, no problem. The second one, I've created just today, and the Gradle sync on that fails:

A problem occurred configuring project ':LibBar'.

Could not get unknown property 'generateReleaseBuildConfig' for project ':LibBar' of type org.gradle.api.Project.

If I comment that line out, it complains about generateDebugBuildConfig.

The second module depends on the first one, not sure if it matters.

Both have a manifest with nothing but package.

What am I missing? I've tried cleaning the project, invalidating the caches. Gradle is the latest, so is Android Studio (just updated).

标签: javaandroidgradleandroid-gradle-plugin

解决方案


The build system decorates the step name with the current build flavor. So it should be instead:

afterEvaluate {
    generateBlueReleaseBuildConfig.enabled = false
    generateBlueDebugBuildConfig.enabled = false
    generateRedReleaseBuildConfig.enabled = false
    generateRedDebugBuildConfig.enabled = false
}

推荐阅读