How do you manage multiple environments while developing Android apps?

With android studio and gradle its simple now.

inside your app build.gradle edit signing configs

signingConfigs {
    debug {
        storeFile file("debug.keystore")
        storePassword "..."
        keyAlias "..."
        keyPassword "..."
    }

    prod {
        storeFile file("prod.keystore")
        storePassword "..."
        keyAlias "..."
        keyPassword "..."
    }

    dev {
        storeFile file("dev.keystore")
        storePassword "..."
        keyAlias "..."
        keyPassword "..."
    }
}

add buildTypes

buildTypes {

    debug {
        buildConfigField 'String', 'BASE_URL', '"http://127.0.0.1:8080/"'
        ......
        signingConfig signingConfigs.debug
    }

    prod {

        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        buildConfigField 'String', 'BASE_URL', '"http://prod.example.com"'
        ......
        signingConfig signingConfigs.prod
    }


    dev {
        buildConfigField 'String', 'BASE_URL', '"http://dev.example.com"'
        ......
        signingConfig signingConfigs.dev
    }
}

In your code take base url configured in gradle file by this code.

public final static String BASE_URL = BuildConfig.BASE_URL;

You can also put different KEY or whatever which is build type specific in gradle file and in code it will take according to the build type you are running.

Its even possible to have different package name.

productFlavors {
    my_prod {
        applicationId "com.example.packtwo"
    }
    my_dev {
        applicationId "com.example.packone"
    }
}

In recent gradle config, there are some updates in specifying package name. You have to add flavourDimensions if using productFlavours. See below code with added flavourDimensions

flavorDimensions "pack"

productFlavors {
    flavor_dev {
        applicationId 'com.example.packtwo'
        dimension "pack"
    }

    flavor_prod {
        applicationId 'com.example.packone'
        dimension "pack"
    }
}

This will give you more details about product flavours and dimensions

https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html

Check for more possibilities…

But if you are using different flavors you might have to deal with Manifest merging and all.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)