3

So I have 10 flavors in my android project: fl1, fl2, ..., fl10 with the permissions below. Now I want my fl5 not have the android.permission.REQUEST_INSTALL_PACKAGES. How to do this?

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

I know that I can have a different manifest file in fl5/AndroidManifest.xml. And it can include any excessive element for this specific build. But this can be a source of errors and mistakes as I should be careful of syncing every time I add an element to main manifest.

I have searched and found different solutions. But I couldn't implement them in my project because they weren't exact. Any help is appreciated.

Here is my fl5/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mycompany.myapp">

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" tools:node="remove"></uses-permission >

And here it is my flavors in build.gradle file:

  productFlavors {
    fl1 {flavorDimensions "in_1"}
    fl2 {flavorDimensions "in_2"}
    fl3 {flavorDimensions "in_3"}
    fl4 {flavorDimensions "in_4"}
    fl5 {flavorDimensions "in_5"}  
    fl6 {flavorDimensions "in_6"}
    fl7 {flavorDimensions "in_7"}
    fl8 {flavorDimensions "in_8"}
    fl9 {flavorDimensions "in_9"}
    fl10 {flavorDimensions "in_10"}       
}
sajjad
  • 834
  • 6
  • 13
  • Does this answer your question? [Removing AndroidManifest element with gradle product flavors](https://stackoverflow.com/questions/22392593/removing-androidmanifest-element-with-gradle-product-flavors) – Mir Milad Hosseiny Jan 19 '20 at 08:00
  • Look at this https://developer.android.com/studio/build/manifest-merge – Squti Jan 19 '20 at 08:05
  • Thanks for the suggestion. I did all of it but still I can't get the result. I don't know where I am making a mistake. @MirMiladHosseiny – sajjad Jan 19 '20 at 08:41

2 Answers2

5

Add tools:node="remove" in your element. In your case, add the following line in fl5/AndroidManifest.xml.

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" tools:node="remove"/>

Take a look at Removing AndroidManifest element with gradle product flavors and How to remove specific permission when build Android app with gradle? questions.

Mir Milad Hosseiny
  • 2,769
  • 15
  • 19
3

Best and easiest way is to create a different AndroidManifest for each flavor. To reduce redundancy, you can create an AndroidManifest under the main folder containing all activities, services, etc... and separate AndroidManifest under each flavor's folder containing the permissions for that flavor.

For Example, the fl1 Manifest (located atapp/src/fl1/AndroidManifest.xml) should look like:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android">  
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>  

the fl2 Manifest (located at app/src/fl2/AndroidManifest.xml) should look like:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android">  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>  

The main AndroidManifest file (located atapp/src/main/AndroidManifest.xml) should look like always but without permissions:

<manifest  
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- permissions are imported via brand-specific AndroidManifest.xml -->

    <!-- common manifest elements -->
    <application
        android:name=".ui.FSApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">

        <!-- activities -->
        <activity
            android:name=".ui.activities.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <activity
            android:name=".ui.activities.SecondActivity/>

        <!-- receiver ->
        <receiver android:name=".receiver.BlogPostReceiver"/>

    </application>
</manifest>  
whd.nsr
  • 684
  • 6
  • 12