Apoorva Naganalli
Apoorva Naganalli

Reputation: 31

ERROR: Gradle sync failed: Could not get unknown property 'API_KEY' for DefaultConfig_Decorated

ERROR: Could not get unknown property 'API_KEY' for DefaultConfig_Decorated{name=main, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=null, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}, mWearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.DefaultConfig. apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.2'
    defaultConfig {
        buildConfigField("String", "API_KEY", API_KEY)        //error here
        buildConfigField("String", "ER_API_KEY", ER_API_KEY)
        applicationId "com.gpads.gautham.imagetotextanalysis"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 2
        versionName "2.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    // ... other values
}

Upvotes: 3

Views: 10400

Answers (4)

harika
harika

Reputation: 43

If you get an "illegal start of expression" using Braian Coronel's answer, you can try to escape the quotes with:

def API_KEY_2 = "your api key"
buildConfigField("String", "API_KEY", "\"API_KEY_2 \"")

Upvotes: 0

Braian Coronel
Braian Coronel

Reputation: 22867

The error is that you are trying to use something you did not define.

As a solution, you have several ways to initialize the configuration field:


. In the same scope it could be:

Look at the following code where API_KEY_2 is defined.

android {
    //...
    defaultConfig {
        //...
        def API_KEY_2 = "API_KEY_2"
        buildConfigField("String", "API_KEY", API_KEY_2)
    }
}

. Or in a global scope:

build.gradle

class Globals {
    static String API_KEY_2 = "API_KEY_2"
}

android {
    //...
    defaultConfig {
        //...
        def API_KEY_2 = "API_KEY_2"
        buildConfigField("String", "API_KEY", API_KEY_2)
    }
}

Note: I don't recommend using BuildConfig.SOMETHING to initialize a buildConfigField

GL

Upvotes: 1

Mirieri Mogaka
Mirieri Mogaka

Reputation: 561

To avoid any errors with your code add this snippet, found this as a solution for my movie project:

def getProperty(String filename, String propName) {
    def propsFile = rootProject.file(filename)
    if (propsFile.exists()) {
        def props = new Properties()
        props.load(new FileInputStream(propsFile))
        if (props[propName] != null) {
            return props[propName]
        } else {
            print("No such property " + propName + " in file " + filename);
        }
    } else {
        print(filename + " does not exist!")
    }
}
android {
    compileSdkVersion 27
    buildToolsVersion '27.0.2'
    defaultConfig {
        buildConfigField "String", "API_KEY", "\"${getProperty("local.properties", API_KEY)}\""
        buildConfigField "String", "\"${getProperty("local.properties", ER_API_KEY)}\""
        applicationId "com.gpads.gautham.imagetotextanalysis"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 2
        versionName "2.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

Upvotes: 2

Osaf
Osaf

Reputation: 87

Change this buildConfigField("String", "API_KEY", API_KEY) Into this

    buildConfigField "String", "API_KEY", "\" API_KEY\"" 

Upvotes: 6

Related Questions