簡體   English   中英

React Native - Android 中的 ENV 配置問題

[英]React Native - Issue with ENV configurations in Android

我已經使用react-native-config package 配置了 ENV。 我在app/build.gradle中定義了 3 個不同的 ENV

這定義如下 -

project.ext.envConfigFiles = [
    debug: ".env.dev",
    stagingRelease: ".env.staging",
    prodRelease: ".env.prod",
]

現在,當我運行npx react-native run-android --variant=prodRelease 它因錯誤而中斷- Task 'installProdRelease' not found in project ':app'.

我怎樣才能讓它工作,以便我可以在應用程序中運行不同的環境?

傳遞給--variant選項的值基本上是產品風味和構建類型的組合。 默認情況下,定義了 2 種構建類型—— debugrelease 要添加諸如developmentstagingproduction之類的風格,請在android/app/build.gradle buildTypes buildTypes 下面定義它們 -

    buildTypes {
        .....
    }

    flavorDimensions "default"
    productFlavors {
        production {}
        staging {}
        development {}
    }

現在有了這樣的配置 -

project.ext.envConfigFiles = [
    developmentdebug: ".env.development",
    developmentrelease: ".env.development",
    stagingdebug: ".env.staging",
    stagingrelease: ".env.staging",
    productiondebug: ".env.production",
    productionrelease: ".env.production",
]

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

你現在可以隨心所欲地建造——

npx react-native run-android --variant=stagingdebug
npx react-native run-android --variant=productionrelease

您可以在此處閱讀有關 Android 構建類型和風格的更多信息。
https://developer.android.com/studio/build/build-variants

對於 iOS 中的類似配置,請遵循此優秀教程。
https://www.bigbinary.com/books/learn-react-native/handling-environment-specific-configurations-in-react-native

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM