简体   繁体   中英

Android, how to exclude a specific permission for a specific flavor?

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"}       
}

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 at app/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 at app/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>  

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM