anthony anthony - 3 months ago 60
Android Question

How can I access a BuildConfig value in my AndroidManifest.xml file?

Is it possible to access a BuildConfig value from AndroidManifest.xml?

In my build.gradle file, I have:

defaultConfig {
applicationId "com.compagny.product"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"

// Facebook app id
buildConfigField "long", "FACEBOOK_APP_ID", FACEBOOK_APP_ID
}


FACEBOOK_APP_ID
is defined in my gradle.properties files:

# Facebook identifier (app ID)
FACEBOOK_APP_ID=XXXXXXXXXX


To use Facebook connect in my app, I must add this line to my AndroidManifest.xml:

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/applicationId"/>


I want to replace
@string/applicationId
by the BuildConfig field
FACEBOOK_APP_ID
defined in gradle, like this:

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="FACEBOOK_APP_ID"/>


Is that possible using BuildConfig? If not, how can I achieve this?

Answer

Replace

buildConfigField "long", "FACEBOOK_APP_ID", FACEBOOK_APP_ID

with

resValue "string", "FACEBOOK_APP_ID", FACEBOOK_APP_ID

then rebuild your project (Android Studio -> Build -> Rebuild Project).

The two commands both produce generated values - consisting of Java constants in the first case, and Android resources in the second - during project builds, but the second method will generate a string resource value that can be accessed using the @string/FACEBOOK_APP_ID syntax. This means it can be used in the manifest as well as in code.

Comments