android 外掛程式化架構 speed-tools

來源:互聯網
上載者:User

標籤:代理   dcl   需要   外掛程式化   hack   util   blog   abs   ref   

項目介紹:

speed-tools 是一款基於代理模式的動態部署apk熱更新架構、外掛程式化開發架構;

speed-tools這個名字主要指的快速迭×××發工具集的意思。

功能與特性:

1、支援Android 2.3 以上版本

2、支援R檔案資源直接調用

3、開發過程中無發射調用

4、apk無需安裝直接調用

5、代理模式對代碼侵入性少

6、使用簡單,只需要繼承簡單的類即可

使用方法

添加依賴:

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

一航

android 外掛程式化架構 speed-tools

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.