Browse Source

优化服务器地址配置

zk 1 year ago
parent
commit
dd5111b83b

+ 32 - 4
app/build.gradle

@@ -4,6 +4,22 @@ plugins {
     id 'stringfog'
 }
 
+def LOCAL = "local", TEST = "test", PROD = "prod"
+
+def getServerHost(String env) {
+    switch (env) {
+        case "local":
+            return "$local_server_host";
+        case "test":
+            return "$test_server_host";
+        case "prod":
+            return "$prod_server_host";
+        default:
+            return "$prod_server_host";
+    }
+}
+
+
 android {
     namespace 'com.datarecovery.master'
     compileSdk rootProject.compileSdkVersion
@@ -36,14 +52,21 @@ android {
         }
     }
 
+
+    def env_release = PROD
+    def env_debug = LOCAL
+
     buildTypes {
         release {
             minifyEnabled true
             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
             signingConfig signingConfigs.release
 
-            buildConfigField "boolean", "isLocalNetwork", "false"
-            buildConfigField "String", "HOST", "\"$prod_host\""
+            buildConfigField "String", "ENV", "\"$env_release\""
+            buildConfigField "String", "HOST", "\"${getServerHost(env_release)}\""
+            buildConfigField "String", "LOCAL", "\"$LOCAL\""
+            buildConfigField "String", "TEST", "\"$TEST\""
+            buildConfigField "String", "PROD", "\"$PROD\""
             buildConfigField "String", "WECHAT_APP_ID", "\"$wechat_app_id\""
             buildConfigField "String", "WECHAT_KF_ID", "\"$wechat_kf_id\""
             buildConfigField "String", "WEWORK_CROP_ID", "\"$wework_crop_id\""
@@ -58,8 +81,11 @@ android {
             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
             signingConfig signingConfigs.debug
 
-            buildConfigField "boolean", "isLocalNetwork", "true"
-            buildConfigField "String", "HOST", "\"$test_host\""
+            buildConfigField "String", "ENV", "\"$env_debug\""
+            buildConfigField "String", "HOST", "\"${getServerHost(env_debug)}\""
+            buildConfigField "String", "LOCAL", "\"$LOCAL\""
+            buildConfigField "String", "TEST", "\"$TEST\""
+            buildConfigField "String", "PROD", "\"$PROD\""
             buildConfigField "String", "WECHAT_APP_ID", "\"$wechat_app_id\""
             buildConfigField "String", "WECHAT_KF_ID", "\"$wechat_kf_id\""
             buildConfigField "String", "WEWORK_CROP_ID", "\"$wework_crop_id\""
@@ -81,10 +107,12 @@ android {
 
     applicationVariants.configureEach { variant ->
         def date = new Date().format("YYMMddHHmmss")
+        def server = variant.buildType.name == "release" ? env_release : env_debug
         variant.outputs.configureEach {
             def fileName = "${defaultConfig.applicationId}" +
                     "-v${defaultConfig.versionName}" +
                     "-${variant.buildType.name}" +
+                    "-${server}" +
                     "-${date}" +
                     ".apk"
             outputFileName = fileName

+ 18 - 0
app/src/main/java/com/datarecovery/master/App.java

@@ -10,6 +10,9 @@ import com.datarecovery.master.sdk.bugly.BuglyHelper;
 import com.datarecovery.master.sdk.gravity.GravityHelper;
 import com.datarecovery.master.sdk.qiyu.QiYuHelper;
 import com.datarecovery.master.sdk.umeng.UmengHelper;
+import com.datarecovery.master.utils.ToastUtil;
+
+import java.util.Objects;
 
 import dagger.hilt.android.HiltAndroidApp;
 
@@ -34,6 +37,21 @@ public class App extends BaseApplication {
 
 
     @Override
+    public void onCreate() {
+        super.onCreate();
+        checkServerRuntime();
+    }
+
+    private void checkServerRuntime() {
+        if (BuildConfig.DEBUG) {
+            return;
+        }
+        if (!Objects.equals(BuildConfig.ENV, BuildConfig.PROD)) {
+            ToastUtil.show("当前环境不是正式环境!", ToastUtil.LENGTH_LONG);
+        }
+    }
+
+    @Override
     protected boolean isDebug() {
         return BuildConfig.DEBUG;
     }

+ 1 - 5
app/src/main/java/com/datarecovery/master/data/consts/Constants.java

@@ -5,11 +5,7 @@ import com.datarecovery.master.BuildConfig;
 public class Constants {
 
 
-    //    private static final String Atmob_Server_Base_URL_LOCAL = "http://192.168.10.68:8880";
-    private static final String Atmob_Server_Base_URL_LOCAL = "http://192.168.10.171:8880";
-    public static final String Atmob_Server_Base_URL_REMOTE = BuildConfig.HOST;
-    public static final String Atmob_Server_Base_URL = BuildConfig.isLocalNetwork ? Atmob_Server_Base_URL_LOCAL : Atmob_Server_Base_URL_REMOTE;
-
+    public static final String Atmob_Server_Base_URL = BuildConfig.HOST;
 
     public static final String App_DefaultChannel = "Android";
     public static final int App_DefaultAppId = 0;

+ 3 - 2
gradle.properties

@@ -21,8 +21,9 @@ android.useAndroidX=true
 android.nonTransitiveRClass=true
 android.enableJetifier=true
 android.injected.testOnly=false
-test_host=https://central-test.atmob.com
-prod_host=http://project-api.atmob.com
+prod_server_host=http://project-api.atmob.com
+test_server_host=https://central-test.atmob.com
+local_server_host=http://192.168.10.171:8880
 wechat_app_id=wx1f761300f09cfd14
 wework_crop_id=wwda29771d597510a9
 wechat_kf_id=kfcb5b96d3fb25aca03