Jakub Anioła
Jakub Anioła

Reputation: 330

Different style applicationId in multiple dimensions

Problem: setting applicationId depending on flavor. More problem: Two apps are already on the store, both having a different style of applicationId.

  1. com.name.dimension1.dimension2

  2. com.name.dimension1dimension2 (without dot)

In our Android app we need to introduce new flavors/dimensions. Dimensions:

flavorDimensions "company", "app", "server"

Seeing that, this is why we cannot use applicationIdSuffix in build.gradle because it is automatically adding . (dot) before suffix.

We already have method to decide which versionCode should be done for every flavor (thanks to that answer on Stack)

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    Pattern pattern
    if (tskReqStr.contains("assemble"))
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
    Matcher matcher = pattern.matcher(tskReqStr)
    if (matcher.find())
        return matcher.group(1).toLowerCase()
    else {
        println "NO MATCH FOUND"
        return ""
    }

Even more problem: Same method which helps us with setting version code, cannot help with applicationId.

def getFlavorApplicationId() {
    def flavor = getCurrentFlavor()
    if (flavor.contains("company1") && flavor.contains("app1")) {
        return ext.company1app1AppId
    } else if (flavor.contains("company2") && flavor.contains("app1")) {
        return ext.company2app1AppId
    } else if (flavor.contains("company2") && flavor.contains("app2")) {
        return ext.company2app2AppId
    }
    return "nothing"
}

When app is built/synchronized - everything is working properly (file BuildConfig and also generated apk is having correct applicationId). The problem is occurring when we are trying to Run the app with applicationId is depending on flavor.

Error while executing: am start -n "**non.of.those**/com.rsqtechnologies.rsqphysio.splash.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=non.of.those/com.rsqtechnologies.rsqphysio.splash.SplashActivity }
Error type 3
Error: Activity class {**non.of.those**/com.rsqtechnologies.rsqphysio.splash.SplashActivity} does not exist.
Error while Launching activity

If I understand this well, Android Studio is not taking applicationId to run the app from BuildConfig or even generated .apk. It is trying to generate it while launching app (when it cannot gather information about flavor from gradle task - def getCurrentFlavor())

when I am running the app by myself in Terminal with the same command but correct appId - everything works fine.

Things which I have already tried also:

Does anyone have some advice? Pretty complex problem, I can share more details if anyone interested.

Upvotes: 2

Views: 1052

Answers (1)

Henning
Henning

Reputation: 2560

When you are using groovy you can change the applicationId for all your merged flavors:

android.applicationVariants.all { variant ->
    def isApp1 = variant.name.contains('app1')
    def isApp2 = variant.name.contains('app2')

    def idAppendix = ""
    if (isApp1) idAppendix = ".withDotForApp1"
    if (isApp2) idAppendix = "noDotForApp2"

    mergedFlavor.setApplicationId(mergedFlavor.applicationId + idAppendix)
}

Note that this won't work if you plan to use kotlinscript since the applicationid of the merged flavor is a val and doesnt offer a setter.

Upvotes: 1

Related Questions