Android, Gradle, Product Flavors and Manifest

In build.gradle , I have product settings:

 productFlavors { AlternateFlavour { applicationId "com.myapp.alternateflavour" } } 

Then, in the sourceSets section, I use different resources, resources, and manifest directories for these flavors:

 sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } AlternateFlavour { manifest.srcFile 'manifest-tmp/AlternateFlavour/AndroidManifest.xml' java.srcDirs = ['src'] res.srcDirs = ['res-tmp/AlternateFlavour'] assets.srcDirs = ['assets-tmp/AlternateFlavour'] } } 

OK so far.

In this manifesto, used by a fragrance that is partially generated automatically, I:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myapp.myapp" android:versionCode="1010001" android:versionName="V1.1.0.1" > 

but in the original manifest in the root project, the following looks:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myapp.myapp" android:versionCode="1010000" android:versionName="V1.1.0.DEBUG" > 

This will result in a Gradle error:

 Error: Attribute manifest@versionCode value=(1010001) from AndroidManifest.xml:4:5-28 is also present at AndroidManifest.xml:4:5-28 value=(1010000). Attributes of <manifest> elements are not merged. 

Why is he trying to merge with the original manifesto in general when I indicated that he should look elsewhere?

And how can I stop this?

I expect some to question why I do it this way at all, or really why I don't use the proposed flavor design framework. Well, I need a normal manifest to use outside Gradle, for example. for deployment from Eclipse (one at a time, please!), and I also need versions that will be inserted by the build process.

+5
source share
2 answers

I will remember what @CommonsWare said in his answer, but now I have solved it as follows:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.myapp.myapp" tools:replace="android:versionName,android:versionCode" android:versionCode="1010001" android:versionName="V1.1.0.1" > 

Note the two tools snippet.

I knew about this from the beginning, but I was suspended from trying because the full Gradle error related to three problems:

  • Versioncode
  • versionName
  • Maps API Key

all of which are automatically inserted. However, he only preferred tools:replace as a suggestion for the last one, so I got the impression that it would not work with manifest attributes. Actually it is.

0
source

Why is he trying to merge with the original manifesto in general when I indicated that he should look elsewhere?

You said that the manifest of taste is in a different place. The explicit merge process brings together all the relevant manifestos:

  • main
  • assembly type
  • product taste
  • libraries

the manifest in main always matters.

And how can I stop this?

This is not the case, since the manifest in main always matters.

The best solution in the general case is to get rid of versionCode and versionName from all manifestations and install them in build.gradle , where you have full programmatic control.

The next best solution is to move versionCode and versionName from the main manifest to the flavor of the product that you don't see here. I have never seen anyone create only one taste of a product, since AFAIK is pointless - you can only ever create this one taste of a product (since AFAIK has no aromatic designs as soon as you introduce the aromas). Product flavors are usually in 2+ groups (for example, a google flavor that uses Play Services and a standalone flavor that doesn't). If the values ​​in main really match a flavor other than AlternateFlavour , move main version... .

In addition, you can review the documentation on the manifest merge process to see if you can insert the correct directives to get what you want.

+4
source

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


All Articles