Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

How to change the Android app package name when assembling with Gradle?

Is it possible to change the package name of an Android application using Gradle?

I need to compile two copies of the same app, having a unique package name (so I can publish to the market twice).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As a simpler alternative to using product flavours as in Ethan's answer, you can also customise build types.

How to choose between the approaches:

  • If you need different package names to be able to have both debug and release apks installed on a device, then use the build type approach below, as Gradle plugin docs agree. In this case flavours are an overkill. (I think all projects should by default do this, as it will make life easier especially after you've published to the store and are developing new features.)
  • There are valid uses for product flavours, the typical example being an app with free and paid versions. In such case, check Ethan's answer and read the documentation too: Configuring Gradle Builds and Gradle Plugin User Guide.

(You can also combine the two approaches, which results in every build variant having distinct package name.)

Build type configuration

For debug build type, and all other non-release types, define applicationIdSuffix which will be added to the default package name. (Prior to Android Gradle plugin version 0.11 this setting was known as packageNameSuffix.)

android {
    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '-DEBUG'
        }

        beta {
            applicationIdSuffix '.beta'
            versionNameSuffix '-BETA'

            // NB: If you want to use the default debug key for a (non-debug) 
            // build type, you need to specify it:
            signingConfig signingConfigs.debug 
        }

        release {
            // signingConfig signingConfigs.release
            // runProguard true
            // ...
        }

    }
}

Above, debug and release are default build types whose some aspects are configured, while beta is a completely custom build type. To build the different types, use assembleDebug, assembleBeta, etc, as usual.

Similarly, you can use versionNameSuffix to override the default version name from AndroidManifest (which I find very useful!). E.g. "0.8" → "0.8-BETA", as configured above.

Resources:

Myself I've been using productFlavors so far for this exact purpose, but it seems build type customisation may be closer to my needs, plus it keeps the build config simpler.

Update (2016): I've since used this approach in all my projects, and I think it definitely is the way to go. I also got it included in Android Best Practices guide by Futurice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...