在尝试找到Android Jasper Reporting的答案时,我发现还有两个要解决的问题,我被问为一个问题,而不是一个答案;):
我的问题现在是:“是否有任何编译器可直接在设备上使用”和“如何在不root设备的情况下执行此类编译。如果有人可以给我提示,我将非常感谢...
我在这种方法上花了一点时间,发现了可以直接在没有根目录的Android设备上创建APK的应用程序:
看起来他们正在使用Eclipse中的编译器和移植的dex转换器。现在,我试图弄清楚该怎么做。
当然:获取源代码并进行研究。但是,当我遇到一些奇怪的问题来建立与服务器的连接并尝试解决该问题时,我请您在这里提出这个问题。希望既能帮助他人,又能为我自己找到答案;)
我从靛蓝的插件目录中获取了org.eclipse.jdt.core_3.7.3.v20120119-1537.jar并尝试了以下代码:
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,有时它在以100%的使用率加热我的处理器时卡住了什么……
目前,我无法确定发生了什么以及为什么。由于我还有其他工作要做,因此必须稍等一下。
我从JavaIDEdroid的源中吸取了灵感并意识到自己很愚蠢(在一段时间内,我尝试将编译器与设备上的精巧框架类一起使用-自然无法使用),从而获得了成功。
在使用sdcard上的ADT android-jar的副本成功编译Test.java之后,我只需要用DexClassLoader加载类。
在通知自己如何做的同时,我在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-xxxjar(可在eclipse的plugins目录中找到)和dx.jar(可在Android SDK的目录platform-tools / lib中找到)
并不难;)
现在,我将找出JasperReports源中的更改内容,以使其在我们钟爱的Android设备上运行:D
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句