【问题标题】:How to implement a Java compiler and DEX converter into an Android app?如何在 Android 应用中实现 Java 编译器和 DEX 转换器?
【发布时间】:2012-06-27 09:44:39
【问题描述】:

在尝试寻找Android Jasper Reporting 的答案时,我发现还有另外两个问题需要回答,我被要求作为问题而不是作为答案提出;):

我现在的问题是:“是否有任何编译器可以直接在设备上使用”以及“如何在不生根设备的情况下执行这些编译器。 如果有人能给我一个提示,我将不胜感激......


我在这种方法上看了一点时间,发现可以直接在没有 root 的 Android 设备上创建 APK 的应用程序:

看起来他们正在使用来自 eclipse 的编译器和一个移植的 dex 转换器。现在我正试图弄清楚如何做同样的事情。

当然:获取源代码并查看它。但是,虽然我在连接服务器并尝试解决它时遇到了奇怪的问题,但我还是按照请求在这里提出了这个问题。希望既能帮助别人,也能得到自己的答案;)


我从我的 indigo 的插件目录中取出了 org.eclipse.jdt.core_3.7.3.v20120119-1537.jar 并尝试了以下代码:

     org.eclipse.jdt.internal.compiler.batch.Main ecjMain = new org.eclipse.jdt.internal.compiler.batch.Main(new PrintWriter(System.out), new PrintWriter(System.err), false/*noSystemExit*/, null, progress);
     System.err.println("compiling...");
     ecjMain.compile(new String[] {"-classpath", "/system/framework", storage.getAbsolutePath()+"/Test.java"});
     ecjMain.compile(new String[] {storage.getAbsolutePath()+"/Test.java"});
     System.err.println("compile succeeded!!!");

有时会抛出异常,即找不到 java.lang.Object,有时它会在以 100% 的使用率加热我的处理器时卡住什么也不做...... ...

此时我无法弄清楚发生了什么以及为什么。由于我还有其他工作要做,所以这部分需要稍等。

【问题讨论】:

标签: java android dex


【解决方案1】:

在从JavaIDEdroid 的源代码中获得灵感并意识到自己很愚蠢后,我成功了(有一段时间我尝试将编译器与设备上的 dexified 框架类一起使用 - 这自然无法正常工作)。
在我成功使用 sdcard 上的 ADTs android-jar 的副本编译我的 Test.java 后,我只需要使用 DexClassLoader 加载类。
在告知自己如何做到这一点时,我发现了这篇不错的文章Custom Class Loading in Dalvik ,它至少启发了我编写这段代码:

    File storage = getDir("all41", Context.MODE_PRIVATE);


    System.err.println("copying the android.jar from asssets to the internal storage to make it available to the compiler");
    BufferedInputStream bis = null;
    OutputStream dexWriter = null;
    int BUF_SIZE = 8 * 1024;
    try {
          bis = new BufferedInputStream(getAssets().open("android.jar"));
          dexWriter = new BufferedOutputStream(
              new FileOutputStream(storage.getAbsolutePath() + "/android.jar"));
          byte[] buf = new byte[BUF_SIZE];
          int len;
          while((len = bis.read(buf, 0, BUF_SIZE)) > 0) {
              dexWriter.write(buf, 0, len);
          }
          dexWriter.close();
          bis.close();

    } catch (Exception e) {
        System.err.println("Error while copying from assets: " + e.getMessage());
        e.printStackTrace();
    }


    System.err.println("instantiating the compiler and compiling the java file"); 
    org.eclipse.jdt.internal.compiler.batch.Main ecjMain = new org.eclipse.jdt.internal.compiler.batch.Main(new PrintWriter(System.out), new PrintWriter(System.err), false/*noSystemExit*/, null);
    ecjMain.compile(new String[] {"-classpath", storage.getAbsolutePath()+"/android.jar", Environment.getExternalStorageDirectory().getAbsolutePath() + "/Test.java"});


    System.err.println("calling DEX and dexifying the test class"); 
    com.android.dx.command.Main.main(new String[] {"--dex", "--output=" + storage.getAbsolutePath() + "/Test.zip", Environment.getExternalStorageDirectory().getAbsolutePath() + "/./Test.class"});


    System.err.println("instantiating DexClassLoader, loading class and invoking toString()");
    DexClassLoader cl = new DexClassLoader(storage.getAbsolutePath() + "/Test.zip", storage.getAbsolutePath(), null, getClassLoader());
    try {
        Class libProviderClazz = cl.loadClass("Test");
        Object instance = libProviderClazz.newInstance();
        System.err.println(instance.toString());
    } catch (Exception e) {
        System.err.println("Error while instanciating object: " + e.getMessage());
        e.printStackTrace();
    }

Test.java 只包含一个方法:

public String toString() {
    return "Hallo Welt!";
}

要使其运行,您需要 jars jdt-compiler-x.x.x.jar(位于 eclipse 的 plugins 目录中)和 dx.jar(位于 Android SDK 的 platform-tools/lib 目录中)


并不难 ;)
现在我将找出要更改 JasperReports 源的哪些内容,以使其在我们心爱的 Android 设备上运行:D

【讨论】:

  • 你能说一下你是如何在android终端中运行dex文件的吗?是否编写了自己的终端?
  • @BRUCE 抱歉,8 年后我不记得具体细节了:/ 我什至认为这对您没有帮助,版本应该与那些日子有很大不同。但是,如果您有特定的问题或错误,请随时为这些问题提出问题:)
猜你喜欢
  • 2018-01-20
  • 1970-01-01
  • 2011-10-25
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
  • 2016-12-26
相关资源
最近更新 更多