Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in build config in Android Studio

I am running a app in Android Studio. Following is the code:

strings.xml

<resources>
  <string name="google_nearby_api_key">XXXXXXXXXXXXXXXXXXXXXXXX</string>
</resources>

Manifest file

<application
    <meta-data
        android:name="com.google.android.nearby.messages.API_KEY"
        android:value="@string/google_nearby_api_key"/>
</application>

build.gradle

def filesAuthorityValue = applicationId + ".files"
manifestPlaceholders = [filesAuthority: filesAuthorityValue]
buildConfigField "String", "FILES_AUTORITY", "\"${filesAuthorityValue}\""
resValue "string", "google_nearby_api_key", getLocalProperties().getProperty("google_nearby_api_key")

I get the error:

Error:(35, 0) Build Config field cannot have a null parameter

Can anyone please help on this?

like image 745
user1613245 Avatar asked Jan 01 '18 05:01

user1613245


1 Answers

I checked on my machine this is working for me

local.properties

GOOGLE_NEARBY_API_KEY="XXXXXXXXXX"

build.gradle

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
resValue "string", "google_nearby_api_key", properties.getProperty('GOOGLE_NEARBY_API_KEY')

You will find the generated file with your string in:

app/build/generated/res/resValues/{build-type}/values/generated.xml

Now you can use the string in your app as @string/google_nearby_api_key.

Also, when I removed the key from local.properties I am getting the same error:

Error:(17, 1) A problem occurred evaluating project ':app'.
> Build Config field cannot have a null parameter

So, you probably do not have the key in your local.properties.

And you also seemed to have manually added the string in your strings.xml resource file, you shouldn't do this if you want to hide your API key. Inject it through your gradle build (the above approach).

like image 142
jayeshsolanki93 Avatar answered Oct 05 '22 18:10

jayeshsolanki93