热修复框架

chengzhenyu 8 jaren geleden
bovenliggende
commit
425dfdf3ba

+ 2 - 1
app/proguard-project.txt

@@ -130,4 +130,5 @@ public static final int *;
130 130
 -keepclassmembers class * {
131 131
    public java.lang.Object accessSuper(java.lang.Object,int,java.lang.Object[]);
132 132
 }
133
--keep class com.android.hotpatch.**{*;}
133
+-keep class com.android.hotpatch.**{*;}
134
+-keep class ai.pai.ai.patch.**{*;}

+ 1 - 1
app/src/main/java/ai/pai/client/app/PaiAiApplication.java

@@ -33,7 +33,7 @@ public class PaiAiApplication extends Application {
33 33
         PlatformConfig.setSinaWeibo("2730190333","ff16591583c7bcf4a0d781eae316635a");
34 34
         PhotoLoader.getInstance(this);
35 35
         checkAppDir();
36
-//        new PatchManager().patch(this, UrlContainer.PATCH_CONFIG_URL,Constants.APP_PATCH_DIR+File.separator+"patch.dex","");
36
+        new PatchManager().patch(this, UrlContainer.PATCH_CONFIG_URL,Constants.APP_PATCH_DIR+File.separator+"patch.dex","");
37 37
     }
38 38
 
39 39
     private void checkAppDir(){

+ 4 - 4
app/src/main/java/ai/pai/client/db/Preferences.java

@@ -133,12 +133,12 @@ public class Preferences {
133 133
         mPrefs.edit().putBoolean("isPermissionGranted",isPermissionGranted).commit();
134 134
     }
135 135
 
136
-    public void setPatchMD5(String md5){
137
-        mPrefs.edit().putString("md5",md5).commit();
136
+    public void setPatchUrl(String patchUrl){
137
+        mPrefs.edit().putString("patchUrl",patchUrl).commit();
138 138
     }
139 139
 
140
-    public String getPatchMD5(){
141
-        return mPrefs.getString("md5",NullStr);
140
+    public String getPatchUrl(){
141
+        return mPrefs.getString("patchUrl",NullStr);
142 142
     }
143 143
 
144 144
     public void clearPrefs(){

+ 58 - 0
app/src/main/java/ai/pai/client/patch/AboutUsActivity$Override.java

@@ -0,0 +1,58 @@
1
+package ai.pai.client.patch;
2
+
3
+import android.os.Bundle;
4
+import android.view.View;
5
+import android.widget.TextView;
6
+
7
+import com.android.hotpatch.framework.ChangeQuickRedirect;
8
+import com.android.hotpatch.utils.PatchUtils;
9
+
10
+import ai.pai.client.R;
11
+import ai.pai.client.activity.AboutUsActivity;
12
+import ai.pai.client.utils.SystemUtils;
13
+
14
+
15
+public class AboutUsActivity$Override implements ChangeQuickRedirect {
16
+    @Override
17
+    public boolean isSupport(String s, Object[] objects) {
18
+        String methodName = s.split(":")[1];
19
+        if(methodName.equals("onCreate")){
20
+            return true;
21
+        }
22
+        return false;
23
+    }
24
+
25
+    @Override
26
+    public Object accessDispatch(String s, Object[] objects) {
27
+        String methodName = s.split(":")[1];
28
+        if(methodName.equals("onCreate")){
29
+            onCreate((AboutUsActivity)objects[objects.length-1],(Bundle)objects[0]);
30
+        }
31
+        return null;
32
+    }
33
+
34
+
35
+    protected void onCreate(AboutUsActivity $this,Bundle savedInstanceState) {
36
+        try{
37
+            PatchUtils.invokeProtectedMethod($this,new Object[]{$this,-641568046,new Object[]{savedInstanceState}},new Class[]{Object.class,int.class,Object[].class},"accessSuper");
38
+        }catch (Throwable t){
39
+            t.printStackTrace();
40
+        }
41
+        $this.setContentView(R.layout.activity_about_us);
42
+        SystemUtils.setImmerseLayout($this,$this.findViewById(R.id.title_layout));
43
+        TextView title = (TextView) $this.findViewById(R.id.title_bar_middle_txt);
44
+        title.setTextSize(25);
45
+        title.setText(R.string.slide_about_us);
46
+        TextView curVersionText = (TextView)$this.findViewById(R.id.tv_cur_version);
47
+        try {
48
+            curVersionText.setText( "v"+$this.getPackageManager().getPackageInfo($this.getPackageName(), 0).versionName);
49
+        }catch (Exception e){
50
+            e.printStackTrace();
51
+        }
52
+        $this.findViewById(R.id.title_bar_back_layout).setOnClickListener($this);
53
+        $this.findViewById(R.id.title_bar_option_layout).setVisibility(View.INVISIBLE);
54
+        $this.findViewById(R.id.layout_about_declare).setOnClickListener($this);
55
+        $this.findViewById(R.id.layout_check_update).setOnClickListener($this);
56
+        $this.findViewById(R.id.layout_contact_us).setOnClickListener($this);
57
+    }
58
+}

+ 19 - 0
app/src/main/java/ai/pai/client/patch/PatchesInfoImpl.java

@@ -0,0 +1,19 @@
1
+package ai.pai.client.patch;
2
+
3
+import com.android.hotpatch.framework.PatchedClassInfo;
4
+import com.android.hotpatch.framework.PatchesInfo;
5
+
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+
9
+
10
+public class PatchesInfoImpl implements PatchesInfo {
11
+    public List<PatchedClassInfo> getPatchedClassesInfo() {
12
+        List<PatchedClassInfo> patchedClassesInfos = new ArrayList<PatchedClassInfo>();
13
+        PatchedClassInfo patchedClass = new PatchedClassInfo(
14
+        		"ai.pai.client.activity.AboutUsActivity",
15
+        		AboutUsActivity$Override.class.getCanonicalName());
16
+        patchedClassesInfos.add(patchedClass);
17
+        return patchedClassesInfos;
18
+    }
19
+}

+ 18 - 6
app/src/main/java/ai/pai/client/utils/PatchManager.java

@@ -7,12 +7,14 @@ import com.android.hotpatch.utils.IPatchManager;
7 7
 import org.json.JSONObject;
8 8
 
9 9
 import java.io.File;
10
+import java.util.HashMap;
10 11
 
12
+import ai.pai.client.BuildConfig;
11 13
 import ai.pai.client.db.Preferences;
12 14
 
13 15
 public class PatchManager extends IPatchManager{
14 16
 
15
-    private String md5;
17
+    private String patchUrl;
16 18
 
17 19
     public PatchManager(){
18 20
         new File(Constants.APP_PATCH_DIR).mkdirs();
@@ -20,11 +22,21 @@ public class PatchManager extends IPatchManager{
20 22
 
21 23
     @Override
22 24
     protected String getPatchFileDownloadUrl(String patchConfigUrl) {
23
-        String jsonStr = HttpUtils.doHttpPost(patchConfigUrl,null);
25
+        if(patchConfigUrl.contains("?")){
26
+            patchConfigUrl+="&platform=android";
27
+        }else{
28
+            patchConfigUrl+="?platform=android";
29
+        }
30
+        patchConfigUrl+="&version="+ BuildConfig.VERSION_NAME;
31
+        patchConfigUrl+="&channel="+"guanwang";
32
+        HashMap<String,String> httpParams = new HashMap<>();
33
+        httpParams.put("src","0");
34
+        String jsonStr = HttpUtils.doHttpPost(patchConfigUrl,httpParams);
24 35
         try{
25 36
             JSONObject response = new JSONObject(jsonStr);
26
-            md5 = response.getString("md5");
27
-            return response.getString("patch_url");
37
+            JSONObject data = response.getJSONObject("data");
38
+            patchUrl = data.getString("patch_url");
39
+            return patchUrl;
28 40
         }catch (Exception e){
29 41
             e.printStackTrace();
30 42
         }
@@ -33,7 +45,7 @@ public class PatchManager extends IPatchManager{
33 45
 
34 46
     @Override
35 47
     protected boolean isNewPatchFound() {
36
-        if(!Preferences.getInstance(context).getPatchMD5().equals(md5)){
48
+        if(!Preferences.getInstance(context).getPatchUrl().equals(patchUrl)){
37 49
             return true;
38 50
         }
39 51
         if(!new File(patchSavePath).exists()){
@@ -44,6 +56,6 @@ public class PatchManager extends IPatchManager{
44 56
 
45 57
     @Override
46 58
     protected void afterPatchSuccess() {
47
-        Preferences.getInstance(context).setPatchMD5(md5);
59
+        Preferences.getInstance(context).setPatchUrl(patchUrl);
48 60
     }
49 61
 }

+ 1 - 1
app/src/main/java/ai/pai/client/utils/UrlContainer.java

@@ -86,5 +86,5 @@ public class UrlContainer {
86 86
 
87 87
     public static final String TOUR_INFO_URL = HOST_URL +"pai2/tginfo";
88 88
 
89
-    public static final String PATCH_CONFIG_URL = HOST_URL+"patch";
89
+    public static final String PATCH_CONFIG_URL = HOST_URL+"op/patch";
90 90
 }