如何实现Java编译器和DEX转换器转换成一个Android应用程序?编译器、转换器、转换成、如何实现

2023-09-06 14:58:09 作者:萌一个辣妹子

在试图寻找一个答案, Android的碧玉报告我发现还有两个问题需要为此回答,我被问了问的一个问题,而不是一个答案):

While trying to find an answer to Android Jasper Reporting I found out that there are two other questions to be answered therefor, which I been asked to ask as a question, not as an answer ;):

我的问题是现在:有没有编译器直接在设备上使用和如何执行这种不生根的设备。 如果有人能够给我一个提示我真的AP preciate吧...

My questions are now: "Is there any compiler to use directly on the device" AND "how to execute such without rooting the device. If anybody could give me a hint I would really appreciate it...

我看了一点点时间向前这种方法,并发现应用程序,这使得它可以直接其中的根源并非是在Android设备上创建的APK:

I looked a little time forward on this approach, and found apps which makes it possible to create APKs directly on an Android device which is NOT rooted:

TerminalIDE - 的https://play.google.com/store/apps/details?id=com.spartacusrex.spartacuside&hl=de JavaIDEdroid - HTTP://$c$c.google.com / P / Java的IDE-机器人/ 在AIDE - https://开头的发挥。 google.com/store/apps/details?id=com.aide.ui&hl=en TerminalIDE - https://play.google.com/store/apps/details?id=com.spartacusrex.spartacuside&hl=de JavaIDEdroid - http://code.google.com/p/java-ide-droid/ AIDE - https://play.google.com/store/apps/details?id=com.aide.ui&hl=en

看起来他们正在使用的编译器日食和移植DEX转换器。现在,我试图找出如何做。

Looks like they're using the compiler from eclipse and a ported dex converter. Now I'm trying to figure out how to do the same.

肯定:获得源$ C ​​$ c和一下。但我有好奇的问题,同时获得对服务器的连接,并试图解决它,我跟着认罪问这个问题在这里。希望既能帮助别人,并同时得到一个答案呢;)

Sure: get the source code and look into it. But while I'm having curious problems to get a connection to the servers and trying to solve it, I follow the plea to ask this question here. Hoping both to help others with it and also getting an answer for myself ;)

我从我的靛蓝的插件目录中的org.eclipse.jdt.core_3.7.3.v20120119-1537.jar和尝试以下code:

I took the org.eclipse.jdt.core_3.7.3.v20120119-1537.jar from the plugin directory of my indigo and tried following code:

     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不能被发现和othertimes它坚持做什么而升温我的处理器与100%的使用率......

Sometimes the Exception was thrown that java.lang.Object could not be found and othertimes it stuck doing nothing while heating up my processor with 100% usage ... ...

在这个时候,我无法弄清楚发生了什么以及为什么。而在事业,我还有其他工作要做,这部分必须等待一点点。

At this time i could not figure out what is happening and why. And in cause that i have other work to do this part has to wait a little.

推荐答案

取灵感来自的 JavaIDEdroid 并意识到我是愚蠢的(一段时间我试图使用编译器与设备上的dexified框架类 - 这naturtally无法工作)。 之后我成功编译我的Test.java与SD卡的ADT的Andr​​oid-JAR的副本,我只是加载类与DexClassLoader。 同时通报myselft有关如何做到这一点,我发现这个好文章的自定义类加载在Dalvik的这启发了我,至少写这片code:

I succeeded after taking inspiration from source of JavaIDEdroid and realizing that I'm dumb (for a time I tried to uses the compiler with the dexified framework classes on the device - which naturtally could not work). After i succeeded compiling my Test.java with a copy of ADTs android-jar on sdcard I just had to load the classes with the DexClassLoader. While informing myselft about how to do that I found this nice article Custom Class Loading in Dalvik which inspired me at least to write this piece of code:

    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只包含一个方法:

The Test.java only contains one method:

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

要让它运行,你需要的罐子JDT编译器,xxxjar和dx.jar(在Eclipse的插件目录中找到)(目录平台工具发现/ Android SDK中的LIB)

To get it running you need the jars jdt-compiler-x.x.x.jar (found in plugins directory of eclipse) and dx.jar (found in directory platform-tools/lib of Android SDK)

不是真的很难;)现在我将找出的JasperReports源改变什么让它在我们敬爱的Andr​​oid设备的工作:D

Not really hard ;) And now I will find out what to change in source of JasperReports to get it work on our beloved Android devices :D