简体   繁体   中英

Android configuration value in CI

I am new to android but already thinking if there is a way to include some important configurations for the app in continuous integration . We use visual studio online for build and release . Example . Currently we have Constants.java file which stores some imp configuration values like server URL which wil be different for each environment app is deployed on for example dev,test uat production , so currently that code needs to be manually checked before it goes in to vso for build and release . . Is it possible to somehow configure it in continuous integration of environment and pick from there so that manual check is not required ..

See this:

Look this function readApiKey() it reads the keys in from Environment or from a .properties file . Then look at this line:

buildConfigField("String", "API_PUBLIC_KEY", readApiKey(KeyType.PUBLIC_KEY)) it loads the string resource with the value.

If you have different builds with different values, that's where flavours comes in. You can read it up here.

apply plugin: 'com.android.application'
apply from: '../codequality/quality.gradle'

/*
 * Read API key from CI server or from keys.properties file
 */
enum KeyType {
    PUBLIC_KEY,
    PRIVATE_KEY
}

def readApiKey(KeyType type) {
    String apiKey = ""
    if (System.getenv("CIENV")) {
        if (type == KeyType.PUBLIC_KEY) {
            apiKey = System.getenv("PB_KEY")
        } else apiKey = System.getenv("PR_KEY")
    } else {
        Properties props = new Properties()
        try {
            props.load(new FileInputStream(new File(getRootDir().getAbsolutePath() + "/keys.properties")))
            if (type == KeyType.PUBLIC_KEY) {
                apiKey = props.getProperty("api_pb_key")
            } else apiKey = props.getProperty("api_pr_key")
        } catch (FileNotFoundException fnfe) {
            println("Please create a keys.properties file in the root directory of the project")
        }
    }
    return "\"$apiKey\"";
}

println 'PUBLIC KEY Log' + readApiKey(KeyType.PUBLIC_KEY)
println 'Private Key Log' + readApiKey(KeyType.PRIVATE_KEY)

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    dataBinding {
        enabled true
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }

    defaultConfig {
        applicationId "co.tonespy.dummarvel"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        // Setting up picked up data to String resource
        buildConfigField("String", "API_PUBLIC_KEY", readApiKey(KeyType.PUBLIC_KEY))
        buildConfigField("String", "API_PRIVATE_KEY", readApiKey(KeyType.PRIVATE_KEY))
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // support dependencies
    ....

    // test dependencies
    testCompile "junit:junit:$jUnitVersion"
    androidTestCompile("com.android.support.test.espresso:espresso-core:$espressoCoreVersion", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile "org.mockito:mockito-core:$mockitoVersion"
    compile "com.android.support:support-v4:$supportLibraryVersion"
}

You can just use Replace Tokens step/task to replace the value per to each environment.

  1. Install Replace Tokens extension
  2. Add Replace Tokens task to each environment of your release definition (Target files **\\ Constants.java)
  3. Add environment variable (eg server, same variable name for each environment) for each environment of you release definition

在此处输入图片说明

  1. Modify Constants.java file, replace necessary value to #{server}# and check in changes (The developers can modify this file with specific value and do not check in changes if they need to debug/run app in local machine)
  2. After that the #{server}# will be replaced with the value of server environment

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