2
votes

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
}
3
package com.gpads.gautham.imagetotextanalysis; public final class BuildConfig { public static final boolean DEBUG = Boolean.parseBoolean("true"); public static final String APPLICATION_ID = "com.gpads.gautham.imagetotextanalysis"; public static final String BUILD_TYPE = "debug"; public static final String FLAVOR = ""; public static final int VERSION_CODE = 2; public static final String VERSION_NAME = "2.0"; } @HemantParmar this is my BuildConfig.javaApoorva Naganalli

3 Answers

4
votes

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

    buildConfigField "String", "API_KEY", "\" API_KEY\"" 
1
votes

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"
    }
}
0
votes

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