APKinspector / androguard / specs / dalvik / hello-world.html

The default branch has multiple heads

    <title>Basic Dalvik VM Invocation</title>

<h1>Basic Dalvik VM Invocation</h1>

On an Android device, the Dalvik virtual machine usually executes embedded
in the Android application framework.  It's also possible to run it directly,
just as you would a virtual machine on your desktop system.
After compiling your Java language sources, convert and combine the .class
files into a DEX file, and push that to the device.  Here's a simple example:

% <font color="green">echo 'class Foo {'\</font><br>
&gt; <font color="green">'public static void main(String[] args) {'\</font><br>
&gt; <font color="green">'System.out.println("Hello, world"); }}' &gt;</font><br>
% <font color="green">javac</font><br>
% <font color="green">dx --dex --output=foo.jar Foo.class</font><br>
% <font color="green">adb push foo.jar /sdcard</font><br>
% <font color="green">adb shell dalvikvm -cp /sdcard/foo.jar Foo</font><br>
Hello, world 
The <code>-cp</code> option sets the classpath.  The initial directory
for <code>adb shell</code> may not be what you expect it to be, so it's
usually best to specify absolute pathnames.

The <code>dx</code> command accepts lists of individual class files,
directories, or Jar archives.  When the <code>--output</code> filename
ends with <code>.jar</code>, <code>.zip</code>, or <code>.apk</code>,
a file called <code>classes.dex</code> is created and stored inside the
Run <code>adb shell dalvikvm -help</code> to see a list of command-line

<h2>Using a debugger</h2>

You can debug stand-alone applications with any JDWP-compliant debugger.
There are two basic approaches.
The first way is to connect directly through TCP.  Add, to the "dalvikvm"
invocation line above, an argument like:
This tells the VM to wait for a debugger to connect to it on TCP port 8000.
You need to tell adb to forward local port 8000 to device port 8000:
<code>% <font color="green">adb forward tcp:8000 tcp:8000</font></code>
and then connect to it with your favorite debugger (using <code>jdb</code>
as an example here):
<code>% <font color="green">jdb -attach localhost:8000</font></code>
When the debugger attaches, the VM will be in a suspended state.  You can
set breakpoints and then tell it to continue.

You can also connect through DDMS, like you would for an Android application.
Add, to the "dalvikvm" command line:
Note the <code>transport</code> has changed, and you no longer need to
specify a TCP port number.  When your application starts, it will appear
in DDMS, with "?" as the application name.  Select it in DDMS, and connect
to it as usual, e.g.:
<code>% <font color="green">jdb -attach localhost:8700</font></code>
Because command-line applications don't include the client-side
DDM setup, features like thread monitoring and allocation tracking will not
be available in DDMS.  It's strictly a debugger pass-through in this mode.
See <a href="debugger.html">Dalvik Debugger Support</a> for more information
about using debuggers with Dalvik.

<h2>Working with the desktop build</h2>

<!-- largely lifted from

The Dalvik VM can also be used directly on the desktop.  This is somewhat
more complicated however, because you won't have certain things set up in
your environment, and several native code libraries are required to support
the core Dalvik libs.
Start with:

  . build/
  lunch sim-eng

You should see something like:


This configures you to build for the desktop, linking against glibc.
This mode is NOT recommended for anything but experimental use.  It
may go away in the future.
You may see <code>TARGET_BUILD_TYPE=release</code> or <code>=debug</code>
or possibly nothing there at all.  You may want to replace the
<code>lunch</code> command with
<code>choosecombo Simulator debug sim eng</code>.
Build the world (add a <code>-j4</code> if you have multiple cores):


When that completes, you have a working dalvikm on your desktop

  % dalvikvm
  E/dalvikvm(19521): ERROR: must specify non-'.' bootclasspath
  W/dalvikvm(19521): JNI_CreateJavaVM failed
  Dalvik VM init failed (check log file)

To actually do something, you need to specify the bootstrap class path
and give it a place to put DEX data that it uncompresses from jar
files.  You can do that with a script like this:


# base directory, at top of source tree; replace with absolute path

# configure root dir of interesting stuff
export ANDROID_ROOT=$root

# configure bootclasspath
export BOOTCLASSPATH=$bootpath/core.jar:$bootpath/ext.jar:$bootpath/framework.jar:$bootpath/android.policy.jar:$bootpath/services.jar

# this is where we create the dalvik-cache directory; make sure it exists
export ANDROID_DATA=/tmp/dalvik_$USER
mkdir -p $ANDROID_DATA/dalvik-cache

exec dalvikvm $@

The preparation with <code>dx</code> is the same as before:

  % cat &gt;
  class Foo { public static void main(String[] args) {
    System.out.println("Hello, world");
  } }
  % javac
  % dx --dex --output=foo.jar Foo.class
  % ./rund -cp foo.jar Foo
  Hello, world

As above, you can get some info about valid arguments like this:

  % ./rund -help

This also shows what options the VM was configured with.  The sim "debug"
build has all sorts of additional assertions and checks enabled,
which slows the VM down, but since this is just for experiments it
doesn't matter.

All of the above applies to x86 Linux.  Anything else will likely
require a porting effort.  If libffi supports your system, the amount of
work required should be minor.

<address>Copyright &copy; 2009 The Android Open Source Project</address>