最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】折腾Android(java)的动态编译工具:compilation-toolbox

Android crifan 3705浏览 0评论

【背景】

折腾:

【记录】在android中编译并调用编译好的java代码

的过程中,需要折腾:

http://code.google.com/p/compilation-toolbox/

去试试,Android中是否可用此库,去实现动态编译。

【折腾过程】

1.看了其介绍:

http://code.google.com/p/compilation-toolbox/wiki/GetStarted

觉得还是做得不错的,算是蛮好用的,如果可以用的话。。。

2.从:

http://code.google.com/p/compilation-toolbox/downloads/list

下载到22KB的:

compilation-toolbox-0.3.2.jar

3.加入到android项目中:

imported compilation toolbox jar file

4.写测试代码:

import org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompiler;
import org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl;

	//http://code.google.com/p/compilation-toolbox/wiki/GetStarted
	public void testCompilationToolbox()
	{
	    JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();
	    JavaSourceCompiler.CompilationUnit compilationUnit = javaSourceCompiler.createCompilationUnit();
	    String javaSourceCode =  "package com.test.foo;\n" +
	      "public class Foo {\n" +
	      "        public static void main(String [] args) {\n" +
	      "            System.out.println(\"Hello world\");\n" +
	      "        }\n" +
	      "    }";
	    compilationUnit.addJavaSource("com.test.foo.Foo", javaSourceCode);
	    ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);
	    try {
			Class fooClass = classLoader.loadClass("com.test.foo.Foo");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

试试结果,

结果是,执行了第一行:

JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();

结果就出现一堆的输入,显示有问题:

05-07 16:43:24.349: W/dalvikvm(1574): VFY: unable to find class referenced in signature (Ljavax/tools/Diagnostic;)

05-07 16:43:24.349: I/dalvikvm(1574): Could not find method javax.tools.Diagnostic.getSource, referenced from method org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.buildDiagnosticMessage

05-07 16:43:24.349: W/dalvikvm(1574): VFY: unable to resolve interface method 7874: Ljavax/tools/Diagnostic;.getSource ()Ljava/lang/Object;

05-07 16:43:24.349: D/dalvikvm(1574): VFY: replacing opcode 0x72 at 0x0000

05-07 16:43:24.349: I/dalvikvm(1574): Could not find method javax.tools.ToolProvider.getSystemJavaCompiler, referenced from method org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.compile

05-07 16:43:24.349: W/dalvikvm(1574): VFY: unable to resolve static method 7888: Ljavax/tools/ToolProvider;.getSystemJavaCompiler ()Ljavax/tools/JavaCompiler;

05-07 16:43:24.349: D/dalvikvm(1574): VFY: replacing opcode 0x71 at 0x0000

05-07 16:43:24.349: W/dalvikvm(1574): VFY: unable to find class referenced in signature (Ljavax/tools/JavaCompiler;)

05-07 16:43:24.349: E/dalvikvm(1574): Could not find class ‘javax.tools.DiagnosticCollector’, referenced from method org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.compile

05-07 16:43:24.349: W/dalvikvm(1574): VFY: unable to resolve new-instance 1067 (Ljavax/tools/DiagnosticCollector;) in Lorg/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceCompilerImpl;

05-07 16:43:24.349: D/dalvikvm(1574): VFY: replacing opcode 0x22 at 0x0019

05-07 16:43:24.349: W/dalvikvm(1574): VFY: unable to resolve static field 3273 (CLASS) in Ljavax/tools/JavaFileObject$Kind;

05-07 16:43:24.349: D/dalvikvm(1574): VFY: replacing opcode 0x62 at 0x000f

05-07 16:43:24.349: D/dalvikvm(1574): DexOpt: unable to opt direct call 0x1ec4 at 0x1b in Lorg/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceCompilerImpl;.compile

05-07 16:43:24.349: W/dalvikvm(1574): Unable to resolve superclass of Lorg/abstractmeta/toolbox/compilation/compiler/impl/SimpleJavaFileManager; (1070)

05-07 16:43:24.349: W/dalvikvm(1574): Link of class ‘Lorg/abstractmeta/toolbox/compilation/compiler/impl/SimpleJavaFileManager;’ failed

05-07 16:43:24.349: D/dalvikvm(1574): DexOpt: unable to opt direct call 0x1f00 at 0x2a in Lorg/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceCompilerImpl;.compile

结果执行:

compilationUnit.addJavaSource("com.test.foo.Foo", javaSourceCode);

就挂了,程序就跑飞了,无法继续调试了。

5.网上搜到类似的代码:

http://compilation-toolbox.googlecode.com/svn-history/r6/trunk/src/test/java/org/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceCompilerImplTest.java

所以再去试试代码:

import org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompiler;
import org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


	//http://code.google.com/p/compilation-toolbox/wiki/GetStarted
	public void testCompilationToolbox() throws Exception 
	{
        JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();
        JavaSourceCompiler.CompilationUnit compilationUnit = javaSourceCompiler.createCompilationUnit();
        String javaSourceCode = "   " +
                "package com.test.foo;" +
                "public class Foo {\n" +
                "        public static void main(String [] args) {\n" +
                "            System.out.println(\"Hello world\");\n" +
                "        }\n" +
                "}";

        compilationUnit.addJavaSource("com.test.foo.Foo", javaSourceCode);
        ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);
        Class clazz = classLoader.loadClass("com.test.foo.Foo");
        //Assert.assertEquals(clazz.getName(), "com.test.foo.Foo");
        Object foo = clazz.newInstance();
        Method main = clazz.getMethod("main", String[].class);
        String args = null;
        main.invoke(foo, args);
        javaSourceCompiler.persistCompiledClasses(compilationUnit);
	}

看看结果,

结果现象和上面类似,第一行就出现类似错误。

然后后面再执行,就彻底挂了,全部log如下:

05-07 16:59:01.182: W/dalvikvm(2770): VFY: unable to find class referenced in signature (Ljavax/tools/Diagnostic;)

05-07 16:59:01.182: I/dalvikvm(2770): Could not find method javax.tools.Diagnostic.getSource, referenced from method org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.buildDiagnosticMessage

05-07 16:59:01.182: W/dalvikvm(2770): VFY: unable to resolve interface method 7874: Ljavax/tools/Diagnostic;.getSource ()Ljava/lang/Object;

05-07 16:59:01.182: D/dalvikvm(2770): VFY: replacing opcode 0x72 at 0x0000

05-07 16:59:01.182: I/dalvikvm(2770): Could not find method javax.tools.ToolProvider.getSystemJavaCompiler, referenced from method org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.compile

05-07 16:59:01.182: W/dalvikvm(2770): VFY: unable to resolve static method 7888: Ljavax/tools/ToolProvider;.getSystemJavaCompiler ()Ljavax/tools/JavaCompiler;

05-07 16:59:01.182: D/dalvikvm(2770): VFY: replacing opcode 0x71 at 0x0000

05-07 16:59:01.182: W/dalvikvm(2770): VFY: unable to find class referenced in signature (Ljavax/tools/JavaCompiler;)

05-07 16:59:01.182: E/dalvikvm(2770): Could not find class ‘javax.tools.DiagnosticCollector’, referenced from method org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.compile

05-07 16:59:01.182: W/dalvikvm(2770): VFY: unable to resolve new-instance 1067 (Ljavax/tools/DiagnosticCollector;) in Lorg/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceCompilerImpl;

05-07 16:59:01.182: D/dalvikvm(2770): VFY: replacing opcode 0x22 at 0x0019

05-07 16:59:01.182: W/dalvikvm(2770): VFY: unable to resolve static field 3273 (CLASS) in Ljavax/tools/JavaFileObject$Kind;

05-07 16:59:01.182: D/dalvikvm(2770): VFY: replacing opcode 0x62 at 0x000f

05-07 16:59:01.182: D/dalvikvm(2770): DexOpt: unable to opt direct call 0x1ec4 at 0x1b in Lorg/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceCompilerImpl;.compile

05-07 16:59:01.182: W/dalvikvm(2770): Unable to resolve superclass of Lorg/abstractmeta/toolbox/compilation/compiler/impl/SimpleJavaFileManager; (1070)

05-07 16:59:01.182: W/dalvikvm(2770): Link of class ‘Lorg/abstractmeta/toolbox/compilation/compiler/impl/SimpleJavaFileManager;’ failed

05-07 16:59:01.182: D/dalvikvm(2770): DexOpt: unable to opt direct call 0x1f00 at 0x2a in Lorg/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceCompilerImpl;.compile

05-07 16:59:26.583: W/dalvikvm(2770): VFY: unable to resolve static field 3277 (SOURCE_OUTPUT) in Ljavax/tools/StandardLocation;

05-07 16:59:26.583: D/dalvikvm(2770): VFY: replacing opcode 0x62 at 0x0000

05-07 16:59:26.583: W/dalvikvm(2770): Unable to resolve superclass of Lorg/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceFileObject; (1077)

05-07 16:59:26.583: W/dalvikvm(2770): Link of class ‘Lorg/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceFileObject;’ failed

05-07 16:59:26.583: D/dalvikvm(2770): DexOpt: unable to opt direct call 0x1ef4 at 0x0a in Lorg/abstractmeta/toolbox/compilation/compiler/impl/JavaSourceCompilerImpl$CompilationUnitImpl;.addJavaSource

05-07 16:59:26.583: E/dalvikvm(2770): Could not find class ‘javax.tools.JavaFileObject’, referenced from method org.abstractmeta.toolbox.compilation.compiler.registry.impl.JavaFileObjectRegistryImpl.get

05-07 16:59:26.583: W/dalvikvm(2770): VFY: unable to resolve check-cast 1076 (Ljavax/tools/JavaFileObject;) in Lorg/abstractmeta/toolbox/compilation/compiler/registry/impl/JavaFileObjectRegistryImpl;

05-07 16:59:26.583: D/dalvikvm(2770): VFY: replacing opcode 0x1f at 0x0019

05-07 16:59:26.583: E/dalvikvm(2770): Could not find class ‘javax.tools.JavaFileObject’, referenced from method org.abstractmeta.toolbox.compilation.compiler.registry.impl.JavaFileObjectRegistryImpl.get

05-07 16:59:26.583: W/dalvikvm(2770): VFY: unable to resolve check-cast 1076 (Ljavax/tools/JavaFileObject;) in Lorg/abstractmeta/toolbox/compilation/compiler/registry/impl/JavaFileObjectRegistryImpl;

05-07 16:59:26.583: D/dalvikvm(2770): VFY: replacing opcode 0x1f at 0x0006

05-07 16:59:26.583: W/dalvikvm(2770): VFY: unable to find class referenced in signature (Ljavax/tools/JavaFileObject;)

05-07 16:59:26.583: I/dalvikvm(2770): Could not find method javax.tools.JavaFileObject.toUri, referenced from method org.abstractmeta.toolbox.compilation.compiler.registry.impl.JavaFileObjectRegistryImpl.register

05-07 16:59:26.583: W/dalvikvm(2770): VFY: unable to resolve interface method 7886: Ljavax/tools/JavaFileObject;.toUri ()Ljava/net/URI;

05-07 16:59:26.583: D/dalvikvm(2770): VFY: replacing opcode 0x72 at 0x0002

05-07 16:59:33.293: D/AndroidRuntime(2770): Shutting down VM

05-07 16:59:33.293: W/dalvikvm(2770): threadid=1: thread exiting with uncaught exception (group=0xb4eb9288)

05-07 16:59:33.306: E/AndroidRuntime(2770): FATAL EXCEPTION: main

05-07 16:59:33.306: E/AndroidRuntime(2770): java.lang.NoClassDefFoundError: javax.tools.StandardLocation

05-07 16:59:33.306: E/AndroidRuntime(2770):     at org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl$CompilationUnitImpl.addJavaSource(JavaSourceCompilerImpl.java:235)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at com.mm.antlrv3demo.DDParser.testCompilationToolbox(DDParser.java:84)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at com.mm.antlrv3demo.DDParser.onCreate(DDParser.java:57)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at android.app.Activity.performCreate(Activity.java:5008)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at android.app.ActivityThread.access$600(ActivityThread.java:130)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at android.os.Handler.dispatchMessage(Handler.java:99)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at android.os.Looper.loop(Looper.java:137)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at android.app.ActivityThread.main(ActivityThread.java:4745)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at java.lang.reflect.Method.invokeNative(Native Method)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at java.lang.reflect.Method.invoke(Method.java:511)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

05-07 16:59:33.306: E/AndroidRuntime(2770):     at dalvik.system.NativeStart.main(Native Method)

 

6.另外,又去:

http://compilation-toolbox.googlecode.com/svn-history/r6/trunk/src/test/java/org/abstractmeta/toolbox/compilation/compiler/impl/

看了看,有好几个测试文件的:

  • JavaSourceCompilerImplTest.java
  • SimpleClassLoaderTest.java
  • SimpleJavaFileManagerTest.java
  • TestHelper.java
  • 转载请注明:在路上 » 【记录】折腾Android(java)的动态编译工具:compilation-toolbox

    发表我的评论
    取消评论

    表情

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址

    网友最新评论 (2)

    1. 我也遇到同样的问题,求指教
      beryl.xu10年前 (2015-01-13)回复
    2. 请问楼主这个问题解决了吗
      beryl.xu10年前 (2015-01-13)回复
    86 queries in 0.207 seconds, using 22.13MB memory