亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

動(dòng)態(tài)加載DEX

zhangke3016 / 3313人閱讀

摘要:一什么是動(dòng)態(tài)加載為什么要?jiǎng)討B(tài)加載動(dòng)態(tài)加載就是用到的時(shí)候再去加載,也叫懶加載,也就意味著用不到的時(shí)候是不會(huì)去加載的。

一.什么是動(dòng)態(tài)加載?為什么要?jiǎng)討B(tài)加載?

動(dòng)態(tài)加載就是用到的時(shí)候再去加載,也叫懶加載,也就意味著用不到的時(shí)候是不會(huì)去加載的。

二.編寫(xiě)Demo 1.利用jar,dx 創(chuàng)建dex 1)創(chuàng)建DynamicDex.java,生成DynamicDex.class

2)生成dex




jar是JDK提供的,dx是Android SDK提供,只要配置好環(huán)境變量,可在任意位置執(zhí)行
我是在debug中執(zhí)行,方便使用
jar -cvf cn 生成jar包
dx --dex --output=target.jar source.jar 生成包含dex的jar包
注意:把dex放入assets后,把DynamicDex.java刪掉或改名,否則加載的是它,而不是dex中的類(lèi)

2.加載Dex
    public class DexUtil {
    public static void loadDexClass(final Context context, final String dexName, final Handler handler) {
        new AsyncTask() {
            @Override
            protected String doInBackground(Void... params) {

                File cacheFile = getCacheDir(context.getApplicationContext());
                String internalPath = cacheFile.getAbsolutePath() + File.separator + dexName;
                Log.v("DexUtil", "internalPath = " + internalPath);
                File desFile = new File(internalPath);
                if (!desFile.exists()) {
                    try {
                        if (!desFile.exists()) {
                            desFile.createNewFile();
                            copyFiles(context, dexName, desFile);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return internalPath;
            }

            @Override
            protected void onPostExecute(String internalPath) {
                super.onPostExecute(internalPath);
                File dexOutputDir = context.getDir("dex", 0);
                File soOutputDir = context.getDir("lib", 0);
                String librarySerachPath = soOutputDir.getAbsolutePath().replace("app_lib", "lib");
                Log.v("DexUtil", "librarySerachPath = " + librarySerachPath);
                DexClassLoader dexClassLoader = new DexClassLoader(internalPath, dexOutputDir.getAbsolutePath(),
                        librarySerachPath, context.getClassLoader());//ClassLoader.getSystemClassLoader().getParent());
                Message msg = new Message();
                msg.obj = dexClassLoader;
                handler.sendMessage(msg);
            }
        }.execute();

    }


    public static void copyFiles(Context context, String fileName, File desFile) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = context.getApplicationContext().getAssets().open(fileName);
            out = new FileOutputStream(desFile.getAbsolutePath());
            byte[] bytes = new byte[1024];
            int i;
            while ((i = in.read(bytes)) != -1) {
                out.write(bytes, 0, i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static boolean hasExternalStorage() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 獲取緩存路徑
     *
     * @param context
     * @return 返回緩存文件路徑
     */
    public static File getCacheDir(Context context) {
        File cache;
        if (hasExternalStorage()) {
            cache = context.getExternalCacheDir();
        } else {
            cache = context.getCacheDir();
        }
        if (!cache.exists())
            cache.mkdirs();
        return cache;
    }

}

Dex加載步驟:
1)把a(bǔ)ssets中的dex的jar拷貝到met目錄下
文件拷貝需要添加權(quán)限

2)利用DexClassLoader加載Dex,把dex放入到相關(guān)目錄上


注意:文件的拷貝需要放在子線(xiàn)程中運(yùn)行
3.利用反射加載

public class MainActivity extends AppCompatActivity {

    public static DexClassLoader mapLoader;
    TextView tv_hello;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_hello = (TextView) this.findViewById(R.id.tv_hello);
        DexUtil.loadDexClass(this, "dex_dynamic.jar", dexHandler);

    }

    private Handler dexHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mapLoader = (DexClassLoader) msg.obj;
            try {
                Class DynamicDex_Class = mapLoader.loadClass("cn.liufei.dynamicdex.DynamicDex");
                Constructor DynamicDex_Cons = DynamicDex_Class.getConstructor(null);
                Object DynamicDex_Obj = DynamicDex_Cons.newInstance();
                Method test_meth = DynamicDex_Class.getMethod("test", null);
                String result = (String) test_meth.invoke(DynamicDex_Obj, null);
                tv_hello.setText(result);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

        }
    };
}

Demo地址:鏈接: https://pan.baidu.com/s/1kVMQ6jX 密碼: i4vp

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/70905.html

相關(guān)文章

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<