Overwrite Application Id Outside of Flavors and Bu

2019-02-22 11:01发布

问题:

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','')

回答1:

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.