Android Dynamic class generates preloaded-dexmaker using

Source: Internet
Author: User

First, Dexmaker simple introduction

Dexmaker is an API that runs on Android Dalvik VMs and is written in Java to dynamically generate Dex bytecode. If the reader understands AOP programming, you should have heard of cglib or ASM, but both tools generate Java bytecode, and Dalvik load must be DEX byte code. So, for AOP programming on Android, Dexmaker can be a good choice. Project address: Https://github.com/crittercism/dexmaker.

Two. Simple to use

The following example is very typical, it can be said that the introduction is very good. The process is simple, generate a class that contains a function, load it dynamically in the main program (using ClassLoader), and then execute the function inside the class. This is in the Java platform example, I directly on the Android programming, behind or explain the corresponding problems and solutions, down to see this example.

Public final class Helloworldmaker {public static void main (string[] args) throws Exception {Dexmaker Dexmaker        = new Dexmaker ();        Generate a HelloWorld class.        typeid<?> HelloWorld = Typeid.get ("Lhelloworld;");        Dexmaker.declare (HelloWorld, "helloworld.generated", Modifier.public, Typeid.object);        Generatehellomethod (Dexmaker, HelloWorld);        Create the Dex file and load it.        File OutputDir = new file ("."); ClassLoader loader = dexmaker.generateandload (HelloWorldMaker.class.getClassLoader (), OutputDir, OutputDir)        ;        class<?> Helloworldclass = Loader.loadclass ("HelloWorld");        Execute our newly-generated code in-process.    Helloworldclass.getmethod ("Hello"). Invoke (null); } private static void Generatehellomethod (Dexmaker dexmaker, typeid<?> declaringtype) {//Lookup some Typ        Es we ' ll need along the the the. Typeid<system> SystemType = Typeid.get (system.class);        typeid<printstream> Printstreamtype = Typeid.get (Printstream.class);        Identify the ' Hello () ' method on DeclaringType.        Methodid Hello = Declaringtype.getmethod (typeid.void, "Hello"); Declare This method on the Dexmaker.        Use the returned Code instance//As a builder, which we can append instructions to. Code code = dexmaker.declare (Hello, modifier.static |        Modifier.public); Declare all the locals we'll need up front.        The API requires this.        Local<integer> a = code.newlocal (Typeid.int);        Local<integer> B = code.newlocal (typeid.int);        local<integer> C = code.newlocal (Typeid.int);        local<string> s = code.newlocal (typeid.string);        local<printstream> localsystemout = code.newlocal (Printstreamtype);        int a = 0XABCD;        Code.loadconstant (A, 0XABCD);        int b = 0XAAAA;        Code.loadconstant (b, 0xaaaa);        int C = A-B; Code.op (BinaryoP.subtract, C, a, b);        String s = integer.tohexstring (c); Methodid<integer, string> tohexstring = Typeid.get (Integer.class). GetMethod (typeid.string, "ToHexStri        Ng ", typeid.int);        Code.invokestatic (tohexstring, S, c);        System.out.println (s);        Fieldid<system, printstream> Systemoutfield = Systemtype.getfield (Printstreamtype, "out");        Code.sget (Systemoutfield, localsystemout); Methodid<printstream, void> Printlnmethod = Printstreamtype.getmethod (typeid.void, "println", TypeId .        STRING);        Code.invokevirtual (Printlnmethod, NULL, localsystemout, s);        Return    Code.returnvoid (); }}

generateHelloMethod函数生成的函数是:
       public static void hello() {             int a = 0xabcd;             int b = 0xaaaa;             int c = a - b;            String s = Integer.toHexString(c);            System.out.println(s);            return;        }
这里很关键的是变量的声明与赋值和函数的声明与调用。例如int变量声明:
Local<Integer> a = code.newLocal(TypeId.INT);
Variable assignment:
code.loadConstant(a, 0xabcd);
function declaration:
MethodId<Integer, String> toHexString = TypeId.get(Integer.class).getMethod(TypeId.STRING, "toHexString", TypeId.INT);
Function call:
code.invokeStatic(toHexString, s, c);
The process is very simple, and to generate a complete and simple class, follow this step to complete it quickly. OK, now in the positive section, that is, using Dexmaker on the Android platform.

Android Dynamic class generates preloaded-dexmaker using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.