Custom classloader for Android?

I am writing a tool library in which I would like to work both on the desktop and mobile (Android).

It performs the function of:

  • Providing a primary that takes one parameter, the primary target class
  • Install a class loader that intercepts all classes as they are loaded and executes them.

Same:

// 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 }); } 

The question arises:

How can I do roughly the same for an Android app?

One idea is to change the android manifest to replace the existing actions with a β€œwrapper”, and then install the class loaders and call the original underlying asset. Is there a better way?

+4
source share
1 answer

To detect malware for Android, there is a project called droidbox . There is 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); } } 
+5
source

Source: https://habr.com/ru/post/1415335/


All Articles