Android插件化開發之OpenAtlas初體驗

OpenAtlas一款強大的Android非代理動態部署框架,目前已經處於穩定定狀態。 
與傳統的代理方式插件不同,OpenAtlas需要對註冊動態部署的組件到manifest文件。

初體驗,只不過就是把程序跑起來,跑通,後面的路還很長。這個過程中也是遇到了不少問題,剛開始拿到這個項目真是無處下手(沒有看到有wiki),後來百度搜索了下找到四篇博文,大概就是OpenAtlas的作者寫的吧

後來,意外的發現項目上是有wiki的,真是想找塊豆腐撞死算了

當然issues也可以當做學習的資料

基本上,能找的到的資源就這麼多了

最後上一下項目主頁地址

其實最早接觸到Atlas也只是在一個多月前吧,當時去面試,面試官問我怎麼動態加載dex,當時沒聽清楚,只是啊?了一下,他重複了一遍,說是65536方法數爆炸有沒有遇到過,其實吧,說實話,真沒遇到過,但是呢,就在那幾天前在github上看到過這個問題,地址應該在這裏Android應用方法數量超過65K個怎麼辦?,當時知道可以通過multi-dex來解決。面試完後自己回去搜了一下,最後發現方案很多,這裏有總結Android 插件化 動態升級,然後這裏是美團的解決方案美團Android DEX自動拆包及動態加載簡介,Atlas最開始是在trinea的主頁上看到的,一個優酷上的視頻視頻: 阿里技術沙龍第十六期《android插件化及動態部署—ATLAS 》伯奎,然後就去百度搜Atlas,結果很失望,搜不到這個東西。。。。後來去github上搜,無意間看到了OpenAtlas,粗粗瞄了一眼,感覺大概就是Atlas了,於是Star了該項目。知道最近,纔開始研究這個東西。於是我就有問題了,這個OpenAtlas和淘寶的Atlas究竟有什麼區別,就試着跑一個Demo試試看。這個過程也是有的艱苦的啊。。。。

首先從github上把核心項目下載下來https://github.com/bunnyblue/OpenAtlashttps://github.com/bunnyblue/OpenAtlasExtension, 原來的項目是eclipse和android studio都兼容的,個人有強迫症,強行把項目結構轉化爲了android studio的,轉化完成後就是如圖所示的結構,其實只是拷貝文件而已。

這裏寫圖片描述

openatlasbundler是一個生成BundleList文件的Java項目,轉到android studio下新建的是Java library項目,之後要修改下build.gradle

apply plugin: 'java'
apply plugin: 'application'

mainClassName='com.openAtlas.bundleInfo.maker.BundleMakeBooter'
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

先把patch的aapt替換原來的,在..\OpenAtlasExtension\buildTools\binary\build.tool.R22\windows\aapt下找到aapt.exe,備份sdk目錄..\sdk\build-tools\22.0.1中對應的aapt,將patch的aapt.exe移到該目錄。

在app的module中,也就是宿主,增加依賴,依賴項目爲openatlascore

compile project(':openatlascore')
  • 1
  • 1

新建一個類繼承AtlasApp,在清單文件中指定該Application的name爲該類。

新建一個module作爲test plugin,注意包名以com開頭,這個是因爲這個庫裏面寫死了是以libcom_開頭的進行查找,也就是包名爲com開頭。我們將這個module中的清單文件中關於引用v7包中的資源刪除,比如主題,也就是在清單文件中刪除style屬性,在style.xml中刪除所有主題什申明。將v4,v7包以provided形式提供,

provided files('libs/android-support-v4.jar')
provided files('libs/android-support-v7-appcompat.jar')
  • 1
  • 2
  • 1
  • 2

至於原因麼,v7包中的資源和類會在宿主中編譯進去,插件不用編譯進去。否則會報錯的喲,見這個issueActivity繼承AppCompatActivity的問題

然後你就編譯該模塊,會生成一個apk,在項目根目錄新建一個目錄叫plugin,將生產的apk拷到該目錄

打開openatlasbundler項目中的BundleMakeBooter類,修改代碼,將main函數前3句註釋掉,加入下面三行代碼。,目錄指向的就是剛纔新建的plugin目錄。然後在該類上右鍵運行。

args=new String[2];
args[0]="C:\\Users\\kltz\\Desktop\\AtlasDemo\\plugin";
args[1]="C:\\Users\\kltz\\Desktop\\AtlasDemo\\plugin\\bundle-info.json";
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

最終會在plugin目錄生成一個so和一個json

這裏寫圖片描述

將該so拷到宿主程序的src/main/jniLibs/armeabi文件夾下,沒有則新建。將json文件拷到src/main/assets目錄下,沒有則新建。

然後在宿主中添加測試代碼,跳轉到插件的Activity中去。如果成功跳轉,說明你成功了一點點。 
但是在運行前,你需要在宿主的清單中申明該Activity,必須申明哦

        <activity
            android:name="com.lizhangqu.test.MainActivity"
            >
        </activity>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
Intent intent = new Intent();
intent.setClassName(MainActivity.this, "com.lizhangqu.test.MainActivity");
startActivity(intent);
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

我以相同方式加入二維碼掃描,使用的是google的zxing。過程同test模塊,最後調用

Intent intent=new Intent();
intent.setClassName(MainActivity.this,"com.lizhangqu.zxing.android.CaptureActivity");
startActivity(intent);
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

而且你還得在宿主的清單中加入權限,記住是宿主,在插件的清單中加入依然會報錯。

   <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.front"
        android:required="false" />
    <!-- TODO replace above two with next line after Android 4.2 -->
    <!-- <uses-feature android:name="android.hardware.camera.any"/> -->
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.flash"
        android:required="false" />
    <uses-feature android:name="android.hardware.screen.landscape" />
    <uses-feature
        android:name="android.hardware.wifi"
        android:required="false" />
    <!-- This excludes Google TV, which is unfortunately included by virtue of not requiring a camera -->
    <uses-feature android:name="android.hardware.touchscreen" />
    <!-- TODO make this not required again after android.hardware.camera.any is available -->


    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

同時注意申明Activity

        <activity
            android:name="com.lizhangqu.zxing.android.CaptureActivity"
            >
        </activity>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

成功打開掃描界面就可以了

這裏寫圖片描述

總結爲一句話就是基礎的公共資源放在宿主中,插件中不要將這些東西依賴進去,只是以provided形式編譯。

最後來看看使用了atlas之後,app的data目錄會有什麼變化。

這裏寫圖片描述

最後,來看看淘寶Android客戶端

這裏寫圖片描述

簡直完美,幾乎是一樣的,其實可以差不多確定淘寶的Atlas和OpenAtlas沒什麼區別了。

可能會有人不明白,插件apk是如何轉化爲so的,其實很簡單,沒有轉化,只是修改了一個後綴而已,不信?不信你把so改成apk再安裝一下試試,絕逼可以正常的跑!這也是個坑。告訴我們不要被表面迷惑

文章有點亂,不親自試驗過還是會看得迷迷糊糊的,提供源碼下載。

http://download.csdn.net/detail/sbsujjbcy/8996357

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章