本文介绍了android的自定义类加载器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个仪器库,希望在台式机和移动版(Android)上都能使用.

I'm writing an instrumentation library that I'd like to work on both desktop and mobile (Android).

它的作用是:

  1. 公开一个带有单个参数的主对象,即目标类的主对象
  2. 安装一个类加载器,该类加载器会在加载所有类时对其进行拦截并对其进行检测

像这样:

    // Expects args[0] to contain the name of the INNER main
    public static void main(String[] args) throws Throwable {
            String className = args[0];
            String [] newArgs = new String[0];

            if(args.length > 1) {
                    newArgs = Arrays.copyOfRange(args, 1, args.length-1);
            }

            System.out.println("Bootstrapping " + className);

            Loader s = new Loader(ClassLoader.getSystemClassLoader().getParent());
            Class<?> c = s.loadClass(className);
            c.getDeclaredMethod("main", new Class[] { String[].class }).invoke(
                            null, new Object[] { newArgs });
    }

问题是这样的:

如何为Android应用程序执行大致相同的操作?

How can I do roughly the same thing for an android app?

一个想法是修改android清单,以将现有活动替换为包装"活动,然后安装类加载器并调用原始基础活动.有更好的方法吗?

One idea is to modify the android manifest to replace the existing activities with "wrapper" activities, that then install class loaders and call into the original underlying activity. Is there a better way?

推荐答案

有一个名为 droidbox 的项目可以检测android恶意软件.有一个代码可以为您提供很多帮助.

There is a project called droidbox to detect android malware. There is a code that can help you a lot.

package com.loader;

import dalvik.system.DexClassLoader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class LoaderActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DexClassLoader dLoader = new DexClassLoader("/sdcard/DroidBoxTests.apk","/sdcard/", null, ClassLoader.getSystemClassLoader().getParent());

        Class calledClass = null;
        try {
            calledClass = dLoader.loadClass("droidbox.tests.DroidBoxTests");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Intent it=new Intent(this, calledClass);
        it.setClassName("droidbox.tests", "droidbox.tests.DroidBoxTests");
        startActivity(it);
    }
}

这篇关于android的自定义类加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:59