How to change AndroidManifest.xml file based on build options?

I have an application with several build options. Variants are used to create versions of the same application for different companies. So, I have several different options that create different applications:

  • com.acme.app1
  • com.schmoe.app2
  • com.yop.app3
  • etc...

The build.gradle file handles this very well:

productFlavors { app1 { applicationId "com.acme.app1" } app2 { applicationId "com.schmoe.app2" } app3 { applicationId "com.yop.app3" } } 

Here is my problem. I integrate Dropbox into my applications. The AndroidManifest.xml file must be changed for each option to include the corresponding application key (saved in my string file). Dropbox has the following addition to the manifest file:

  <activity android:name="com.dropbox.client2.android.AuthActivity" android:launchMode="singleTask" android:configChanges="orientation|keyboard"> <intent-filter> <!-- Change this to be db- followed by your app key --> <data android:scheme="db-abcdef" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 

Each build option should change the following line:

  <data android:scheme="db-abcdef" /> 

to the corresponding value for each application variant. In other words, I need to replace part of the specified string based on the assembly option. For instance,

App1

  <data android:scheme="db-111111" /> 

App2

  <data android:scheme="db-222222" /> 

App3

  <data android:scheme="db-333333" /> 

The line is the same for each option before the text "db -".

I need to dynamically replace the variable part of the string (x value) "db-xxxxxx" with the string from my string file.

I think this can be done using gradle scripts, but I am a complete newb with gradle. HELP!

If you can help, please be very specific about what has been going on since I SUKKA to gradle files and scripts. Thanks in advance!

+7
source share
3 answers

You can do this the same way you would change the name of the application using a string resource. In your manifest, include:

 <data android:scheme="@string/db_scheme" /> 

Then you add resource folders for different types of assembly, and in each place your own copy of the resource file, for example res/values/flavor.xml :

 <?xml version="1.0" encoding="utf-8" standalone="no"?> <resources> <string name="app_name">Customer 1 App</string> <string name="db_scheme">db-111111</string> </resources> 

This will allow you to have different API keys, schemas, application names, icons, etc. for your various tastes.

+4
source

You can also save the line in build.gradle :

 productFlavors { app1 { applicationId "com.acme.app1" resValue "string", "my_app_key", "db-abc" } app2 { applicationId "com.schmoe.app2" resValue "string", "my_app_key", "db-def" } app3 { applicationId "com.yop.app3" resValue "string", "my_app_key", "db-ghi" } } 

And then use it like:

 <data android:scheme="@string/my_app_key" /> 
+15
source

For people visiting this in 2019: You can use manifestPlaceholders for each fragrance. In your case:

 productFlavors { app1 { applicationId "com.acme.app1" manifestPlaceholders.scheme = "db-111111" } app2 { applicationId "com.schmoe.app2" manifestPlaceholders.scheme = "db-222222" } app3 { applicationId "com.yop.app3" manifestPlaceholders.scheme = "db-333333" } } 

and then use it in your manifest:

 <activity android:name="com.dropbox.client2.android.AuthActivity" android:launchMode="singleTask" android:configChanges="orientation|keyboard"> <intent-filter> <!-- Change this to be db- followed by your app key --> <data android:scheme="${scheme}" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 
0
source

Source: https://habr.com/ru/post/1265817/


All Articles