如何在Android Studio中定义变体以开发和发布React Native应用程序的应用程序

问题描述

我使用react-native-config在生产和开发环境之间进行切换。

我可以根据IOS的方案切换环境dev / prod(捆绑程序标识符,appIcon,splashScreeen,appName等)。

对于开发人员:

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

和产品:

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

如果我想使用它类似于android:

对于开发人员:

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

和产品:

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

我想在一个项目下拥有两种类型的appdev和prod,它们具有applicationId,例如: com.my.app 用于生产和 com.my.app.dev 为了发展

我想切换环境dev / prod(捆绑程序标识符,appIcon,splashScreeen,appName,..)

但是我每次都使用相同的appIcon和appName构建相同的应用程序。

在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
            }

        }
    }
}

我的目标是使用application_id com.my.app和开发com.my.app.dev构建用于生产的应用程序。

谢谢大家的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)