Autore Topic: Compilare sullo smartphone  (Letto 368 volte)

Offline dAn89

  • Nuovo arrivato
  • *
  • Post: 37
  • Respect: 0
    • Mostra profilo
    • WhiteHole - Soluzioni Informatiche
  • Sistema operativo:
    OS X
Compilare sullo smartphone
« il: 18 Aprile 2013, 17:53:23 CEST »
0
Raga per motivi di studio ho bisogno di creare un piccolo programma che compila un .java ( con un semplice Toast ).

ho trovato il seguente codice su internet

Codice (Java): [Seleziona]
 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();
    }

e Test.java

Codice (Java): [Seleziona]
public String toString() {
    return "Hallo Welt!";
}

il problema che se creo l'activity, butto quel codice nell'oncreate, importo nel progetto i vari jar jdt e dx.jar mi da il seguente errore:

Codice: [Seleziona]
04-18 12:46:40.169: W/dalvikvm(1314): VFY: unable to resolve static method 3386: Lcom/android/dx/command/Main;.main ([Ljava/lang/String;)V
04-18 12:46:40.179: D/dalvikvm(1314): VFY: replacing opcode 0x71 at 0x00af
04-18 12:46:40.648: W/System.err(1314): copying the android.jar from asssets to the internal storage to make it available to the compiler
04-18 12:46:40.648: W/System.err(1314): Error while copying from assets: android.jar
04-18 12:46:40.648: W/System.err(1314): java.io.FileNotFoundException: android.jar
04-18 12:46:40.668: W/System.err(1314):         at android.content.res.AssetManager.openAsset(Native Method)
04-18 12:46:40.668: W/System.err(1314):         at android.content.res.AssetManager.open(AssetManager.java:315)
04-18 12:46:40.668: W/System.err(1314):         at android.content.res.AssetManager.open(AssetManager.java:289)
04-18 12:46:40.668: W/System.err(1314):         at com.example.compilatore.MainActivity.onCreate(MainActivity.java:33)
04-18 12:46:40.668: W/System.err(1314):         at android.app.Activity.performCreate(Activity.java:5104)
04-18 12:46:40.668: W/System.err(1314):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-18 12:46:40.668: W/System.err(1314):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-18 12:46:40.679: W/System.err(1314):         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-18 12:46:40.679: W/System.err(1314):         at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-18 12:46:40.679: W/System.err(1314):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-18 12:46:40.679: W/System.err(1314):         at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 12:46:40.679: W/System.err(1314):         at android.os.Looper.loop(Looper.java:137)
04-18 12:46:40.688: W/System.err(1314):         at android.app.ActivityThread.main(ActivityThread.java:5041)
04-18 12:46:40.688: W/System.err(1314):         at java.lang.reflect.Method.invokeNative(Native Method)
04-18 12:46:40.688: W/System.err(1314):         at java.lang.reflect.Method.invoke(Method.java:511)
04-18 12:46:40.688: W/System.err(1314):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-18 12:46:40.688: W/System.err(1314):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-18 12:46:40.688: W/System.err(1314):         at dalvik.system.NativeStart.main(Native Method)
04-18 12:46:40.688: W/System.err(1314): instantiating the compiler and compiling the java file
04-18 12:46:40.698: W/System.err(1314): calling DEX and dexifying the test class
04-18 12:46:40.698: D/AndroidRuntime(1314): Shutting down VM
04-18 12:46:40.698: W/dalvikvm(1314): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-18 12:46:40.718: E/AndroidRuntime(1314): FATAL EXCEPTION: main
04-18 12:46:40.718: E/AndroidRuntime(1314): java.lang.NoClassDefFoundError: com.android.dx.command.Main
04-18 12:46:40.718: E/AndroidRuntime(1314):         at com.example.compilatore.MainActivity.onCreate(MainActivity.java:57)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at android.app.Activity.performCreate(Activity.java:5104)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at android.os.Looper.loop(Looper.java:137)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at android.app.ActivityThread.main(ActivityThread.java:5041)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at java.lang.reflect.Method.invokeNative(Native Method)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at java.lang.reflect.Method.invoke(Method.java:511)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-18 12:46:40.718: E/AndroidRuntime(1314):         at dalvik.system.NativeStart.main(Native Method)

mi aiutate? Non so davvero dove mettere mano :(