android動態加載


轉載自:

http://www.cnblogs.com/over140/archive/2012/03/29/2423116.html

http://www.cnblogs.com/over140/archive/2012/04/19/2446119.html

 

關於第一部分(加載未安裝apk中的類),自己在看原文時遇到的問題(使用的版本android4.2):

現象:

String path = Environment.getExternalStorageDirectory() + "/";使用這句代碼,指定DexClassLoader 生成的中間文件時,報錯:

Caused by: java.lang.IllegalArgumentException: optimizedDirectory not readable/writable:+路徑;

解決:

官方文檔:

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 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

 

正文

  一、前提

    目的:動態加載SD卡中Apk的類。

    注意:被加載的APK是未安裝的。

    相關:本文是本博另外一篇文章:Android動態加載jar/dex的升級版。

 

    截圖: 成功截圖:

      

 

  二、准備

    准備調用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卡的根目錄下。

 

  三、調用 

    調用工程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使用起來方便得多。

 

  四、下載

    TestA.zip

    TestB.zip    

 

  五、注意

    6.1  別忘了加上SDCARD的寫權限:

      android.permission.WRITE_EXTERNAL_STORAGE

    6.2  同樣注意,不要再兩個工程包含package和名稱相同的接口,否則報錯。(參見Android動態加載jar/dex的后期維護)

 

  六、擴展閱讀

    探秘騰訊Android手機游戲平台之不安裝游戲APK直接啟動法

    (強烈推薦:QQ游戲動態調用Activity的方法:通過ClassLoader,loadClass Activity類,然后分別在主工程的onDestroy、onKeyDown、onPause、onRestart、onResume等生命周期方法中反射調用(Method、invoke)子工程的類方法來模擬實現整個生命周期。此外巧妙的通過解壓縮APK文件來獲取游戲的資源)

 

    Android中文Wiki:DexFile

 

 

  七、缺點

    6.1  由於是使用反射,無法取得Context,也就是TestBActivity與普通的類毫無區別,沒有生命周期。

 

  八、推薦

    Android版 程序員專用搜索

 

第二部分:加載已安裝apk中的類和資源

 

聲明
  歡迎轉載,但請保留文章原始出處:) 
    博客園:http://www.cnblogs.com
    農民伯伯: http://over140.cnblogs.com 

    Android中文Wiki:http://wikidroid.sinaapp.com  

 

正文

  一、目標

    注意被調用的APK在Android系統中是已經安裝的。

   上篇文章:Android應用開發提高系列(4)——Android動態加載(上)——加載未安裝APK中的類 

    從當前APK中調用另外一個已安裝APK的字符串、顏色值、圖片、布局文件資源以及Activity。

     

 

  二、實現

    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.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;
復制代碼

    } 

 

  四、下載 

     Test2012-4-19.zip

 

  五、文章

    Android類動態加載技術 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM