gpt4 book ai didi

android - 如何在构建 Android App Bundle 时指定环境变量

转载 作者:行者123 更新时间:2023-12-04 23:56:32 28 4
gpt4 key购买 nike

看起来 App Bundle 是首选的处理方式(优于 APK),所以我想尝试使用它。

我需要一些方法来指定 ENVFILE 参数来构建我的 React Native 应用程序。这在通过 ENVFILE=.env.production assembleRelease 生成 APK 时很容易完成,但我找不到 App Bundle 的等效项。

此外,在指定构建选项时,Android Studio App Bundle 向导似乎很少。

有什么想法吗?

最佳答案

您的问题将由这个 Node 包模块解决 -> react-native-config

我已经使用了这个模块并将我的 ENVFILE 变量传递给 string.xml,如下所示

<resources>
<string name="app_name">@string/APP_NAME</string>
<string name="facebook_app_id">@string/FB_APP_ID</string>
<string name="fb_login_protocol_scheme">@string/FB_APP_LOGIN_SCHEME</string>
<string name="map_api_key">@string/Google_map_key</string>

</resources>

在app.gradle文件中

  defaultConfig {
applicationId project.env.get("APP_ID")
versionCode project.env.get("ANDROID_VERSION_CODE").toInteger()
versionName project.env.get("ANDROID_VERSION_NAME")
multiDexEnabled true
missingDimensionStrategy "RNN.reactNativeVersion", "reactNative57"
ndk.abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
missingDimensionStrategy 'react-native-camera', 'general'
vectorDrawables.useSupportLibrary = true
renderscriptTargetApi rootProject.ext.renderscriptVersion
renderscriptSupportModeEnabled true
minSdkVersion 23
}

在 iOS 中使用 Objective-c

 NSString *apiUrl = [ReactNativeConfig envFor:@"Google_map_key"];

我的.env文件

Base_url=
InstaBase_url=https://api.instagram.com/oauth/
InstaGraph_url=https://graph.instagram.com/
Insta_Redirect_url=
Insta_AppID=
Insta_App_secret=
Google_Near_By_Restaurant=https://maps.googleapis.com/maps/api/place/nearbysearch/json?
Google_Restaurant_Photo=https://maps.googleapis.com/maps/api/place/photo?
Google_Place_auto_Base_url=https://maps.googleapis.com/maps/api/place/autocomplete/json?
Google_Place_details_Base_url=https://maps.googleapis.com/maps/api/place/details/json?
ANDROID_VERSION_CODE=6
ANDROID_VERSION_NAME=0.2
FB_APP_ID=
FB_APP_LOGIN_SCHEME=
Google_map_key=
APP_NAME=
appleAppId=
GooglePackageName=

我也有如下不同的 env 文件 enter image description here

我写了一个设置环境的脚本。下面是我的 debug.sh

echo $3
if [ "$2" != "android" ] && [ "$2" != "ios" ]; then
echo "Please enter the platform either iOS or Android"
exit
fi
if [ "$1" != "dev" ] && [ "$1" != "prod" ] && [ "$1" != "qa" ] && [ "$1" != "stage" ]; then
echo "Please enter the enviornment one of the following"
echo "1. prod"
echo "2. dev"
echo "3. stage"
echo "4. qa"
exit
fi

case $1 in
prod)
clear
if [ "$2" = "android" ]; then
ENVFILE=.env react-native run-android --variant=prodDebug
elif [ "$2" = "ios" ]; then
cp Plist_JSON/Info.plist ./ios/Fomoyolo/Info.plist
cp Plist_JSON/GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
ENVFILE=.env react-native run-ios "$3"
fi
break
;;
dev)
clear
if [ "$2" = "android" ]; then
ENVFILE=.env.dev react-native run-android --variant=devDebug --appIdSuffix 'dev'
elif [ "$2" = "ios" ]; then
cp Plist_JSON/Info_Dev.plist ./ios/Fomoyolo/Info.plist
cp Plist_JSON/Dev_GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
ENVFILE=.env.dev react-native run-ios "$3"
fi
break
;;
stage)
clear
if [ "$2" = "android" ]; then
ENVFILE=.env.staging react-native run-android --variant=stageDebug --appIdSuffix 'stage'
elif [ "$2" = "ios" ]; then
cp Plist_JSON/Info_Stage.plist ./ios/Fomoyolo/Info.plist
cp Plist_JSON/Stage_GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
ENVFILE=.env.staging react-native run-ios "$3"
fi
break
;;
esac

发布

echo $3
if [ "$2" != "android" ] && [ "$2" != "ios" ]; then
echo "Please enter the platform either iOS or Android"
exit
fi
if [ "$1" != "dev" ] && [ "$1" != "prod" ] && [ "$1" != "qa" ] && [ "$1" != "stage" ]; then
echo "Please enter the enviornment one of the following"
echo "1. prod"
echo "2. dev"
echo "3. stage"
echo "4. qa"
exit
fi

case $1 in
prod)
if [ "$2" = "android" ]; then
cp .env.prod .env
npm run android-prod
elif [ "$2" = "ios" ]; then
cp Plist_JSON/Info.plist ./ios/Fomoyolo/Info.plist
cp Plist_JSON/GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
cp .env.prod .env
rm -r ios/assets/node_modules/react-native/package.json
npm run ios-release
fi
break
;;
dev)
if [ "$2" = "android" ]; then
cp .env.dev .env
npm run android-dev
elif [ "$2" = "ios" ]; then
cp Plist_JSON/Info_Dev.plist ./ios/Fomoyolo/Info.plist
cp Plist_JSON/Dev_GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
cp .env.dev .env
rm -r ios/assets/node_modules/react-native/package.json
npm run ios-release
fi
break
;;
stage)

if [ "$2" = "android" ]; then
cp .env.staging .env
npm run android-stage
elif [ "$2" = "ios" ]; then
cp Plist_JSON/Info_Stage.plist ./ios/Fomoyolo/Info.plist
cp Plist_JSON/Stage_GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
cp .env.staging .env
rm -r ios/assets/node_modules/react-native/package.json
npm run ios-release
fi
break
;;
esac
{
"name": "Fomoyolo",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint .",
"fullSetup": "npm install && react-native link && cd ios/ && pod install && cd ..",
"postinstall": "node ./android-release-gradle-fix.js",
"android-dev": "ENVFILE=.env.dev && react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ && cd ./android && ./gradlew app:assembleDevRelease",
"android-stage": "ENVFILE=.env.staging && react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ && cd ./android && ./gradlew app:assembleStageRelease",
"android-prod": "ENVFILE=.env && react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ && cd ./android && ./gradlew app:assembleRelease"
},
}

关于android - 如何在构建 Android App Bundle 时指定环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61052187/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com