美文网首页
react-native-config 基本使用windows

react-native-config 基本使用windows

作者: who_are_you_ | 来源:发表于2025-09-15 14:27 被阅读0次

版本配置

    "react": "19.1.0",
    "react-native": "npm:react-native-tvos@0.81.0-0",
    "react-native-config": "1.4.6",
第一步安装自动对号入座哈:命令行
yarn add react-native-config // yarn
// npm install react-native-config --save-dev // npm

下载之后需要连接,新版本不需要连接,旧版本需要连接,这里使用的新版本(对于 React Native 0.60 或更高版本,可以使用自动链接,旧版本手动连接,看官网哈

第二步Android 的额外步骤

使用 RN-Integrate

自动应用额外步骤:命令行

npx react-native-integrate react-native-config
第三步

虽然是自动的,但是遇到了报错,下边是报错解决
您还需要手动将插件应用到您的应用程序,来自android/app/build.gradle:
加到最后一行,不要放到任何{}里边

// 2nd line, add a new apply:
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
第四步根目录下,新建对应.env文件

.env.development

API_BASE_URL=https://dev-api.example.com
ENV_NAME=dev
LOGGING=true

.env.test

API_URL=https://test-api.example.com
ENV_NAME=test
LOGGING=true

.env.production

API_BASE_URL=https://prod-api.example.com
ENV_NAME=prod
LOGGING=false
第五步配置

package.json

scripts {
   "android": "SET ENVFILE=.env.development && react-native run-android -- --variant=development",
   "android:dev": "SET ENVFILE=.env.development && react-native run-android -- --variant=development",
   "android:test": "SET ENVFILE=.env.test && react-native run-android -- --variant=test",
   "android:prod": "SET ENVFILE=.env.production && react-native run-android -- --variant=production",
   "android:build": "SET ENVFILE=.env.production && cd ./android && gradlew.bat assembleStagingRelease && cd ..",
}

相关文章

网友评论

      本文标题:react-native-config 基本使用windows

      本文链接:https://www.haomeiwen.com/subject/uectzjtx.html