Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite Application Id Outside of Flavors and Build Types

Tags:

I have an application with many flavors (A,B,C) and two build types (debug,release)

In the build type debug I add a suffix to the application ID like so:

debug {
    applicationIdSuffix '.debug'
}

This is fine for flavors A and B but I can't append .debug to flavor C's application ID.

I have looked at overriding on the variant like I have for the versionCode but no luck.

    applicationVariants.all { variant ->
        def changedVersionCode = variant.versionCode
        variant.
        variant.outputs.each { output ->
            if (variant.buildType.name != "debug") {
                output.setVersionCodeOverride(project.ext.versionCode)
                changedVersionCode = project.ext.versionCode
            } 
        }
        changeApkFileName(variant,changedVersionCode)
    }

Is it possible to override a variants application ID depending on the flavor. For example my plan was to do something like this:

variant.buildType.name.replace('.debug','')

like image 431
StuStirling Avatar asked Apr 25 '18 10:04

StuStirling


People also ask

What is a build type and a product flavor in android?

buildType is the how of the build. flavor is the what of the build.

When would you use a product Flavour in your build setup?

You use same core ingredients to make the base but will use different toppings for each one to have a different taste. Similarly, android apps can have the same base functionalities with changes to some of the features like styles, logo etc. This can be achieved using product flavours.

How do I change a variant in build?

You can change the build variant to whichever one you want to build and run—just go to Build > Select Build Variant and select one from the drop-down menu. To start customizing each build variant with its own features and resources, however, you'll need to know how to create and manage source sets.

What does build type user mean?

Build Type refers to build and packaging settings like signing configuration for a project. For example, debug and release build types. The debug will use android debug certificate for packaging the APK file. While, release build type will use user-defined release certificate for signing and packaging the APK.


1 Answers

Is it possible to override a variants application ID depending on the flavor

Yes it is possible.

The reason you are not able to see the expected id is:

Because Gradle applies the build type configuration after the product flavor, the application ID for the "C" build variant will be "<your-applicaion-id>.debug".

So if you want it to be different for different flavors then you have to segregate the applicationIdSuffix for different flaovors and remove it from debug {} as follows:

android {
    defaultConfig {
        applicationId "<your-application-id>"
    }
    productFlavors {
        A {
            applicationIdSuffix ".debug"
        }
        B {
            applicationIdSuffix ".debug"
        }
        C {
            applicationIdSuffix ""
        }
    }
}

For more details, refer to official documentation.

like image 75
Sagar Avatar answered Sep 28 '22 05:09

Sagar