About the switch between the official version and the test version of Android app

Some time ago, there was a problem with the app. The server link on the app was actually the link to the test server, which embarrassed me.
Because every formal and test server switch is manually replaced, this problem arises. Then I wonder if I can switch formal and test server links according to release and debug. Finally, the research results are recorded here.

 

The formal and tested code is implemented in build.gradle.

 

def Base_Url = "\"http://Official server / \ "" / / slash must be added for java code to recognize that this is a String type
def CBase_Url = "\"http://Test server / \ ""
def Base_UM = "baidu"
android {
//Some codes are omitted in the middle
  buildTypes {
        release {
            //confusion
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 
            'proguard-rules.pro'
            //Key direct run takes effect
            signingConfig signingConfigs.release
            //Formal, where "base" is the field referenced in Java
            buildConfigField "String", "BASE_URL",  Base_Url
            //Source setting of the alliance, if you receive the Alliance
            manifestPlaceholders = [umeng_app_key: Base_UM]
          
        }

        debug {
            //confusion
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 
            'proguard-rules.pro'
            //Key direct run takes effect
            signingConfig signingConfigs.release
            //test
            buildConfigField "String", "BASE_URL", CBase_Url
            manifestPlaceholders = [umeng_app_key: "ceshi"]
            
        }
    }

 //Automatically generate apk file of release release with specified name
    android.applicationVariants.all { variant ->
        def appName
        if(variant.buildType.name.equals('release')){
            //In the formal mode, judge whether it is a formal server according to the server link
            if(Base_Url.contains("api")){
                if(Base_UM.equals("guanwang")){
                    appName = "NameV"+defaultConfig.versionName
                }else{
                    appName = "Name-"+Base_UM+"V"+defaultConfig.versionName
                }
            }else if(Base_Url.contains("sixsix")){
                appName = "test_V"+defaultConfig.versionName
            }else {
                appName = "Unknown_V"+defaultConfig.versionName
            }
        } else {
            appName = "test"
        }
//        if(variant.buildType.name.equals('release')){
//        } else {
//        }
        variant.outputs.all { output ->
            outputFileName = "${appName}.apk"
        }
    }
}

To reference the base? URL in java:
String BASE_URL = BuildConfig.BASE_URL;

Reference the alliance source in Android manifest.xml

<meta-data android:value="${umeng_app_key}" android:name="UMENG_CHANNEL"/>

The last automatic apk generation is for convenience. Because you need to generate apks from different sources for uploading, in order not to change the name every time you generate it, you can generate the name directly.

Tags: Android Java Gradle xml

Posted on Thu, 09 Jan 2020 14:47:56 -0500 by itsinmyhead