zoukankan      html  css  js  c++  java
  • 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类动态加载技术 

  • 相关阅读:
    编程之类 字符串包括问题
    Performance Counter的使用
    MVVM Light Toolkit使用指南
    Lambda表达式详解
    C#线程篇---Task(任务)和线程池不得不说的秘密(5)
    在WPF中如何使用RelativeSource绑定
    WPF中使用ObjectDataProvider绑定方法
    免费的精品: Productivity Power Tools 动画演示
    使用Myeclipse进行简单webservice开发的示例
    <context:component-scan>使用说明
  • 原文地址:https://www.cnblogs.com/LittleRedPoint/p/3429709.html
Copyright © 2011-2022 走看看