轉載自:html
http://www.cnblogs.com/over140/archive/2012/03/29/2423116.htmljava
http://www.cnblogs.com/over140/archive/2012/04/19/2446119.htmlandroid
關於第一部分(加載未安裝apk中的類),本身在看原文時遇到的問題(使用的版本android4.2):程序員
現象:app
String path = Environment.getExternalStorageDirectory() + "/";使用這句代碼,指定DexClassLoader 生成的中間文件時,報錯:ide
Caused by: java.lang.IllegalArgumentException: optimizedDirectory not readable/writable:+路徑;佈局
解決:post
官方文檔:ui
A class loader that loads classes from .jar
and .apk
files containing a classes.dex
entry. This can be used to execute code not installed as part of an application.this
This class loader requires an application-private, writable directory to cache optimized classes. Use Context.getDir(String, int)
to create such a directory:
File dexOutputDir = context.getDir("dex",0);
Do not cache optimized classes on external storage. External storage does not provide access controls necessary to protect your application from code injection attacks.
因此添加了如下代碼,問題解決了:
Context context=getApplicationContext();//獲取Context對象;
File dexOutputDir = context.getDir("dex", 0);
DexClassLoader classLoader = new DexClassLoader(path + filename, dexOutputDir.getAbsolutePath(),null, getClassLoader());
第一部分:加載未安裝apk中的類
關鍵字:Android動態加載
聲明
歡迎轉載,但請保留文章原始出處:)
博客園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com
Android中文Wiki:http://wikidroid.sinaapp.com
正文
1、前提
目的:動態加載SD卡中Apk的類。
注意:被加載的APK是未安裝的。
相關:本文是本博另一篇文章:Android動態加載jar/dex的升級版。
截圖: 成功截圖:
![](http://static.javashuo.com/static/loading.gif)
2、準備
準備被調用Android工程:TestB
ITest
public
interface ITest {
String getMoney();
}
TestBActivity
public
class TestBActivity
extends Activity
implements ITest {
/**
Called when the activity is first created.
*/
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public String getMoney() {
return "1";
}
}
代碼說明:很簡單的代碼。將生成後的TestB.apk拷貝到SD卡的根目錄下。
3、調用
調用工程TestA
public
class TestAActivity
extends Activity {
/**
Called when the activity is first created.
*/
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String path = Environment.getExternalStorageDirectory() + "/";
String filename = "TestB.apk";
DexClassLoader classLoader =
new DexClassLoader(path + filename, path,
null, getClassLoader());
try {
Class mLoadClass = classLoader.loadClass("com.nmbb.TestBActivity");
Constructor constructor = mLoadClass.getConstructor(
new Class[] {});
Object TestBActivity = constructor.newInstance(
new Object[] {});
Method getMoney = mLoadClass.getMethod("getMoney",
null);
getMoney.setAccessible(
true);
Object money = getMoney.invoke(TestBActivity,
null);
Toast.makeText(
this, money.toString(), Toast.LENGTH_LONG).show();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SecurityException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
執行的時候能夠發現會自動生成TestB.dex文件。動態加載方面還能夠搜索一下"Java動態加載"方面的資料,頗有參考價值。能夠發現比Android動態加載jar/dex使用起來方便得多。
4、下載
TestA.zip
TestB.zip
5、注意
6.1 別忘了加上SDCARD的寫權限:
android.permission.WRITE_EXTERNAL_STORAGE
6.2 一樣注意,不要再兩個工程包含package和名稱相同的接口,不然報錯。(參見Android動態加載jar/dex的後期維護)
6、擴展閱讀
探祕騰訊Android手機遊戲平臺之不安裝遊戲APK直接啓動法
(強烈推薦:QQ遊戲動態調用Activity的方法:經過ClassLoader,loadClass Activity類,而後分別在主工程的onDestroy、onKeyDown、onPause、onRestart、onResume等生命週期方法中反射調用(Method、invoke)子工程的類方法來模擬實現整個生命週期。此外巧妙的經過解壓縮APK文件來獲取遊戲的資源)
7、缺點
6.1 因爲是使用反射,沒法取得Context,也就是TestBActivity與普通的類毫無區別,沒有生命週期。
8、推薦
Android版 程序員專用搜索
第二部分:加載已安裝apk中的類和資源
聲明
歡迎轉載,但請保留文章原始出處:)
博客園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com
Android中文Wiki:http://wikidroid.sinaapp.com
正文
1、目標
注意被調用的APK在Android系統中是已經安裝的。
上篇文章:Android應用開發提升系列(4)——Android動態加載(上)——加載未安裝APK中的類
從當前APK中調用另一個已安裝APK的字符串、顏色值、圖片、佈局文件資源以及Activity。
2、實現
2.1 被調用工程
基本沿用上個工程的,添加了被調用的字符串、圖片等,因此這裏就不貼了,後面有下載工程的連接。
2.2 調用工程代碼
public
class TestAActivity
extends Activity {
/**
TestB包名
*/
private
static
final String PACKAGE_TEST_B = "com.nmbb.b";
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
final Context ctxTestB = getTestBContext();
Resources res = ctxTestB.getResources();
//
獲取字符串string
String hello = res.getString(getId(res, "string", "hello"));
((TextView) findViewById(R.id.testb_string)).setText(hello);
//
獲取圖片Drawable
Drawable drawable = res
.getDrawable(getId(res, "drawable", "testb"));
((ImageView) findViewById(R.id.testb_drawable))
.setImageDrawable(drawable);
//
獲取顏色值
int color = res.getColor(getId(res, "color", "white"));
((TextView) findViewById(R.id.testb_color))
.setBackgroundColor(color);
//
獲取佈局文件
View view = getView(ctxTestB, getId(res, "layout", "main"));
LinearLayout layout = (LinearLayout) findViewById(R.id.testb_layout);
layout.addView(view);
//
啓動TestB Activity
findViewById(R.id.testb_activity).setOnClickListener(
new OnClickListener() {
@Override
public
void onClick(View v) {
try {
@SuppressWarnings("rawtypes")
Class cls = ctxTestB.getClassLoader()
.loadClass("com.nmbb.TestBActivity");
startActivity(
new Intent(ctxTestB, cls));
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/**
* 獲取資源對應的編號
*
*
@param
testb
*
@param
resName
*
@param
resType
* layout、drawable、string
*
@return
*/
private
int getId(Resources testb, String resType, String resName) {
return testb.getIdentifier(resName, resType, PACKAGE_TEST_B);
}
/**
* 獲取視圖
*
*
@param
ctx
*
@param
id
*
@return
*/
public View getView(Context ctx,
int id) {
return ((LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(id,
null);
}
/**
* 獲取TestB的Context
*
*
@return
*
@throws
NameNotFoundException
*/
private Context getTestBContext()
throws NameNotFoundException {
return createPackageContext(PACKAGE_TEST_B,
Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
}
代碼說明:
基本原理:經過package獲取被調用應用的Context,經過Context獲取相應的資源、類。
注意:
a). 網上許多文章是經過當前工程的R.id來調用被調用工程的資源 ,這是錯誤的,即便不報錯那也是湊巧,由於R是自動生成的,兩個應用的id是沒有辦法對應的,因此須要經過getIdentifier來查找。
b). Context.CONTEXT_INCLUDE_CODE通常狀況下是不須要加的,若是layout裏面包含了自定義控件,就須要加上。注意不能在當前工程強制轉換得到這個自定義控件,由於這是在兩個ClassLoader中,沒法轉換。
c). 獲取這些資源是不須要shareUserId的。
3、總結
與上篇文章相比,獲取資源更加方便,但也存在一些限制:
3.1 被調用的apk必須已經安裝,下降用戶體驗。
3.2 style是沒法動態設置的,即便可以取到。
3.3 從目前研究結果來看,被調用工程若是使用自定義控件,會受到比較大的限制,不能強制轉換使用(緣由前面已經講過)。
3.4 因爲一個工程裏面混入了兩個Context,比較容易形成混淆,取資源也比較麻煩。這裏分享一下批量隱射兩個apk id的辦法,能夠經過反射獲取兩個apk的R類,一次獲取每個id和值,經過名稱一一匹配上,這樣就不用手工傳入字符串了。
@SuppressWarnings("rawtypes")
private
static HashMap<String, Integer> getR(Class cls)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
HashMap<String, Integer> result =
new HashMap<String, Integer>();
for (Class r : cls.getClasses()) {
if (!r.getName().endsWith("styleable")) {
Object owner = r.newInstance();
for (Field field : r.getFields()) {
result.put(field.getName(), field.getInt(owner));
}
}
}
return result;
}
4、下載
Test2012-4-19.zip
5、文章
Android類動態加載技術