3

I have old apps using non compliant applicationId. I trying to migrate them with flavorDimensions to share so common assets & code. I have this flavors setup :

defaultConfig {
    applicationId "com.example"
}

flavorDimensions 'fruit', 'env'

productFlavors {
    pear {
        dimension 'fruit'
    }

    banana {
        dimension 'fruit'
    }

    staging {
        dimension 'env'
    }

    prod {
        dimension 'env'
    }
}

I would like to have these applicationId by flavor combination :

  • pearStaging : com.example.pear_staging (note the "_")
  • pearProd : com.example.pear
  • bananaStaging : com.example.banana_staging (note the "_")
  • bananaProd : com.example.banana

I have tried to use applicationIdSuffix :

productFlavors {
    pear {
        dimension 'fruit'
        applicationIdSuffix 'pear'
    }

    banana {
        dimension 'fruit'
        applicationIdSuffix 'banana'
    }

    staging {
        dimension 'env'
        applicationIdSuffix '_staging'
    }

    prod {
        dimension 'env'
    }
}

but suffixes are separated with dot by default. So it's generate wrong applicationId, ex:

flavor pearStaging : com.example.pear._staging (note the "." before "_")

I saw answers on this thread : How to set different applicationId for each flavor combination using flavorDimensions? They talk about a workaround using mergedFlavor.setApplicationId(...) to override applicationId at the end. But this not working if I use in combination with google services gradle plugin.

Because during plugin process phase, I got this error :

* What went wrong:
Execution failed for task ':app:processPearStagingDebugGoogleServices'.
> No matching client found for package name 'com.example'

As you see, it use default applicationId/package name, not the appId overrided in android.applicationVariants.all phase.

So there is a better way to defined my applicationId per flavor combination that works with google services task ? (I need to keep these applicationId with "_", can't change it).

Aure77
  • 3,034
  • 7
  • 33
  • 53
  • Unfortunately, there is no way to do this automatically. As you can see in [docs](https://developer.android.com/studio/build/application-id#change_the_application_id_for_build_variants) the only option is suffix with dot – kAvEh Apr 26 '20 at 05:57
  • I found a solution but without flavorDimension combination... (and without suffix) – Aure77 Apr 27 '20 at 16:40

1 Answers1

0

It is not possible with the current way that google services gradle plugin works. Maybe there's a way to feedback to Google about that, but meanwhile, the only way to achieve the application IDs with "_" is to rename the application ID after the apk is built. So, the flavour-specific application ID would be built with "." separators first, then rename the application ID after the apk is built.

This could be done with the standard procedure of using apktool to open the apk, change the application ID to what you need, then repackage with apktool, zipalign, re-sign, etc. If it may be too tedious, there are GitHub projects that attempt to do the same, but they may not work on all apks.

For example:

  1. https://github.com/testwhat/PackageRenamer
  2. https://github.com/sjitech/ApkRename
auspicious99
  • 3,902
  • 1
  • 44
  • 58
  • That suppose to declare google-services.json with a dot inside to pass compilation ? This should cause issue with applicationId matching within google services. – Aure77 Apr 27 '20 at 16:37
  • I mean, something like `applicationIdSuffix '.staging'` instead of `applicationIdSuffix '_staging'`. For google-services.json, you can have a different google-services.json for each flavor combination. – auspicious99 Apr 28 '20 at 02:39
  • As I say in my question : "I need to keep these applicationId with "_", can't change it" because these appId are used in backend server and partners. Change it need actions by a lot of persons, that I don't looking for. I know that with dot all will be simplier. – Aure77 Apr 29 '20 at 07:03