Sriram
Sriram

Reputation: 10568

Unable to reference BuildConfig in app gradle file - Android Studio

I am experimenting with basic gradle functionality. I am trying to print the product flavor that is currently being built. My gradle build file looks like this:

android {
    compileSdkVersion 27
    flavorDimensions "versionCode"
    defaultConfig {
        applicationId "com.test.test"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 34
        versionName "0.9.14.20180718"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            debuggable false
            multiDexEnabled true
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        release {
            debuggable false
            minifyEnabled true   //for optimization.
            shrinkResources true
            multiDexEnabled true

            logger.debug("Hello world.")
            logger.debug("Build flavor being used: " + BuildConfig.FLAVOR)

            //check for build flavour.
            /*if(BuildConfig.FLAVOR.contentEquals("managed")) {
                logger.debug("Managed flavour being used here.")
            } else if(BuildConfig.FLAVOR.contentEquals("debugFlavor")) {
                logger.debug("Debug flavor being used.")
            }*/

            //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {
        managed {
            applicationId 'com.test.testing'
        }
        debugFlavor {
            applicationId 'com.test.testdebug
        }
    }
}

Whenever I try to change the Gradle file, I get the following error on sync:

Error:(30, 0) Could not get unknown property 'BuildConfig' for BuildType_Decorated{name=release, debuggable=false, testCoverageEnabled=false, jniDebuggable=false, pseudoLocalesEnabled=false, renderscriptDebuggable=false, renderscriptOptimLevel=3, minifyEnabled=true, zipAlignEnabled=true, signingConfig=null, embedMicroApp=true, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}} of type com.android.build.gradle.internal.dsl.BuildType.
<a href="openFile:I:\work\workspace\test\app\build.gradle">Open File</a>

I am not sure what the error is. I checked the BuildConfig.java file in the project repository and they exist and look just fine.

Upvotes: 0

Views: 3671

Answers (2)

shizhen
shizhen

Reputation: 12583

logger.debug("Build flavor being used: " + BuildConfig.FLAVOR)

I am not sure what the error is. I checked the BuildConfig.java file in the project repository and they exist and look just fine.

This line is technically wrong, BuildConfig.java is a file generated by gradle build tool, this file can be used only for your Java/Kotlin source code, but not for any of your build.gradle gradle script files.

Inside gradle script, the correct way for printing out build flavor is as below:

project.afterEvaluate {
    android.productFlavors.all { flavor ->
        println "flavor.name: " + flavor.name
    }
}

If you want to print out build variants, it can be done as below:

android.applicationVariants.all { variant ->
    println "variant.name: " + variant.name
}

Upvotes: 1

seyed Jafari
seyed Jafari

Reputation: 1265

as developer.andorid states you should specify each flavor dimension explicitly:

flavorDimensions "version"
productFlavors {
    demo {
        // Assigns this product flavor to the "version" flavor dimension.
        // This property is optional if you are using only one dimension.
        dimension "version"
        applicationIdSuffix ".demo"
        versionNameSuffix "-demo"
    }
    full {
        dimension "version"
        applicationIdSuffix ".full"
        versionNameSuffix "-full"
    }
}

Upvotes: 0

Related Questions