How to define variants in Android Studio for development and release application for React Native application

946 views Asked by At

I use react-native-config for changing between production and development environments.

I can switch environments dev/prod (bundler identifier, appIcon, splashScreeen, appName, ..) base on the scheme for IOS

for dev:

ENVFILE=.env npx react-native run-ios --scheme 'testConfigAppDev'

and for prod:

ENVFILE=.env.production npx react-native run-ios --scheme 'testConfigApp'

If I want to use it similar to android:

for dev:

ENVFILE=.env npx react-native run-android --variant debug

and for prod:

ENVFILE=.env.production npx react-native run-ios --scheme 'testConfigApp'

I want to have under one project two types of app dev and prod with applicationId e.g: com.my.app for production and com.my.app.dev for development

I want to switch environments dev/prod (bundler identifier, appIcon, splashScreeen, appName, ..)

But I every time build the same application with the same appIcon and appName.

in Android/app/build.gradle:

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        //applicationId project.env.get("APP_ID")
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        resValue "string", "app_name", project.env.get("APP_DISPLAY_NAME")
        resValue "string", "build_config_package", project.env.get("APP_ID")
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
            debuggable true
            applicationIdSuffix ".dev"
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }

        }
    }
}

My goal is to build an application for production with application_id com.my.app and development com.my.app.dev.

Thank guys for your help.

0

There are 0 answers