5

Short question:

How can I change the package name for debug or release build type in a library module?

Context:

In an Android project with MVP + Clean architecture, we have the repository pattern in a library module. We want to include Firebase on that library with two environments (development and production).

I already created the project in Firebase (com.example.com and com.example.com.dev) and then downloaded the respective google-services.json to src/main and src/debug folders.

Gradle google-services plugin validates the module package name with the client ID defined in the google-services.json, but Android restrict to change the applicationId in a library (I can't find the technical reason why)

Things that I tried:

  • Have two AndroidManifest.xml with different package property. com.example.com in the src/main and com.example.com.dev in src/debug/ but the second one it is just ignored
  • Set manifest.srcFile in Gradle sourceSets. The file is in the list when I run ./gradlew sourceSets but the package name doesn't change
  • Two different flavors in the library module and set different manifests for each one. The package anyway doesn't change.

At this moment I have just two suitable solutions: 1. Keep the Firebase setup and implementation in the app module outside of the repository. 2. Have only one environment for Firebase.

Thanks a lot for any help or advice.

EDIT:

Consider that I need to modify the package in a module (library), not in the app. For some weird reason Gradle shows this error when I try to use applicationIdSuffix or applicationId in a module:

ERROR: Library projects cannot set applicationIdSuffix.

Max Cruz
  • 1,215
  • 13
  • 17
  • I've not been able to set this in an automated fashion either, but the technical reason why suffix can't be used is explained here: https://groups.google.com/d/msg/adt-dev/OF-kwxX7Cuc/6cOuvS4FEAIJ – thoutbeckers Aug 05 '19 at 07:31

2 Answers2

1

Library projects cannot set applicationId.

This is the designed behaviour of Android Library project.

Two different flavors in the library module and set different manifests for each one. The package anyway doesn't change.

Probably you need to change your Build Variants to that particular flavor/buildtype then you can see the updated package name taking effect.

See below screenshot.

enter image description here

shizhen
  • 12,251
  • 9
  • 52
  • 88
1

I spent some time investigating and trying different alternatives to achieve two different package name per flavor or build type in a library module, and my short answer is: You can't, at least without doing something tricky and dirty.

Some guys that recommend using the parameter applicationIdSuffix or applicationId, that option doesn't work because of the design of library modules restricts those parameters to the app module. In a library, the package must be defined in the manifest.

That restriction delimits options to flavors, but, the package attribute in the manifest isn't merged, and you get an error when having different package name for the scr/main/AndroidManifest.xml and src/flavor/AndroidManifest.xml.

A tricky solution is to get the currently selected flavor form the task name and then change the main manifest in the sourceSets.

sourceSets {
    main {
        manifest.srcFile "src/$flavor_name/AndroidManifest.xml"
    }
}

This hack works but in general is a bad idea, and I prefer to change the design of my architecture and implement the repository for both Firebase variants in a different way.

Gradle plugin: 3.2.0

Max Cruz
  • 1,215
  • 13
  • 17