Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Android Builds For Different Environments

I'd like to be able to build and install multiple versions of my app (concurrently), targeting different environments such as Development, Staging and of course Production.

The package name in the AndroidManifest.xml seems to be the major hurdle here, as it is what uniquely identifies the app. I thought it would be possible to simply switch between com.mydomain.prod, com.mydomain.staging and com.mydomain.dev or some sort of similar convention but so far I've had no luck coming up with a package structure that works for this approach.

What strategy can I employ to do this with as little pain as possible?

like image 735
E-Madd Avatar asked Mar 06 '13 03:03

E-Madd


People also ask

What are build types in Android?

Once the new project is created, by default it consists of two build types/variants - debug, release. Debug is the build type that is used when we run the application from the IDE directly onto a device. A release is the build type that requires you to sign the APK.

How many types of Gradle build files do we have in Android?

Each module has its own build file, so every Android Studio project contains two kinds of Gradle build files.


1 Answers

With Gradle you can specify different "flavors" of your application. This is done in your build.gradle:

android {
    // …

    defaultConfig {
        // …
    }

    productFlavors {
        prod
            applicationId "com.mydomain.prod"
        }

        staging {
            applicationId "com.mydomain.staging"
        }

        dev {
            applicationId "com.mydomain.dev"
            versionCode 2
        }
    }
}

Each flavor properties are inherited from defaultConfig.

Now, in the src directory of your app, you can create directories with flavor-specific code/resources in it:

app/
|-- src/
    |-- main/
    |-- prod/
    |-- staging/
    |-- dev/

These links might be useful:

  • How to build customized Android application using Gradle
  • Gradle Build Variants for your android project
like image 187
Bruno Parmentier Avatar answered Sep 26 '22 07:09

Bruno Parmentier