Small 插件化
跳转指定的activity
先buildlib后buildBundle
small也可以实现热修复的功能
|
|
上面这个类就实现了热修复的功能
把bundles.json放在你的服务器上
这里的url换成你自己的url
然后调用
private void requestUpgradeInfo(Map versions, UpgradeManager.OnResponseListener listener) {
System.out.println(versions); // this should be passed as HTTP parameters
mResponseHandler = new UpgradeManager.ResponseHandler(listener);
new Thread() {
@Override public void run() {
try {
// Example HTTP request to get the upgrade bundles information.
// Json format see http://wequick.github.io/small/upgrade/bundles.json
URL url = new URL(“yourUrl”);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
InputStream is = conn.getInputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
sb.append(new String(buffer, 0, length));
}
// Parse json
JSONObject jo = new JSONObject(sb.toString());
JSONObject mf = jo.has("manifest") ? jo.getJSONObject("manifest") : null;
JSONArray updates = jo.getJSONArray("updates");
int N = updates.length();
List<UpgradeManager.UpdateInfo> infos = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
JSONObject o = updates.getJSONObject(i);
UpgradeManager.UpdateInfo
info = new UpgradeManager.UpdateInfo();
info.packageName = o.getString("pkg");
info.downloadUrl = o.getString("url");
infos.add(info);
}
// Post message
UpgradeManager.UpgradeInfo
ui = new UpgradeManager.UpgradeInfo();
ui.manifest = mf;
ui.updates = infos;
Message.obtain(mResponseHandler, 1, ui).sendToTarget();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
|
|
{
“manifest”: {
“version”: “1.0.0”,
“bundles”: [
{
“uri”: “lib.utils”,
“pkg”: “net.wequick.example.small.lib.utils”
},
{
“uri”: “lib.style”,
“pkg”: “com.example.mysmall.lib.style”
},
{
“uri”: “lib.analytics”,
“pkg”: “net.wequick.example.lib.analytics”
},
{
“uri”: “main”,
“pkg”: “net.wequick.example.small.app.main”
},
{
“uri”: “home”,
“pkg”: “net.wequick.example.small.app.home”
},
{
“uri”: “mine”,
“pkg”: “net.wequick.example.small.app.mine”
},
{
“uri”: “detail”,
“pkg”: “net.wequick.example.small.app.detail”,
“rules”: {
“sub”: “Sub”
}
},
{
“uri”: “stub”,
“type”: “app”,
“pkg”: “net.wequick.example.small.appok_if_stub”
},
{
“uri”: “about”,
“pkg”: “net.wequick.example.small.app.about”
}
]
},
“updates”: [
{
“pkg”: “net.wequick.example.small.app.about”,
“url”: “http://wequick.github.io/small/upgrade/libnet_wequick_example_small_app_about.so“
},
{
“pkg”: “net.wequick.example.small.lib.utils”,
“url”: “http://wequick.github.io/small/upgrade/libnet_wequick_example_small_lib_utils.so“
},
{
“pkg”: “com.example.mysmall.lib.style”,
“url”: “http://wequick.github.io/small/upgrade/libcom_example_mysmall_lib_style.so“
},
{
“pkg”: “net.wequick.example.small.app.home”,
“url”: “http://wequick.github.io/small/upgrade/libnet_wequick_example_small_app_home.so“
}
]
}
```
看到这我们发现bundles的第一个标签是 manifest ,从上面代码发现当我们从服务器获取到bundles.json文件时 就去解析文件内容,解析到manifest标签的时候 ,开始修改AndroidMainfes文件,从而达到更新AndroidMainfest 文件
再来看bundles.json文件
updates标签 这里面 pkg 对应的是我们插件的包名 url则对应的是我们服务器上对应的.So文件的地址,拿到.So文件的地址,就去下载并替换原有的.So文件,从而达到热修复的作用。
其实Small还不能说是热修复,因为当So文件被替换修改的时候需要推出应用,再次进去入才能有效果。