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

后端 未结 1 778
日久生厌
日久生厌 2021-02-06 07:47

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 a

相关标签:
1条回答
  • 2021-02-06 08:03

    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

    0 讨论(0)
提交回复
热议问题