Add gradle-small to the build.gradle dependencies file of your project.
classpath The 'net. Wequick. Tools. Build: gradle - small: 1.3.0 - beta 2'
Copy the code
And reference the plug-in at the end of the file:
apply plygin: 'net.wequick.small'
Copy the code
However, Small has strict requirements on the project structure. For example, the host should be the APP app at the beginning of the new project, and the name should not be changed. Small will search for hosts and plug-ins by name. Plug-in business module names start with app.* and lib.*, and package names should also start with app. Or at the end of app, plug-in business modules are individually packaged modules that are the same as apps.
Then register all of your modules in the /assets/bundle.json file
{
"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.web.about"}}]Copy the code
Jump to Other Activities
The default jump is to jump to the launch page, passing parameters in the same form as a GET request
Jumps the specified activity
After the first buildlib buildBundle
Small can also be used for hot repair
public class UpgradeManager {
private static class UpdateInfo {
public String packageName;
public String downloadUrl;
}
private static class UpgradeInfo {
public JSONObject manifest;
public List<UpgradeManager.UpdateInfo> updates;
}
private interface OnResponseListener {
void onResponse(UpgradeManager.UpgradeInfo info);
}
private interface OnUpgradeListener {
void onUpgrade(boolean succeed);
}
private static class ResponseHandler extends Handler {
private UpgradeManager.OnResponseListener mListener;
public ResponseHandler(UpgradeManager.OnResponseListener listener) {
mListener = listener;
}
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
mListener.onResponse((UpgradeManager.UpgradeInfo) msg.obj);
break;
}
}
}
private UpgradeManager.ResponseHandler mResponseHandler;
private Context mContext;
private ProgressDialog mProgressDlg;
public UpgradeManager(Context context) {
mContext = context;
}
public void checkUpgrade() {
//mProgressDlg = ProgressDialog.show(mContext, "Small"."Checking for updates...");
requestUpgradeInfo(Small.getBundleVersions(), new OnResponseListener() {
@Override public void onResponse(UpgradeInfo info) {
//mProgressDlg.setMessage("Upgrading...");
upgradeBundles(info, new OnUpgradeListener() {
@Override public void onUpgrade(boolean succeed) {
//mProgressDlg.dismiss();
//mProgressDlg = null;
String text = succeed
? "Upgrade Success! Switch to background and back to foreground to see changes."
: "Upgrade Failed!";
Log.i("tag"."= = = = = = = = = = ="+ text); Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show(); }}); }}); } /** * * @param versions * @param listener */ private void requestUpgradeInfo(Map versions, UpgradeManager.OnResponseListener listener) { System.out.println(versions); // this should be passed as HTTP parameters mResponseHandler = new UpgradeManager.ResponseHandler(listener); newThread() {
@Override public void run() {try {/ / into your server address / / Json format URL URL = new URL (see http://wequick.github.io/small/upgrade/bundles.json"http://incoidea.incopat.com/appservice/resources/bundle.json");
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();
}
private static class DownloadHandler extends Handler {
private UpgradeManager.OnUpgradeListener mListener;
public DownloadHandler(UpgradeManager.OnUpgradeListener listener) {
mListener = listener;
}
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
mListener.onUpgrade((Boolean) msg.obj);
break;
}
}
}
private UpgradeManager.DownloadHandler mHandler;
private void upgradeBundles(final UpgradeManager.UpgradeInfo info, final UpgradeManager.OnUpgradeListener listener) {
// Just for example, you can do this by OkHttp or something.
mHandler = new UpgradeManager.DownloadHandler(listener);
new Thread() {
@Override public void run() {
try {
// Update manifest
if(info.manifest ! = null) {if(! Small.updateManifest(info.manifest,false)) {
Message.obtain(mHandler, 1, false).sendToTarget();
return;
}
}
// Download bundles
List<UpgradeManager.UpdateInfo> updates = info.updates;
for (UpgradeManager.UpdateInfo u : updates) {
// Get the patch file for downloading
net.wequick.small.Bundle bundle = Small.getBundle(u.packageName);
File file = bundle.getPatchFile();
// Download
URL url = new URL(u.downloadUrl);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream is = urlConn.getInputStream();
OutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while((length = is.read(buffer)) ! = -1) { os.write(buffer, 0, length); } os.flush(); os.close(); is.close(); // Upgrade bundle.upgrade(); } Message.obtain(mHandler, 1,true).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
Message.obtain(mHandler, 1, false).sendToTarget(); } } }.start(); }}Copy the code
This class does the hot fix thing by putting bundles. Json on your server and replacing the URL here with your own url
Then call new UpgradeManager(getContext()).checkupgrade (); Then you can download the latest.so file to your server
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();
}
Copy the code
Writing rules for bundles. Json
{
"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"}}]Copy the code
When we get bundles. Json files from the server, we parse the contents of the files. When we parse the contents to the manifest*** tag, Start modifying the ***AndroidMainfes*** file to update the ***AndroidMainfest*** file
Look at the bundles. Json file ***updates*** tag, where the PKG corresponds to the package name of our plugin *** URL *** corresponds to the PKG on our server. So file address, get it. So file address, go to download and replace the original. So file, So as to achieve the role of hot repair.
Small is not yet a hot fix, because when the So file is replaced or modified, you need to exit the application and re-enter it.