简体   繁体   中英

Per Environment configuration for Android

What solution does Android offer for per environment configuration? 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.

What is the solution to 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.

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

There is great plugin for Android: http://tools.android.com/tech-docs/new-build-system/user-guide

2.) Use "productFlavors" for environment settings.

Have a look at this: 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"
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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