0

嗨,我按照以下链接创建产品口味 在此处输入链接描述

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

构建.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

4

1 回答 1

2

我用过react-native-build-config这个。BuildConfig效果很好,但如果您使用 proguard ,请记住为生成的类设置保留规则。启发这个库的框架react-native-config也可以工作,但是当目标 >=0.56 RN 时,我在配置库以使用 cocoapods 时遇到问题。

于 2018-09-14T13:22:08.760 回答