1、使用调试器
你可以通过服从jdwp的调试器来调试独立的应用,有两种基本方法。
一种是通过TCP,一种是通过DDMS。(CR:唔,前面看过了)
2、桌面编译
dalvik vm也可以直接在桌面使用,事实上这更复杂,因为你没有建立环境的一些东西,本地库代码被用于支持核心dalvik库。
首先:
. build/envsetup.sh
lunch sim-eng
你可以看到
============================================
TARGET_PRODUCT=sim
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=true
TARGET_BUILD_TYPE=debug
TARGET_ARCH=x86
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=
============================================
这引导你为桌面vm进行编译,编译是基于glibc的。该模式仅仅为实验用,或许将来会更有用。
你可能看到TARGET_BUILD_TYPE=debug或者 = release或者什么都没有,你只要改变lunch命令的参数就可以。
其次,编译:
make
当完成后,在桌面运行dalvik:
% dalvikvm
E/dalvikvm(19521): ERROR: must specify non-'.' bootclasspath
W/dalvikvm(19521): JNI_CreateJavaVM failed
Dalvik VM init failed (check log file)
为了运行,你必须指定指定引导程序的路径,指定放解压jar文件后dex数据的空间。可以这样做:
#!/bin/sh
# base directory, at top of source tree; replace with absolute path
base=`pwd`
# configure root dir of interesting stuff
root=$base/out/debug/host/linux-x86/product/sim/system
export ANDROID_ROOT=$root
# configure bootclasspath
bootpath=$root/framework
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 $@
准备dx的方式和前面一行:
% cat > Foo.java
class Foo { public static void main(String[] args) {
System.out.println("Hello, world");
} }
(ctrl-D)
% javac Foo.java
% dx --dex --output=foo.jar Foo.class
% ./rund -cp foo.jar Foo
Hello, world
你可以获得参数的帮助通过以下的命令:
% ./rund -help
这也可以显示vm可用选项参数。模拟“调试”环境有完整的additional assertion,使能检测功能(导致了vm变慢),但是也因此能测试。
上述所有都是基于x86的,其他的架构还要考虑porting工作,如果libffi支持你的系统,工作量会比较小。
===============================CUT==============================================
source:http://www.netmite.com/android/mydroid/2.0/dalvik/docs/hello-world.html
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:
% echo 'class Foo {'\
> 'public static void main(String[] args) {'\
> 'System.out.println("Hello, world"); }}' > Foo.java
% javac Foo.java
% dx --dex --output=foo.jar Foo.class
% adb push foo.jar /sdcard
% adb shell dalvikvm -cp /sdcard/foo.jar Foo
Hello, world
The -cp option sets the classpath. The initial directory for adb shell may not be what you expect it to be, so it's usually best to specify absolute pathnames.
The dx command accepts lists of individual class files, directories, or Jar archives. When the --output filename ends with .jar, .zip, or .apk, a file called classes.dex is created and stored inside the archive.
Run adb shell dalvikvm -help to see a list of command-line options.
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:
-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=y
This tells the VM to wait for a debugger to connect to it on TCP