How to implement Java compiler and DEX converter in an Android application?

When I tried to find an answer to Android Jasper Reporting, I found out that there are two other questions to this question that I was asked to ask a question, and not as an answer;):

Now my questions are: "Is there any compiler for using the AND directly on the device, how to execute it without rooting the device. If anyone could give me a hint, I would really appreciate it ...


I approached this approach a bit and found applications that allow you to create APKs directly on an Android device that is NOT rooted:

It looks like they are using a compiler from eclipse and a portable dex converter. Now I'm trying to figure out how to do the same.

Of course: get the source code and study it. But while I am having curious problems to get a connection to the servers and try to solve it, I follow the request to ask this question here. Hoping both to help others and to get an answer for yourself;)


I took the org.eclipse.jdt.core_3.7.3.v20120119-1537.jar file from my indigo plugin directory and tried the 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 an exception was thrown due to the fact that java.lang.Object could not be found, and in other cases, it did not do anything when it heated my processor with 100% use ......

, . , , , .

+5
1

JavaIDEdroid , (- , ). , Test.java ADTs android-jar SD-, DexClassLoader.
myselft , , Custom Class Loading 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) dx.jar( -tools/lib Android SDK)


;)
, JasperReports, Android: D

+1

All Articles