-->

manifestPlaceholders value is not string

2019-04-14 23:25发布

问题:

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.