android动态加载

转载自: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的升级版。

 

    截图: 成功截图:

      

 

  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文件来获取游戏的资源)

 

    Android中文Wiki:DexFile

 

 

  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类动态加载技术 

相关文章
相关标签/搜索