項目介紹:java
speed-tools 是一款基於代理模式的動態部署apk熱更新框架、插件化開發框架;android
speed-tools這個名字主要指的快速迭代開發工具集的意思。git
功能與特性:github
一、支持Android 2.3 以上版本json
二、支持R文件資源直接調用app
三、開發過程當中無發射調用框架
四、apk無需安裝直接調用dom
五、代理模式對代碼侵入性少ide
六、使用簡單,只須要繼承簡單的類便可工具
添加依賴:
compile 'com.liyihangjson:speed_tools:1.0.3'
首先看看項目結構:
lib_speed_tools: 插件化核心功能library
module_host_main:宿主工程主工程,負責加載部署apk
module_client_one:測試業務apk 1
module_client_two:測試業務apk 2
lib_img_utils:測試imageloader圖片框架
注意:須要使用speed tools 只須要依賴lib_speed_tools便可,而後開始配置插件化步驟:
首先在module_client_one中建立業務邏輯類: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業務組件中業務activity代理類,他是實現了主要的生命週期方法,至關於組件的activity類。
而後建立hock類每一個業務組件中只建立一個:ClientMainActivity.java
public class ClientMainActivity extends SpeedClientBaseActivity { @Override public SpeedBaseInterface getProxyBase() { return new TestClass(); } }
這個類在組件中是惟一的,做用就是hock在獨立測試時候使用。
接下來配置配置組件的AndroidManifest.xml
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/SpeedTheme"> <!--必須設置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 是調用死後使用對於配置的com.example.clientdome.TestClass業務類。這樣業務組件配置完成。
接下來配置宿主工程module_host_main;
建立宿主工程惟一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; } }
整個宿主工程建立一個類便可,用戶是hock activity;而後建立一個開屏頁apk第一次加載時候須要一些時間,放入開屏等待頁面是很是合適的。
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 dexOutPath2="dex_output3"; File nativeApkPath1 = SpeedUtils.getNativeApkPath(getApplicationContext(), s2); SpeedApkManager.getInstance().loadApk(TWO_APK_KEY, nativeApkPath1.getAbsolutePath(), dexOutPath2, this); handler.sendEmptyMessage(0x78); } @Override public boolean handleMessage(Message message) { showFont.setText("當前是主宿主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);
業務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"> <!--啓動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>
這樣全部配置結束,插件化實現。
github: https://github.com/jasonliyihang/speed_tools
做者:一航