- 首先下載@types/react-native-dotenv和react-native-dotenv喇嘱。
在根目錄的.babelrc文件中添加以下代碼:
{
"plugins": [
[
"module:react-native-dotenv",
{
"envName": "ENVFILE", //這里需要設(shè)置為上面package.json對(duì)應(yīng)的
"moduleName": "react-native-dotenv",
"path": ".env",
"safe": false,
"allowUndefined": true
}
]
]
}
- 在src/common/types目錄下創(chuàng)建env.d.ts文件茉贡,內(nèi)容如下:
declare module 'react-native-dotenv' {
export const BASE_URL: string;
}
-
在app/build.gradle 中添加以下代碼:
buildTypes {
debug {
signingConfig signingConfigs.debug
applicationIdSuffix ".dev"
manifestPlaceholders = [
app_name : "@string/app_name_debug",
app_icon : "@mipmap/ic_launcher",
// //app_icon: "@mipmap/ic_launcher_debug",
app_round_icon: "@mipmap/ic_launcher_round"
]
}
release {
signingConfig signingConfigs.release
minifyEnabled enableProguardInReleaseBuilds
// 設(shè)置manifest占位符,為了顯示不同的軟件名
manifestPlaceholders = [
app_name : "@string/app_name",
app_icon : "@mipmap/ic_launcher",
app_round_icon: "@mipmap/ic_launcher_round"
]
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
releaseStage.initWith(release)
releaseStage {
//為測(cè)試版本的包名添加.beta
applicationIdSuffix ".stage"
// 設(shè)置manifest占位符者铜,為了顯示不同的軟件名
manifestPlaceholders = [
app_name : "@string/app_name_stage",
app_icon : "@mipmap/ic_launcher",
//app_icon: "@mipmap/ic_launcher_stage",
app_round_icon: "@mipmap/ic_launcher_round"
]
matchingFallbacks = ['release']
}
releaseTest.initWith(release)
releaseTest {
//為測(cè)試版本的包名添加.beta
applicationIdSuffix ".test"
// 設(shè)置manifest占位符腔丧,為了顯示不同的軟件名
manifestPlaceholders = [
app_name : "@string/app_name_test",
app_icon : "@mipmap/ic_launcher",
// //app_icon: "@mipmap/ic_launcher_test",
app_round_icon: "@mipmap/ic_launcher_round"
]
matchingFallbacks = ['release']
}
}
test和stage全部以release為基準(zhǔn)進(jìn)行測(cè)試放椰,同時(shí)設(shè)置動(dòng)態(tài)app名和icon
-
在androidmanifest.xml中 改寫(xiě)label和icon:
在app/build.gralde底部添加一行代碼:
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
- 修改tsconfig.json,增加一行:
"typeRoots": ["./common/interface"]
-
簡(jiǎn)化命令行腳本:
"build-android-prod": "cd android && ENVFILE=prod ./gradlew assembleRelease",
"build-android-test": "cd android && ENVFILE=test ./gradlew assembleReleaseTest",
"build-android-stage": "cd android && ENVFILE=stage ./gradlew assembleReleaseStage",
"start": "ENVFILE=dev react-native start",
"android": "react-native run-android",
"ios": "react-native run-ios",
"lint": "eslint .",
"test": "jest"
-
根目錄創(chuàng)建shell文件
使用