Home » Android » Different Android Builds For Different Environments

Different Android Builds For Different Environments

Posted by: admin June 16, 2020 Leave a comment

Questions:

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?

How to&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:

Answer:

I suggest you use the ‘ant’ build tool specificaly the ‘regexp’ task which can be used in order to replace text in certain files (i.e replace your app’s package name in the manifest file according to the proper build target). You can also define differnet properties file for each build target (e.g ‘build_development.properties’ to hold all parameters relevant for building your product for development env.)… hope this is helpful..