代码之家  ›  专栏  ›  技术社区  ›  BRDroid

如何在React原生android中访问BuildConfig产品风格变量

  •  0
  • BRDroid  · 技术社区  · 6 年前

    嗨,我按照下面的链接创建产品口味 enter link description here

    我在flavors中设置了一个变量,但无法在代码中访问该变量。

     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

    1 回复  |  直到 6 年前
        1
  •  2
  •   BrantApps    6 年前

    我用过 react-native-build-config BuildConfig 如果您使用的是proguard,则初始化。启发这个图书馆的框架 react-native-config 也可以,但我在配置库以与cocoapod一起工作时遇到了问题>=0.56牛米。

    推荐文章