javaandroiddex

How to implement a Java compiler and DEX converter into an Android app?


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 ;):

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...


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:

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.

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


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!!!");

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.


Solution

  • 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();
        }
    

    The Test.java only contains one method:

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

    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)


    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