Requirements:

In product development, it is often necessary to release versions with different server addresses for each version. Such as

  • The development server uses 192.168.1.232 server,
  • The test server uses server 192.168.1.245,
  • After the official launch, the server address is xxxx.com. In cooperation with Git development, there is also branch management, which is commonly used:
  • dev
  • test
  • Master Our initial development process was:

1. At the beginning of a project, developers write code and check in code to the dev branch. The developer uses the server address of the development server. 2. When starting a test, the developer lead merges code into the Test branch. The test group owner will modify the source file of the server address in the code, change it to the address of the test server, package it and distribute it to the test group members to start the test. 3. After the test, the head of the test group merged the code to the master branch, changed the server address to the online server address, and tested, and then officially released. As we saw in the above steps, the server address needs to be changed multiple times, which requires some technical ability and is considered easy to do, such as writing a nonexistent service address.

Improvement:

Introducing Jenkins service automatic packaging, Jenkins is an excellent automated build tool that provides a great experience for packaging. This allows testers to do the packaging themselves, when needed, without much more. Of course, this requires us to configure the build script. After using Android Studio, you need to understand Gradle scripts.

Key words:

BuildConfigField, BuildConfig

Ideas:

In gradle scripts, we use buildConfigField to declare a field. After successful compilation, static constants are generated for this field. We can use this constant in our code. Gradle uses buildTypes to configure different buildTypes (such as test, development, and live), each of which specifies a different field value. After a successful build, Gradle creates different APK packages for each type of environment, such as test, development, and live apK packages.

Implementation method:

1. Modify build.gradle to configure buildConfigField under buildTypes

buildTypes { release { minifyEnabled false buildConfigField("String", "API_HOST", "\"http://www.jinlinbao.com\"") signingConfig signingConfigs.zyfkey } debug245 { debuggable true minifyEnabled false BuildConfigField ("String", "API_HOST", "\"http://192.168.1.245\"") signingConfig signingConfigs.zyfkey}}Copy the code

Notice the underlined font above.

This method takes three parameters: the field variable type, the variable name, and the value. Like this one right here

“String”, “API_HOST”, “”http://192.168.1.245″” // Make sure the right slash is followed by the escape semicolon to generate: Public static final String API_HOST = “http://192.168.1.245”;

2. Modify build.gradle to configure the buildConfigField field under defaultConfig. This is to make it easier to compile the code and make API_HOST available in each environment.

API_HOST buildConfigField (” String “, “, “” “” http://192.168.1.232

3. Start build, and when finished, you can use it in your code:

String str = BuildConfig.API_HOST;

Full script

Here is the complete configuration script:

apply plugin: 'com.android.application' android { signingConfigs { zyfkey { keyAlias '-' keyPassword '-' storeFile file('/Users/zhangyunfei/git/vocabulary/zhangyf.keystore') storePassword '-' } } compileSdkVersion 19 buildToolsVersion "22.0.1" defaultConfig {applicationId "vir56k.vecabulary" minSdkVersion 14 targetSdkVersion 19 versionCode 1 "1.0" versionName buildConfigField (" String ", "API_HOST", "\"http://192.168.1.232\"")} buildTypes {release {minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' buildConfigField("String", "API_HOST", "\"http://www.jinlinbao.com\"") signingConfig signingConfigs.zyfkey } debug245 { debuggable true minifyEnabled false buildConfigField("String", "API_HOST", "\"http://192.168.1.245\"") signingConfig SigningConfigs.zyfkey}} productFlavors {}} dependencies {compile fileTree(dir: 'libs', include: ['*.jar']) }Copy the code

Here is a code demo:

package vir56k.vecabulary; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String str = BuildConfig.API_HOST; TextView txt1 = (TextView) findViewById(R.id.txt1); txt1.setText(str); }}Copy the code