如何在React native android中访问BuildConfig产品flavor变量

问题描述 投票:0回答:1

您好,我按照以下链接创建产品口味enter link description here

我在flavor中设置了一个变量,我无法在代码中访问它。

的build.gradle。

 productFlavors {
    production {
        minSdkVersion rootProject.ext.minSdkVersion
        applicationId 'com.xxx.production'
        targetSdkVersion rootProject.ext.targetSdkVersion
        resValue "string", "build_config_package", "com.myapp"
        buildConfigField "boolean", "MOCK_VERSION", "false"
        resValue "string", "MOCK_VERSION_ABCD", "false"
    }
    staging {
        minSdkVersion rootProject.ext.minSdkVersion
        applicationId 'com.xxx.staging'
        targetSdkVersion rootProject.ext.targetSdkVersion
        resValue "string", "build_config_package", "com.myapp"
        buildConfigField "boolean", "MOCK_VERSION", "false"
        resValue "string", "MOCK_VERSION_ABCD", "false"
    }
    t3st {
        minSdkVersion rootProject.ext.minSdkVersion
        applicationId 'com.xxx.test'
        targetSdkVersion rootProject.ext.targetSdkVersion
        resValue "string", "build_config_package", "com.myapp"
        buildConfigField "boolean", "MOCK_VERSION", "true"
        resValue "string", "MOCK_VERSION_ABCD", "true"
    }
}

我试图在我的Login.js文件中访问它

import { BuildCofig } from 'react-native';

loginEndpointDecider = () => {
 if(BuildCofig.MOCK_VERSION){
   alert('MOCK_VERSION - true');
 }else{
   alert('MOCK_VERSION - false');
 }
}

运行应用程序

react-native run-android --variant=stagingDebug

我想访问buildConfigField MOCK_VERSION

你能否建议如何让这个工作。

谢谢R

react-native android-gradle react-native-android android-productflavors android-build-flavors
1个回答
1
投票

我为此使用过react-native-build-config。效果很好但是如果你使用的是proguard,请记得为生成的BuildConfig类设置keep规则。启发这个库react-native-config的框架也可以工作,但是在定位> = 0.56 RN时,我在配置库以使用cocoapods时遇到了问题。

© www.soinside.com 2019 - 2024. All rights reserved.