Java – every environment configuration for Android

What solutions does Android provide for each environment configuration? In the web world, I'm used to c# having configuration transformations and putting them in place Copy the properties file to the built Java Web Application in war

What is the Android solution? Now, I see a lot of people just hard code static variables and change them when they want to build a production APK

Editor: look at similar problems. People suggest using SharedPreferences, but I don't understand because it seems to be a runtime object, not a file I'm not looking to store these values at startup, but to let the application build using a set of properties in the configuration file

Solution

1.) I recommend using gradle to set the environment settings

Android has great plug-ins: http://tools.android.com/tech-docs/new-build-system/user-guide

2.) use "productflavors" to set the environment

Look at this: http://tulipemoutarde.be/2013/10/06/gradle-build-variants-for-your-android-project.html

An example configuration 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 content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>