許多常見的應(yīng)用都有換膚的功能,甚至支持用戶自定義皮膚。如果將所有皮膚用到的資源都打包到apk里面不僅會使得apk的大小急劇上升,也會大大增加維護的難度。所以大部分的開發(fā)者都會選擇將這些資源從apk中剝離出來放到其他地方。
一種常用的方案就是將使用到的字符串、圖片等資源打包到一個皮膚apk中,這個皮膚apk中只包含資源,沒有任何的代碼。主apk在啟動之后從這個皮膚apk中加載資源,從而減少主apk的大小,同時也將不同皮膚的資源分別放到不同項目中,提高可維護性。
獲取皮膚apk的Resources
我們都知道安卓應(yīng)用的資源是通過Resources去管理的,只要能獲取到皮膚apk的Resources,那么就能夠讀取到皮膚apk中的資源文件。
方法一
第一種獲取Resources的方式是通過皮膚apk的Context去獲取:
private Resources getResourcesByContext(Context context, String skinApkPackage) {
try {
return context.createPackageContext(skinApkPackage, Context.CONTEXT_IGNORE_SECURITY)
.getResources();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
這種方式很簡單,但是卻有一定的局限性。因為要使用這種方式去獲取Resources,必須將皮膚apk也安裝到系統(tǒng)。
方法二
第二種方法是通過獲取皮膚apk的AssetManager,直接new一個Resources出來:
private Resources getResourcesByAssetManager(Context context, String skinApkPath) {
try {
Method method = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
AssetManager assetManager = AssetManager.class.newInstance();
method.invoke(assetManager, skinApkPath);
return new Resources(
assetManager,
context.getResources().getDisplayMetrics(),
context.getResources().getConfiguration()
);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
這種方向需要用到反射去調(diào)用AssetManager.addAssetPath,但是只需要將皮膚apk下載下來,并不需要安裝也能讀取到里面的資源。
加載資源
一般我們都是通過資源的id從Resources中加載資源的,但是當(dāng)資源在其他apk里面的時候,我們沒有辦法直接通過R類知道資源的id,所以需要使用Resources的getIdentifier方法去獲取id:
public int getIdentifier(String name, String defType, String defPackage) {
return mResourcesImpl.getIdentifier(name, defType, defPackage);
}
獲取到id之后就能直接加載資源了:
public String getString(String name) {
int id = mResources.getIdentifier(name, "string", SKIN_APK_PACKAGE);
if (id == 0) {
return null;
}
return mResources.getString(id);
}
public Drawable getDrawable(String name) {
int id = mResources.getIdentifier(name, "drawable", SKIN_APK_PACKAGE);
if (id == 0) {
return null;
}
return mResources.getDrawable(id);
}
Demo
這個demo很簡單
首先新建一個皮膚應(yīng)用項目,在皮膚應(yīng)用項目中放入img.png和創(chuàng)建一個字符串:
<resources>
<string name="app_name">Skin</string>
<string name="label">hello world</string>
</resources>
然后創(chuàng)建一個主應(yīng)用項目在MainActivity中從皮膚apk加載圖片和字符串顯示出來
public class MainActivity extends AppCompatActivity {
private final static String SKIN_APK_PACKAGE = "demo.linjw.skin";
private final static String SKIN_APK = "skin-debug.apk";
private SkinHelper mSkinHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//需要將皮膚apk安裝到系統(tǒng)
mSkinHelper = new SkinHelper(this, SKIN_APK_PACKAGE);
//需要將皮膚apk放到存儲卡根目錄
//File skinApk = new File(Environment.getExternalStorageDirectory().getPath(), SKIN_APK);
//mSkinHelper = new SkinHelper(this, SKIN_APK_PACKAGE, skinApk);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(mSkinHelper.getString("label"));
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageDrawable(mSkinHelper.getDrawable("img"));
}
}
SkinHelper代碼如下:
public class SkinHelper {
private final Resources mResources;
private final String mSkinApkPackage;
public SkinHelper(Context context, String skinApkPackage) {
mSkinApkPackage = skinApkPackage;
mResources = getResourcesByContext(context, skinApkPackage);
}
public SkinHelper(Context context, String skinApkPackage, File skinApk) {
mSkinApkPackage = skinApkPackage;
mResources = getResourcesByAssetManager(context, skinApk.getPath());
}
/**
* 使用Context.createPackageContext加載Resource
*
* @param context
* @return
*/
private Resources getResourcesByContext(Context context, String skinApkPackage) {
try {
return context.createPackageContext(skinApkPackage, Context.CONTEXT_IGNORE_SECURITY)
.getResources();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 使用反射創(chuàng)建AssertManager加載Resource
*
* @param context
* @return
*/
private Resources getResourcesByAssetManager(Context context, String skinApkPath) {
try {
Method method = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
AssetManager assetManager = AssetManager.class.newInstance();
method.invoke(assetManager, skinApkPath);
return new Resources(
assetManager,
context.getResources().getDisplayMetrics(),
context.getResources().getConfiguration()
);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
public String getString(String name) {
int id = mResources.getIdentifier(name, "string", mSkinApkPackage);
if (id == 0) {
return null;
}
return mResources.getString(id);
}
public Drawable getDrawable(String name) {
int id = mResources.getIdentifier(name, "drawable", mSkinApkPackage);
if (id == 0) {
return null;
}
return mResources.getDrawable(id);
}
}
效果如下:
完整代碼
完整代碼可以在這里獲取