简体   繁体   English

适用于Android的每个环境配置

[英]Per Environment configuration for Android

What solution does Android offer for per environment configuration? Android为每个环境配置提供了哪些解决方案? In the web world, I am used to C# have config transforms and in Java web apps having a build that copies an appropriate .properties file into the WAR. 在Web世界中,我习惯于C#具有配置转换,并且在具有将适当的.properties文件复制到WAR中的构建的Java Web应用程序中。

What is the solution to Android? Android的解决方案是什么? Right now, I see alot of people just hard-coding static variables and changing them out when they want to build a production APK. 现在,我看到很多人只是硬编码静态变量并在他们想要构建生产APK时更改它们。

EDIT: Looking at similar questions, people suggest using SharedPreferences, but I do not understand this as it seems to be a Runtime object, and not a file. 编辑:看看类似的问题,人们建议使用SharedPreferences,但我不明白,因为它似乎是一个运行时对象,而不是一个文件。 I am not looking for storing these values on startup, but instead having the app build with a certain set of properties from a configuration file. 我不是在寻找在启动时存储这些值,而是让应用程序使用配置文件中的某组属性进行构建。

1.) I recommend to use gradle to set up environment settings. 1.)我建议使用gradle来设置环境设置。

There is great plugin for Android: http://tools.android.com/tech-docs/new-build-system/user-guide 有很棒的Android插件: http//tools.android.com/tech-docs/new-build-system/user-guide

2.) Use "productFlavors" for environment settings. 2.)使用“productFlavors”进行环境设置。

Have a look at this: http://tulipemoutarde.be/2013/10/06/gradle-build-variants-for-your-android-project.html 看看这个: http//tulipemoutarde.be/2013/10/06/gradle-build-variants-for-your-android-project.html

Sample Config would be: 示例配置将是:

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"


    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }

    }

    buildTypes {

        debug {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }

        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    productFlavors {
        production {
            packageName "com.sample.app"
        }

        staging {
            packageName "com.sample.app.staging"
        }

        develop {
            packageName "com.sample.app.develop"
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM