在 android 中反应本机暂存

react native staging in android

我正尝试在 android 中为我的 react-native 项目获取风味或构建变体 运行。

通过计划,我在 iOS 上得到了相同的 运行,但 Android 不想遵循。我想我做错了什么。

到目前为止我采取的步骤(全部在一个新项目中进行测试)

  1. npm 安装 react-native-config
  2. 创建了 3 个文件 .env、.env.staging 和 .env.production
  3. 将以下内容添加到 android/app/build。gradle:
 project.ext.envConfigFiles = [
  staging: ".env.staging",
  production: ".env.production",
]

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
  1. 将口味添加到同一个文件中(后缀被注释掉以不更改名称 -> 希望这不是问题所在):
flavorDimensions "appType"
        productFlavors {
        staging {
            dimension "appType"
            applicationIdSuffix ".staging"
            // resValue "string", "app_name", "Config Demo-Staging"
        }
        production {
            dimension "appType"
            applicationIdSuffix ".production"
            // resValue "string", "app_name", "Config Demo"
        }
        }
  1. 在 package.json 中添加了以下脚本:
"scripts": {
    "androidStagingDebug": "react-native run-android --variant=stagingDebug",
    "androidProductionDebug": "react-native run-android --variant=productionDebug",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },

现在我想从 Android Studio 启动应用程序并通过以下方式检查:

import config from 'react-native-config';

并分别在后面的函数中:

console.log(config)

我在选择构建变体时是否读取了正确的文件。

不幸的是,它要么不加载任何内容,要么只进行调试。任何的想法?! 此外,在某些变体中,该应用似乎无法连接到地铁..

您的 build.gradle 文件似乎没问题。

不过我的略有不同:

project.ext.envConfigFiles = [
    prod: "./environment/.prod",
    beta: "./environment/.beta",
    alpha: "./environment/.alpha",
]

然后:

flavorDimensions "default"

  productFlavors {
    prod {
        applicationIdSuffix ".prod"
    }
    beta {
        applicationIdSuffix ".beta"
    }
    alpha {
        applicationIdSuffix ".alpha"
    }
}

就像我之前在评论中所说的那样,我没有使用任何脚本来启动应用程序,我只是 select Android Studio 中的构建变体。

这是我使用的资源:

https://medium.com/swlh/setting-up-multiple-environments-on-react-native-for-ios-and-android-c43f3128754f

这是一个示例项目:

https://github.com/therealemjy/react-native-tuto-multiple-environments/tree/result

希望对您有所帮助。