I have my facebook application id on my gradle properties with :
FACEBOOK_APPLICATION_ID="XXXXXXXXXXXXXXX"
and I set in my defaultConfig :
manifestPlaceholders = [facebookAppId: FACEBOOK_APPLICATION_ID]
then, I want to use this in my manifest with :
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="${facebookAppId}" />
but it doesn't work. The only things working are to set my faceboook id in a string :
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id" />
or to put this directly with :
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="\ XXXXXXXXXXXXXX" />
but I must set my facebook id in my gradle properties. What is the right way to do this ?
Thanks.
I am new on Gradle, but if I understood your questions I would suggest using something like that:
buildTypes {
debug {
// ...
}
release {
// ...
resValue "string", "facebook_application_id", "XXXXXXXXXXXXXXXXX"
}
}
Then, at your release build, for this example, you can use it as
android:value="#string/facebook_application_id"
Or even
R.string.facebook_application_id
Related
Integrating some libraries like Branch-io in Android need to define meta-data in the project manifest. some of these variables are like TestMode
<meta-data android:name="io.branch.sdk.TestMode" android:value="true" />
So, when we want to publish the application we should change it to False.
Is there any way to define a variable somewhere according to BuildType and assign it to the Meta-data to that?
You can do it by adding manifestPlaceholders to your build.gradle file:
android {
...
buildTypes {
debug {
manifestPlaceholders = [isBranchSdkInTestMode:"true"]
...
}
release {
manifestPlaceholders = [isBranchSdkInTestMode:"false"]
...
}
}
...
}
In AndroidManifest.xml, you can use it as ${isBranchSdkInTestMode}:
<meta-data android:name="io.branch.sdk.TestMode" android:value="${isBranchSdkInTestMode}" />
Yes you can inject build variables from gradle to manifest, it is done by adding variable to the build.gradle:
android {
defaultConfig {
manifestPlaceholders = [hostName:"www.example.com"]
}
deployConfg {
manifestPlaceholders = [hostName:"www.prod-server.com"]
}
...
}
And then in your manifest you can get it by:
<intent-filter ... >
<data android:scheme="http" android:host="${hostName}" ... />
...
</intent-filter>
You can read more about how this works here.
I'm an iOS developer who is trying to make some small changes on android side but with no clue of how to achieve them. My goal is to implement Facebook SDK inside the app, official documentation says:
In /app/res/values/strings.xml add new string element:
<string name="facebook_app_id">Facebook App ID</string>
Then add in AndroidManifest.xml file new meta-data like below:
<application android:label="#string/app_name" ...>
...
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id"/>
...
</application>
All is understood but my problem is that I have few product flavors based on which I build at the end stand alone apps, for each I have different Facebook App ID. I would appreciate any kind of help. Thanks!
You can create multiple build types or product flavors inside the app-level build.gradle file and then define different strings for each build variant like this
buildTypes {
prod{
resValue "string", "facebook_app_id", "your app id"
}
dev {
resValue "string", "facebook_app_id", "your app id"
}
}
Now you can access this string inside manifest
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id"/>
There are many ways. But as you already define multiple flavour in build.gradle in app level then you can define the string over there like this
android {
buildTypes {
flavour1 {
buildConfigField "String", "facebook_app_id", "\"first facebook id 1\""
}
flavour2 {
buildConfigField "String", "facebook_app_id", "\"first facebook id 2\""
}
}
}
It automatically initialise the facebook_app_id to the build when you are creating the build of the flavour.
Happy Coding :)
In build.gradle :
productFlavors {
appDev {
applicationId 'com.android.dev'
}
appTest {
applicationId 'com.android.test'
}
}
flavorDimensions "default"
Inside src folder:
create folder with flavors name
-src
-appDev(folder)
-java(folder)
-res(folder)
-values(folder)
-appTest(folder)
-java(folder)
-res(folder)
-values(folder)
inside values folder you can create strings.xml each folder act as different flavor
you need to pass your facebook app_id in the string files.
<string name="facebook_app_id">35166120881***</string>
<string name="fb_login_protocol_scheme">fb35166120881****</string>
...
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name" />
<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/fb_login_protocol_scheme" />
</intent-filter>
</activity>
...
</application>
Important this
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"/>
I'm unable to add the Branch keys as ManifestPlaceholders.
gradle.properties:
branch_api_key_qa="key_test_xxxxxxxxxxxxxxxxxxxxxxxx"
build.gradle
falvour_qa {
...
...
manifestPlaceholders = [branchKey : branch_api_key_qa]
}
AndroidManifest.xml
<meta-data
android:name="io.branch.sdk.BranchKey.test"
android:value="${branchKey}" />
The above code not making initSession callback in my launcher activity.
But providing the key directly in the manifest is working as expected
Try this
falvour_qa {
manifestPlaceholders = [
branchKey: 'key_test_xxxxxxxxxxxxxxxxxxxxxxxx'
]
}
I have done like this
<meta-data
android:name="io.branch.sdk.BranchKey"
android:value="key_live_**********" />
<meta-data
android:name="io.branch.sdk.BranchKey.test"
android:value="key_test_***************" />
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?
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"/>
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.
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>
I just face a similar issue where my facebook values were treated as a number and garbled.
If I make sure to wrap them in " then it works as expected and they are treated as strings.
manifestPlaceholders += [FACEBOOK_APP_ID: "\"" + rootProject.facebookAppId + "\"", FACEBOOK_CLIENT_TOKEN: "\"" + rootProject.facebookClientToken + "\""]
I want to create a global variable similar with applicationId.
It is set value in build.gradle and will be used in manifest. Is it possible?
You can set them, for instance I'm setting it for different product flavors
productFlavors {
production {
applicationId = "com.myapp.app"
resValue "string", "authority", "com.facebook.app.FacebookContentProvider5435651423234"
}
development {
applicationId = "com.myapp.development"
resValue "string", "authority", "com.facebook.app.FacebookContentProvider2134564533421"
}
qa {
applicationId = "com.myapp.qa"
resValue "string", "authority", "com.facebook.app.FacebookContentProvider29831237981287319"
}
}
And use it like this
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="#string/authority"
android:exported="true" />
If you just want to use the application id set in gradle in your manifest, you can simply use:
${applicationId}
For instance:
<provider
android:authorities="${applicationId}.ShareFileProvider" ... >
...
</provider>
If you want the same behavior with custom variables, you can use manifestPlaceholders, like this:
android {
defaultConfig {
manifestPlaceholders = [hostName:"www.example.com"]
}
}
And in your manifest:
<intent-filter ... >
<data android:scheme="http" android:host="${hostName}" ... />
...
</intent-filter>
See https://developer.android.com/studio/build/manifest-build-variables.html for more information.
While Marko's answer seems to work, there's currently a better solution that doesn't require adding variables to the string resource files.
The manifest merger accepts placeholders:
For custom placeholders replacements, use the following DSL to
configure the placeholders values :
android {
defaultConfig {
manifestPlaceholders = [ activityLabel:"defaultName"]
}
productFlavors {
free {
}
pro {
manifestPlaceholders = [ activityLabel:"proName" ]
}
}
will substitute the placeholder in the following declaration :
<activity android:name=".MainActivity" android:label="${activityLabel}" >
You can also manipulate those strings with groovy functions.
To use the string in Manifest, you can directly make it in strings.xml.
Like this,
<string name="variable_name">value</string>