Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio two flavors with different manifest files

I'm having issues with defining two different manifest files for my flavors in Android Studio. This is my current project structure:

Current project structure

The AndroidManifest.xml in the free flavor looks like this:

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

The AndroidManifest.xml in the main flavor has no uses-permissions, but contains the rest of the manifest code that is shared between all flavors.

The AndroidManifest.xml in the pro flavor looks like this:

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

build.gradle defines the two flavors like

productFlavors {
    free {
        applicationId 'se.example.package.free'
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName '1.0'
    }
    pro {
        minSdkVersion 14
        applicationId 'se.example.package.pro'
        targetSdkVersion 21
        versionCode 2
        versionName '1.1'
    }
}

The result that I am expecting is that the different flavors defines different uses-permissions. This is not the case. The result is currently that the both flavors only defines the <uses-permission android:name="com.android.vending.CHECK_LICENSE" /> as defined in AndroidManifest.xml in the pro flavor.

I have tried:

  • Clean project
  • Rebuild project
  • Restart Android Studio
  • Sync gradle

But without success. How am I to fix this? Any help is appreciated.

EDIT 1

I changed the location of each flavors AndroidManifest.xml file from each of the res folders to free and pro folder. The result of this:

  1. Pro flavor shows Licence permission as expected.
  2. Free flavor shows permissions from both AndroidManifest.xml files, License and network permissions (Should be only network)

This feels like an issue of project structure. What to make of this?

EDIT 2

I pulled the merge reports as Commonsware hinted, these are the reports regarding uses-permissions

Free:

uses-permission#com.android.vending.CHECK_LICENSE
ADDED from qwknoteGIT:licencing-library:unspecified:26:5
    android:name
        ADDED from qwknoteGIT:licencing-library:unspecified:26:22

Pro:

uses-permission#com.android.vending.CHECK_LICENSE
MERGED from qwknoteGIT:licencing-library:unspecified:26:5
like image 556
Marcus Avatar asked Feb 12 '15 12:02

Marcus


People also ask

How do I merge manifest files on Android?

If you want to apply the merge rule markers to only a specific imported library, add the tools:selector attribute with the library package name. For example, with the following manifest, the remove merge rule is applied only when the lower-priority manifest file is from the com. example. lib1 library.

How many manifest file can be in an application?

A manifest can contain only one application node. It uses attributes to specify the metadata for your application (including its title, icon, and theme).

How do you find a merged manifest?

In Android Studio 3.3 you can also see Merged Manifest by clicking on Merged Manifest tab. It's showed at the bottom of the editor pane when you open your standard project manifest.

How do I fix a failed manifest merger?

The initial process would be to open the manifest application known as the AndroidManifest. xml and then click on the Merged Manifest tab below your edit pane. Following which, Click on the merged manifest option. An Error would be visible at the right column and then one must try to solve the error.

What is the manifest file for Android app?

For an introduction to the app manifest file, see the app manifest overview. Your APK or Android App Bundle file can contain just one AndroidManifest.xml file, but your Android Studio project may contain several—provided by the main source set, build variants, and imported libraries.

When should I use Flavors and build types in Android?

Use flavors for situations where you might need different versions of the same app in the Play Store, for example, free and pro, or for situations where you are customizing the same app for multiple clients. Use build types to differentiate between dev, debug and production builds.

How do I See my merged manifest in Android Studio?

List of permissions the merger tool may add to the merged manifest Even before you build your app, you can see a preview of what your merged manifest looks by opening your AndroidManifest.xml file in Android Studio, and then clicking the Merged Manifest tab at the bottom of the editor.

How to specify multiple source sets for Android manifest?

// For each source set, you can specify only one Android manifest. // set in the src/main/ directory. ... // Create additional blocks to configure other source sets. // directory, you can specify that directory using the setRoot property.


4 Answers

Tech background:

on this link it explains the techniques and parameters that can be use for manifest merging: https://developer.android.com/studio/build/manage-manifests#merge_rule_markers

One in specific is the tools:node that points out how certain XML nodes on the manifest should behave whilst merging.

Solution:

to achieve some permisions in one and different in other manifest, add ALL permissions you need to the main and in the flavours manifest remove the ones you don't need, like the example below:

free remove the check license

<uses-permission
   android:name="com.android.vending.CHECK_LICENSE" 
   tools:node="remove"/>
like image 199
Budius Avatar answered Oct 30 '22 12:10

Budius


Your problem is coming from a library, not your flavors. Specifically, qwknoteGIT:licencing-library is requesting CHECK_LICENSE.

If you are not using that library in all flavors, use a flavored compile statement (e.g., proCompile) to only use that library in that flavor.

If you are using the library for all flavors, but feel confident that you do not need the permission in one flavor, that's where a tools:node attribute can be used, in the flavor's manifest, to block out that permission supplied by the library.

And the manifest merger report is your friend. :-)

like image 39
CommonsWare Avatar answered Oct 30 '22 12:10

CommonsWare


This should solve the problem, at least. I find it useful in specifying the exact manifest to use for each variant. Cheers! It explicitly directs to the manifest file under each variant folder.

    android {
      productFlavors {
        prod {
          manifest.srcFile "prod/AndroidManifest.xml"
        }
        dev {
          manifest.srcFile "dev/AndroidManifest.xml"
        }
      }
      ...

}
like image 21
Otieno Rowland Avatar answered Oct 30 '22 13:10

Otieno Rowland


Specify your Manifest exclusively under sourceSets, In your App build.gradle

 android {
    productFlavors {
                bizdartFlavourNoCallLog {
            minSdkVersion 16
            applicationIdSuffix '.bizdart'
            targetSdkVersion 26
            dimension "tier"
            sourceSets {
                main {
                    manifest.srcFile "src/bizdartFlavourNoCallLog/AndroidManifest.xml"
                }
            }
            copy {
                from 'src/bizdartFlavourNoCallLog/'
                include '*.json'
                into '.'
                }
            }
       }
    }
like image 25
Ebin Joy Avatar answered Oct 30 '22 13:10

Ebin Joy