3

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
}
cezar
  • 11,616
  • 6
  • 48
  • 84
  • Possible duplicate of [How to generate buildConfigField with String type](https://stackoverflow.com/questions/30796533/how-to-generate-buildconfigfield-with-string-type) – Hemant Parmar Mar 01 '18 at 10:25
  • 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.java – Apoorva Naganalli Mar 01 '18 at 11:07

3 Answers3

6

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

    buildConfigField "String", "API_KEY", "\" API_KEY\"" 
Osaf
  • 87
  • 1
  • 12
2

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"
    }
}
cezar
  • 11,616
  • 6
  • 48
  • 84
Mirieri Mogaka
  • 517
  • 4
  • 23
1

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

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62