加载未安装apk实现插件化
通过加载未安装应用的方式实现app插件化获取未安装应用的信息获取未安装应用的Resources对象获取插件应用中的图片简单使用
通过加载未安装应用的方式实现app插件化
学习应用插件化技术,通过加载未安装apk,实现插件化功能,此处做个笔记,方便查阅
获取未安装应用的信息
private String
[] getUninstallApkInfo(Context context
,String apkDir
)throws Exception
{
String
[] apkInfo
= new String[2];
PackageManager pm
= context
.getPackageManager();
PackageInfo packageInfo
= pm
.getPackageArchiveInfo(apkDir
, PackageManager
.GET_ACTIVITIES
);
if(packageInfo
!=null
){
ApplicationInfo appInfo
= packageInfo
.applicationInfo
;
String versionName
= packageInfo
.versionName
;
String packageName
= appInfo
.packageName
;
Drawable icon
= pm
.getApplicationIcon(appInfo
);
String appName
= pm
.getApplicationLabel(appInfo
).toString();
apkInfo
[0] = "plug.apk";
apkInfo
[1] = packageName
;
}
return apkInfo
;
}
获取未安装应用的Resources对象
private Resources
getPluginResources(Context context
,String apkDir
) {
try {
AssetManager assetManager
= AssetManager
.class.newInstance();
Method addAssetPath
= assetManager
.getClass().getMethod("addAssetPath", String
.class);
addAssetPath
.invoke(assetManager
, apkDir
);
Resources superRes
= context
.getResources();
Resources resources
= new Resources(assetManager
, superRes
.getDisplayMetrics(), superRes
.getConfiguration());
return resources
;
}catch (Exception e
){
e
.printStackTrace();
return null
;
}
}
获取插件应用中的图片
public Drawable
dynamicLoadApk() throws Exception
{
String apkName
= "plugin.apk";
String mApkDir
= Environment
.getExternalStorageDirectory().getPath()+File
.separator
+"Pictures"+File
.separator
+apkName
;
String apkPackageName
= mApkInfo
[1];
File optimizedDirFile
= mContext
.getDir("dex", Context
.MODE_PRIVATE
);
DexClassLoader dexClassLoader
= new DexClassLoader(mApkDir
, optimizedDirFile
.getPath(), null
, ClassLoader
.getSystemClassLoader());
Class
<?> clazz
= dexClassLoader
.loadClass(Class
<?> clazz
= dexClassLoader
.loadClass(apkPackageName
+ ".R$mipmap"); + ".R$mipmap");
Field field
= clazz
.getDeclaredField("one");
int resId
= field
.getInt(R
.mipmap
.class);
Resources resources
= getPluginResources(mContext
, mApkDir
);
if(resources
!=null
){
Drawable drawable
= resources
.getDrawable(resId
);
return drawable
;
}
return null
;
}
简单使用
public class MainActivity extends Activity {
private static final String appName
= "plug.apk";
private Drawable drawable
;
Handler mHandler
= new Handler(){
@Override
public void handleMessage(Message msg
) {
super.handleMessage(msg
);
switch (msg
.what
){
case 1:
bg
.setImageDrawable(drawable
);
break;
}
}
};
private ImageView bg
;
private LoadUtils loadUtils
;
@Override
protected void onCreate(Bundle savedInstanceState
) {
super.onCreate(savedInstanceState
);
setContentView(R
.layout
.activity_main
);
init();
}
private void init() {
bg
= ((ImageView
) findViewById(R
.id
.bg
));
try {
loadUtils
= new LoadUtils(this, appName
);
drawable
= loadUtils
.dynamicLoadApk();
} catch (Exception e
) {
e
.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu
) {
getMenuInflater().inflate(R
.menu
.menu
,menu
);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item
) {
switch (item
.getItemId()){
case R
.id
.update
:
mHandler
.sendEmptyMessage(1);
break;
}
return true;
}
}