Set a global variable in gradle that can use in manifest file
Asked Answered
L

4

17

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?

Lafreniere answered 23/10, 2015 at 4:55 Comment(3)
gradle is a part of build system/ IDE, not related with the appPayton
if you explain the real use case, I think may be we can helpPayton
Possible duplicate of #17198136Etz
K
26

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" />
Kovno answered 23/10, 2015 at 6:15 Comment(5)
Thank so much. This is exactly my situation. I've tried to set a variable facebookProdiderId inside build.gradle then reused inside android manifest file like ${facebookProdiderId}, but it does not workLafreniere
Glad I could help you! :)Kovno
can we use same applicationId with multiple flavors in this.Cage
@TusharPandey to be honest not sure, I think it could work, give it a try.Kovno
I got solution , there is no need of content provider. it only requires when we pick image from gallery and share it in facebook.Cage
P
19

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.

Phan answered 5/11, 2016 at 13:41 Comment(0)
A
17

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.

Abdulabdulla answered 19/9, 2016 at 17:32 Comment(1)
This is a more convenient solution. Thank you.Ruination
I
2

To use the string in Manifest, you can directly make it in strings.xml. Like this,

<string name="variable_name">value</string>
Ilianailine answered 23/10, 2015 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.