Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidManifest package name conflict with flavors in Android

I am trying to make 2 flavors of my app.

Gradle:

defaultConfig {
        applicationId "be.myname.firstapp"
        minSdkVersion 15
        targetSdkVersion 23 }

productFlavors {
        raterate {
            applicationId = "be.myname.firstapp"
        }
        scarsforlife {
            applicationId = "be.myname.secondapp"
        }
    }

I have an AndroidManifest in the main folder, fistapp folder & secondapp folder.

Packagename in first folder: be.myname.fistapp, second folder: be.myname.secondapp.

In the main folder I first had: be.myname.firstapp, but that conflicted with be.myname.secondapp. Now I tried:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="${packageId}">

</manifest>

And got this:

Attribute manifest@package at AndroidManifest.xml:3:5-27 requires a placeholder substitution but no value for <packageId> is provided.
AndroidManifest.xml Error:
    Overlay manifest:package attribute declared at AndroidManifest.xml:3:5-45 value=(be.myname.secondapp)
    has a different value=(be.myname.secondapp) declared in main manifest at AndroidManifest.xml:3:5-27
    Suggestion: remove the overlay declaration at AndroidManifest.xml   and place it in the build.gradle:
        flavorName {
            applicationId = "be.myname.secondapp"
        }

I'm confused at this point. What is the correct way to do this? :)

like image 556
TomCB Avatar asked Sep 26 '22 03:09

TomCB


1 Answers

You don't need to modify the package in AndroidManifest.xml for every flavors.

Quoting from tools.android.com:

Therefore, we have decoupled the two usages of package name:

  • The final package that is used in your built .apk's manifest, and is the package your app is known as on your device and in the Google Play store, is the "application id".
  • The package that is used in your source code to refer to your R class, and to resolve any relative activity/service registrations, continues to be called the "package".

You can specify the application id in your gradle file as follows:

app/build.gradle:

apply plugin: 'com.android.application'

android {
  compileSdkVersion 19
  buildToolsVersion "19.1"

  defaultConfig {
      applicationId "com.example.my.app"
      minSdkVersion 15
      targetSdkVersion 19
      versionCode 1
      versionName "1.0"
  }
  ...

As before, you need to specify the package used for your code in the Manifest file, just as shown in the above AndroidManifest.xml sample.

Here comes the critical part: When you've done the above, the two packages are independent. You are completely free to refactor your code - changing the internal package used for your activities and services, updating your Manifest package, and refactoring your import statements. This will have no bearing on the final id of your application, which is now always going to be the applicationId specified in the Gradle file.

like image 93
Mattia Maestrini Avatar answered Oct 03 '22 10:10

Mattia Maestrini