您好,登錄后才能下訂單哦!
項(xiàng)目介紹:
speed-tools 是一款基于代理模式的動(dòng)態(tài)部署apk熱更新框架、插件化開發(fā)框架;
speed-tools這個(gè)名字主要指的快速迭×××發(fā)工具集的意思。
功能與特性:
1、支持Android 2.3 以上版本
2、支持R文件資源直接調(diào)用
3、開發(fā)過程中無發(fā)射調(diào)用
4、apk無需安裝直接調(diào)用
5、代理模式對(duì)代碼侵入性少
6、使用簡(jiǎn)單,只需要繼承簡(jiǎn)單的類即可
添加依賴:
compile 'com.liyihangjson:speed_tools:1.0.3'
首先看看項(xiàng)目結(jié)構(gòu):
lib_speed_tools: 插件化核心功能library
module_host_main:宿主工程主工程,負(fù)責(zé)加載部署apk
module_client_one:測(cè)試業(yè)務(wù)apk 1
module_client_two:測(cè)試業(yè)務(wù)apk 2
lib_img_utils:測(cè)試imageloader圖片框架
注意:需要使用speed tools 只需要依賴lib_speed_tools即可,然后開始配置插件化步驟:
首先在module_client_one中創(chuàng)建業(yè)務(wù)邏輯類:TestClass.java
/**
* by liyihang
*/
public class TestClass extends SpeedBaseInterfaceImp {
private Activity activity;
@Override
public void onCreate(Bundle savedInstanceState, final Activity activity) {
this.activity=activity;
activity.setContentView(R.layout.activity_client_main);
activity.findViewById(R.id.jump).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SpeedUtils.goActivity(activity,"first_apk", "two_class");
}
});
ImageView imageView= (ImageView) activity.findViewById(R.id.img_view);
imageView.setVisibility(View.VISIBLE);
ImgUtils.getInstance(activity).showImg("http://img.my.csdn.net/uploads/201309/01/1378037235_3453.jpg", imageView);
}
}
SpeedBaseInterfaceImp業(yè)務(wù)組件中業(yè)務(wù)activity代理類,他是實(shí)現(xiàn)了主要的生命周期方法,相當(dāng)于組件的activity類。
然后創(chuàng)建hock類每個(gè)業(yè)務(wù)組件中只創(chuàng)建一個(gè):ClientMainActivity.java
public class ClientMainActivity extends SpeedClientBaseActivity {
@Override
public SpeedBaseInterface getProxyBase() {
return new TestClass();
}
}
這個(gè)類在組件中是唯一的,作用就是hock在獨(dú)立測(cè)試時(shí)候使用。
接下來配置配置組件的AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/SpeedTheme">
<!--必須設(shè)置root_class-->
<meta-data
android:name="root_class"
android:value="com.example.clientdome.TestClass" />
<meta-data
android:name="two_class"
android:value="com.example.clientdome.TwoClass" />
<activity
android:name=".ClientMainActivity"
android:theme="@style/SpeedTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--組件意圖-->
<intent-filter>
<data android:scheme="speed_tools" android:host="sijienet.com" android:path="/find_class"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
組件意圖寫死保持一直,root_class 是調(diào)用死后使用對(duì)于配置的com.example.clientdome.TestClass業(yè)務(wù)類。這樣業(yè)務(wù)組件配置完成。
接下來配置宿主工程module_host_main;
創(chuàng)建宿主工程唯一hock類:ApkActivity.java
/**
* by liyihang
* blog http://sijienet.com/
*/
public class ApkActivity extends SpeedHostBaseActivity {
@Override
public String getApkKeyName() {
return HostMainActivity.FIRST_APK_KEY;
}
@Override
public String getClassTag() {
return null;
}
}
整個(gè)宿主工程創(chuàng)建一個(gè)類即可,用戶是hock activity;然后創(chuàng)建一個(gè)開屏頁apk第一次加載時(shí)候需要一些時(shí)間,放入開屏等待頁面是非常合適的。
HostMainActivity.java
/**
* by liyihang
* blog http://sijienet.com/
*/
public class HostMainActivity extends AppCompatActivity implements Runnable,Handler.Callback, View.OnClickListener {
public static final String FIRST_APK_KEY="first_apk";
public static final String TWO_APK_KEY="other_apk";
private Handler handler;
private TextView showFont;
private ProgressBar progressBar;
private Button openOneApk;
private Button openTwoApk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_host_main);
showFont= (TextView) findViewById(R.id.show_font);
progressBar= (ProgressBar) findViewById(R.id.progressbar);
openOneApk= (Button) findViewById(R.id.open_one_apk);
openTwoApk= (Button) findViewById(R.id.open_two_apk);
handler=new Handler(this);
new Thread(this).start();
}
@Override
public void run() {
String s = "module_client_one-release.apk";
String dexOutPath="dex_output2";
File nativeApkPath = SpeedUtils.getNativeApkPath(getApplicationContext(), s);
SpeedApkManager.getInstance().loadApk(FIRST_APK_KEY, nativeApkPath.getAbsolutePath(), dexOutPath, this);
String s2 = "module_client_two-release.apk";
String dexOutPath3="dex_output3";
File nativeApkPath2 = SpeedUtils.getNativeApkPath(getApplicationContext(), s2);
SpeedApkManager.getInstance().loadApk(TWO_APK_KEY, nativeApkPath2.getAbsolutePath(), dexOutPath3, this);
handler.sendEmptyMessage(0x78);
}
@Override
public boolean handleMessage(Message message) {
showFont.setText("當(dāng)前是主宿主apk\n插件apk完畢");
progressBar.setVisibility(View.GONE);
openOneApk.setVisibility(View.VISIBLE);
openTwoApk.setVisibility(View.VISIBLE);
openOneApk.setOnClickListener(this);
openTwoApk.setOnClickListener(this);
return false;
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.open_one_apk)
{
SpeedUtils.goActivity(this, FIRST_APK_KEY, null);
}
if (v.getId()==R.id.open_two_apk)
{
SpeedUtils.goActivity(this, TWO_APK_KEY, null);
}
}
}
加載apk核心代碼是:
String s = "module_client_one-release.apk";
String dexOutPath="dex_output2";
File nativeApkPath = SpeedUtils.getNativeApkPath(getApplicationContext(), s);
SpeedApkManager.getInstance().loadApk(FIRST_APK_KEY, nativeApkPath.getAbsolutePath(), dexOutPath, this);
業(yè)務(wù)apk都是放在assets目錄中。最后配置AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hostproject">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/SpeedTheme">
<!--啟動(dòng)activity 加載apk-->
<activity android:name=".HostMainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--組件hack-->
<activity
android:name=".ApkActivity"
android:label="@string/app_name"
android:theme="@style/SpeedTheme" >
<intent-filter>
<data android:scheme="speed_tools" android:host="sijienet.com" android:path="/find_class"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
這樣所有配置結(jié)束,插件化實(shí)現(xiàn)。
github: https://github.com/jasonliyihang/speed_tools
作者:一航
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。