问题
In my AndroidManifest.xml file I have the following meta-data tag which should be populated dynamically:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="${FACEBOOK_APP_ID}"/>
My gradle file looks like this:
manifestPlaceholders = [
                GOOGLE_PROJECT_ID: "A888844613784",
                FACEBOOK_APP_ID: "888570042741264"
        ]
After "Build & Assemble" the FACEBOOK_APP_ID in the manifest file looks like this:
 <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="1481023616.000000" />
Unfortunately this is not a String, but a float value. That's not correct or what I want.
I know there is another way to define the FACEBOOK_APP_ID in the string.xml file. But since I have lots of flavors it would be nice and easy to maintain if we put all flavors-related parameters in the build.gradle file instead of the strings.xml files.
Does anyone know how to avoid the string to float conversion?
回答1:
You can use the following code to add a String value to your string resources from a build.gradle file:
resValue 'string', 'FACEBOOK_APP_ID', 'facebook_application_id'
But I'm not sure if the AndroidManifest.xml file does not support flavor specific strings (I can remember you will get a warning if you try, but I'm not sure).
You could also try to add a null-terminator to your FACEBOOK_APP_ID manifestPlaceholder as suggested in this answer:
FACEBOOK_APP_ID: "888570042741264\0"
Edit:
The code null-terminator method seems not to be working when used directly from the build.gradle file, it does however work when using the null-terminator inside the AndroidManifest.xml file:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="${FACEBOOK_APP_ID}\0"/>
回答2:
I use manifestPlaceholders and force gradle to recognize the string by escaping the quotes like so:
manifestPlaceholders = [facebook_app_id:"\"9999000099990000\"",
                         fb_login_protocol_scheme:"fb9999000099990000"]
You don't need to it for the second param since it has the string fb in front.
回答3:
This is working for me (if you do not use flavors, just put resValue into defaultConfig):
productFlavors {
    production {
        resValue 'string', 'facebookAppId', '22222222222'
        resValue 'string', 'facebookSchemeId', 'fb22222222222'
    }
    development {
        resValue 'string', 'facebookAppId', '11111111111'
        resValue 'string', 'facebookSchemeId', 'fb11111111111'
    }
}
Then in manifest:
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebookAppId" />
    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="@string/facebookSchemeId" />
        </intent-filter>
    </activity>
来源:https://stackoverflow.com/questions/41425868/manifestplaceholders-value-is-not-string