Run Dalvik on X86

很早以前就想过自己编译一个X86上的Dalvik出来玩玩,尝试之后没有成功,就放下了,最近几个月来又想起来搞搞,按照/PATH/TO/AOSP/dalvik/docs/hello-world.html来编译dalvikvm,创建dex档案,设置各种环境变量之后,运行出现错误

E/dalvikvm(10105): execv '/home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/bin/dexopt' failed: No such file or directory
W/dalvikvm(10102): DexOpt: --- END 'Foo.jar' --- status=0x0100, process failed
E/dalvikvm(10102): Unable to extract+optimize DEX from 'Foo.jar'
Dalvik VM unable to locate class 'Foo'
W/dalvikvm(10102): threadid=1: thread exiting with uncaught exception (group=0xf5a1d9c8)
java.lang.NoClassDefFoundError: Foo
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "Foo" on path: DexPathList[[zip file "Foo.jar"],nativeLibraryDirectories=[/home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    ... 1 more

在网络上搜了很久没有出现过这种错误现象的,实在无赖提了个问题
在StackOverflow上,一个星期过去了,还是无人问津

最终请教了一个做Dalvik相关工作的同事,在他的帮助之下解决了问题

原来是我的ANDROID_ROOT写的有问题,可能是我没有理解好/PATH/TO/AOSP/dalvik/docs/hello-world.html的意思,也或者是它的内容比较陈旧。
后来改了就可以正常运行了,我改过的rund请参见https://gist.github.com/guohai/5048153

现在描述下主要过程
1. Download AOSP source code

2. Compile dalvik vm on Ubuntu 11.10 X64

source build/envsetup.sh
lunch 2 # choose full_x86-eng, because we want to run it on X86 directly
make dalvikvm core dexopt ext framework android.policy services

3. Compile Java code and package it to dex.

4. Run & enjoy it.

guohai@KNIGHT:~/dev/src/android/git/aosp$ ./rund -cp Foo.jar Foo
I/dalvikvm( 3473): DexOpt: mismatch dep name: '/home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex'
E/dalvikvm( 3473): /home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/framework/ext.jar odex has stale dependencies
I/dalvikvm( 3473): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory
I/dalvikvm( 3473): DexOpt: mismatch dep name: '/home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex'
E/dalvikvm( 3473): /home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/framework/framework.jar odex has stale dependencies
I/dalvikvm( 3473): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory
I/dalvikvm( 3473): DexOpt: mismatch dep name: '/home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex'
E/dalvikvm( 3473): /home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/framework/android.policy.jar odex has stale dependencies
I/dalvikvm( 3473): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory
I/dalvikvm( 3473): DexOpt: mismatch dep name: '/home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/framework/core.odex' vs. '/system/framework/core.odex'
E/dalvikvm( 3473): /home/guohai/dev/src/android/git/aosp/out/target/product/generic_x86/system/framework/services.jar odex has stale dependencies
I/dalvikvm( 3473): Zip is good, but no classes.dex inside, and no valid .odex file in the same directory
I/dalvikvm( 3473): DexOpt: source file mod time mismatch (4244043d vs 425baf6a)
Hello, Dalvik!

一些对我有些帮助的资料
http://stackoverflow.com/questions/6146983/helloworld-cannot-run-under-dalvikvm
http://stackoverflow.com/questions/11773506/how-to-launch-jar-with-exec-app-process-on-android-ics
http://blog.csdn.net/johnnynuaa/article/details/6543425

附上自己无法运行的启动脚本

#!/bin/sh

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

# configure root dir of interesting stuff
root=$base/out/target/product/generic_x86/system
export ANDROID_ROOT=$root #为什么这里不能指到target的路径?

export LD_LIBRARY_PATH=$root/lib:$LD_LIBRARY_PATH

# 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 $base/out/host/linux-x86/bin/dalvikvm -Xdexopt:none $@