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


while trying find answer android jasper reporting found out there 2 other questions answered therefor, been asked ask question, not answer ;):

my questions now: "is there compiler use directly on device" , "how execute such without rooting device. if give me hint appreciate it...


i looked little time forward on approach, , found apps makes possible create apks directly on android device not rooted:

looks they're using compiler eclipse , ported dex converter. i'm trying figure out how same.

sure: source code , it. while i'm having curious problems connection servers , trying solve it, follow plea ask question here. hoping both others , getting answer myself ;)


i took org.eclipse.jdt.core_3.7.3.v20120119-1537.jar plugin directory of indigo , 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 exception thrown java.lang.object not found , othertimes stuck doing nothing while heating processor 100% usage ... ...

at time not figure out happening , why. , in cause have other work part has wait little.

i succeeded after taking inspiration source of javaidedroid , realizing i'm dumb (for time tried uses compiler dexified framework classes on device - naturtally not work).
after succeeded compiling test.java copy of adts android-jar on sdcard had load classes dexclassloader.
while informing myselft how found nice article custom class loading in dalvik inspired me @ least write piece of code:

    file storage = getdir("all41", context.mode_private);       system.err.println("copying android.jar asssets internal storage make available 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 assets: " + e.getmessage());         e.printstacktrace();     }       system.err.println("instantiating compiler , compiling 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 , dexifying 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 , 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 contains 1 method:

public string tostring() {     return "hallo welt!"; } 

to running need jars jdt-compiler-x.x.x.jar (found in plugins directory of eclipse) , dx.jar (found in directory platform-tools/lib of android sdk)


not hard ;)
and find out change in source of jasperreports work on our beloved android devices :d


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -