20

Can any body please share the method to execute the dex file in android with command?

This is just to understand.

Pratik
  • 1,596
  • 2
  • 13
  • 10

1 Answers1

40

Let's say you have a the following code in file HelloWorld.java:

public class HelloWorld {
    public static void main(String[] args) {
         System.out.println("Hello World!");
    }
}

To run it on an android device:

javac HelloWorld.java
dx --dex --output=classes.dex HelloWorld.class
zip HelloWorld.zip classes.dex
adb push HelloWorld.zip /sdcard/

For GB or earlier, you should be able to simply do:

adb shell dalvikvm -cp /sdcard/HelloWorld.zip HelloWorld

For ICS+:

adb shell mkdir /sdcard/dalvik-cache
adb shell ANDROID_DATA=/sdcard dalvikvm -cp /sdcard/HelloWorld.zip HelloWorld
JesusFreke
  • 19,784
  • 5
  • 65
  • 68
  • I was trying to print android.os.Build.VERSION.SDK_INT. Fails with java.lang.UnsatisfiedLinkError. Is any of android.* available in any way? – Gena Batsyan Sep 12 '15 at 14:31
  • @GenaBatsyan - please create a new question, with more details (code snippet, stack trace, etc.). Tag it with the dex tag, and I'll see it :) – JesusFreke Sep 12 '15 at 19:07
  • Here we go :) http://stackoverflow.com/questions/32564192/error-accessing-android-api-when-running-a-java-program-directly-from-dex-archiv – Gena Batsyan Sep 14 '15 at 11:56
  • You can also use `app_process` instead of `dalvikvm` (which is going to launch ART instead of Dalvik on the newer API's) – mister_potato Jan 25 '19 at 21:15
  • even "dalvikvm" is just a legacy command that actually launches ART, since lolipop. – JesusFreke Jan 26 '19 at 01:42