編輯:Android 資訊
前不久筆者去折騰高德 SDK 中的 HUD 功能。用過該功能的用戶都知道,HUD 界面上的導航轉向圖標是動態變化的。從高德官方導航 API 文檔中 AMapNaviGuide 類的描述可知,導航轉向圖標共有 23 種類型。
咦,等等,23 種?那圖標應該放在 assets 文件夾吧?總不至於是從服務器下載的吧?看看導航 API 的 jar 包結構:
AMap_Navi_v1.3.0_20150828.jar
|- assets
|- autonavi_Resource1_1_0.png
|- custtexture*.png (7 張)
|- com
|- amap.api.navi
|- autonavi
|- META-INF
納尼?assets 上的圖片總共只有 8 張,而且內容跟 HUD 毫無關係,難道真是從服務器下載?用 Android Studio 打開 jar 包中的 AMapHudView.class 看下邏輯(AS 1.2 就引入了反編譯功能):
...
import com.autonavi.tbt.g;
...
public class AMapHudView extends FrameLayout implements OnClickListener, OnTouchListener, e {
static final int[] hud_imgActions = new int[]{
2130837532, 2130837532, 2130837532, 2130837533, 2130837534,
2130837535, 2130837536, 2130837537, 2130837538, 2130837539,
2130837522, 2130837523, 2130837524, 2130837525, 2130837526,
2130837527, 2130837528, 2130837529, 2130837530, 2130837531};
...
private ImageView roadsignimg; // 方向圖標對應的 View
...
private int resId; // 方向圖標的 id,對應 hud_imgActions 的 index,值為 0-23
...
private void updateHudWidgetContent() {
...
if (this.roadsignimg != null && this.resId != 0 && this.resId != 1) {
Drawable var1 = g.a().getDrawable(hud_imgActions[this.resId]); // g.a() 返回 Resource 對象
this.roadsignimg.setBackgroundDrawable(var1);
...
}
}
}
先看 hud_imgActions,裡面的值是不是很眼熟?轉成 16 進制均以 0x7F02 開頭(0x7F 是應用資源,0x02 則是 drawable 資源)。再看 updateHudWidgetContent(),邏輯很簡單:通過 resId 獲取 hud_imgActions 對應的 drawable id,再通過該 id 獲取對應的 Drawable 並設置到 ImageView 中。由此可確定,高德 SDK 最終是通過本地資源的索引獲取到 Drawable。
然而我們的 apk 中並沒有相應資源,為何能正常獲取到 Drawable?回到上面那句代碼:
Drawable var1 = g.a().getDrawable(hud_imgActions[this.resId]); // g.a() 返回 Resource 對象
把注意力集中到 g.a(),找到 com.autonavi.tbt.g#a():
public static Resources a() {
if (b == null) {
b = e.getResources();
}
return b;
}
其中變量 e 是上層傳入的 Activity。前面說過我們的 apk 中並沒有相應資源,所以把注意力放到變量 b 在其他地方的賦值上:
public static boolean a(Context context) {
...
a = b(context.getFilesDir() + "/autonavi_Resource1_1_0.jar");
b = a(context, a); // 變量 a 為 AssetManager
return true;
}
private static AssetManager b(String str) {
try {
Class cls = Class.forName("android.content.res.AssetManager");
AssetManager assetManager = (AssetManager) cls.getConstructor().newInstance();
try {
cls.getDeclaredMethod("addAssetPath", String.class).invoke(assetManager, str);
} catch (Throwable th) { }
return assetManager;
} catch (Throwable th2) {
return null;
}
}
private static Resources a(Context context, AssetManager assetManager) {
DisplayMetrics displayMetrics = new DisplayMetrics();
displayMetrics.setToDefaults();
return new Resources(assetManager, displayMetrics, context.getResources().getConfiguration());
}
可以看到,高德 SDK 先通過反射實例化 AssetManager,並調用 addAssetPath(context.getFilesDir() + "/autonavi_Resource1_1_0.jar"),接著實例化 Resources 對象。所以事實上是通過這個新的 Resource 來獲取對應資源的 Drawable。
但我們的 apk 對應的 files 目錄中並不存在 autonavi_Resource1_1_0.jar,這個文件又是怎麼來的?
private static String k = "autonavi_Resource1_1_0.png";
...
private static boolean b(Context var0) {
String filePath = var0.getFilesDir().getAbsolutePath() + "/autonavi_Resource1_1_0.jar";
...
InputStream var1 = var0.getResources().getAssets().open(k);
File var3 = new File(filePath);
long var21 = var3.length();
int var6 = var1.available();
if (!var3.exists() || var21 != (long) var6) {
...
File var22 = new File(filePath);
FileOutputStream var2 = new FileOutputStream(var22);
byte[] var8 = new byte[1024];
int var9;
while ((var9 = var1.read(var8)) > 0) {
var2.write(var8, 0, var9);
}
}
...
}
還是在 com.autonavi.tbt.g 這個類裡,可以看到高德把 jar 包 assets 目錄中的 autonavi_Resource1_1_0.png 複製到當前 apk 對應的 files 目錄,並命名為 autonavi_Resource1_1_0.jar。
再回到加載資源的問題:為什麼加載 autonavi_Resource1_1_0.jar 能索引到資源?因為該文件其實就是 apk(高德只是把後綴名改成了 jar)。AssetManager 加載該 apk 後,Resource 就能通過這個 AssetManager 獲取到裡面的相應資源。
AssetManager 的相關知識請參考老羅的《Android 應用程序資源管理器(Asset Manager)的創建過程分析》。
至此,我們就能清楚知道高德 SDK 是如何實現動態加載資源的:
把上述內容再精簡一下,動態加載資源所必需的幾個核心步驟:
這裡要注意,目標 apk(目錄)需要放在 context.getFilesDir() 中,否則會加載失敗(addAssetPath 返回 0)。另外,目標 apk 可以不加簽名,因為 addAssetPath 過程並沒有進行簽名校驗。
實際情況中,若要獲取相應資源,必須先獲得資源對應的 id,而外部 apk 的 R.java 並不屬於主 apk,這就給獲取資源帶來了困難。目前存在的解決方案有:
後兩種方案各有優缺點,具體怎麼選,還得結合自身場景。
動態加載資源技術目前的主要應用場景有:
動態加載資源相關文章很多,但筆者目前看到的文章只涉及如何獲取 drawable、string 等資源,尚未發現關於動態加載資源 apk 中佈局文件的內容(難道是筆者姿勢不對?_(:зゝ∠)_)。後續會分享如何動態加載資源 apk 中的佈局文件。
深入源碼:Android Handler 消息機制的實現原理
Handler 是 Android 開發中必須掌握的技術,但不少人僅停留在「會用」的階段。使用起來確實很簡單,無非兩步:在主線程重寫 handleMessage() 方法
TourGuide:為 Android App 加入優雅的操作引導
複雜的 App 常需要讓初次使用者快速理解操作路徑。TourGuide 是一個以動畫方式標示畫面元素的引導庫,適合用在首次啟動、功能介紹或新版本提示。 基本用法 使用時
Android Dialog 對話方塊的 7 種實作方式
在 Android 開發中,我們經常需要在介面上彈出一些對話方塊,比如詢問使用者或讓使用者選擇。這些功能我們稱之為 Android Dialog 對話方塊。在使用 And
擁有七年 iOS 經驗的我,為何轉投 Android 懷抱
本文由碼農網 – 唐李川 原創翻譯,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃! 上週一我格外開心,因為我獲准為一位曾合作過的客戶著手開發一個新的 Progre