From:http://hi.baidu.com/seucrcr/item/ebd1b34879a168086cc2f078

Dalvik——基本Dalvik VM调用

在android设备上,dalvik vm通常都被嵌入到android应用框架里执行,也可以直接运行vm,就像你要在你桌面系统运行虚拟机一样。
在编译完java源码后,转换并整合.class文件到.dex文件然后将它push到设备。这儿是个简单的例子:
% 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
-cp选项设置了路径。adb shel的初始路径可能并不是你要的,最好指定绝对路径。
dx命令接收了每个class的文件、目录或者jar结构列表,当--output文件名以.jar、.zip或者.apk结尾时,一个叫做classes.dex的文件就被创建并保存。
运行adb shell davlikvm -help可获得更多命令行选项。

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

Basic Dalvik VM Invocation

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-cpoption sets the classpath. The initial directory foradb shellmay not be what you expect it to be, so it's usually best to specify absolute pathnames.

Thedxcommand accepts lists of individual class files, directories, or Jar archives. When the--outputfilename ends with.jar,.zip, or.apk, a file calledclasses.dexis created and stored inside the archive.

Runadb shell dalvikvm -helpto see a list of command-line options.

Using a debugger

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 port 8000. You need to tell adb to forward local port 8000 to device port 8000:

% adb forward tcp:8000 tcp:8000

and then connect to it with your favorite debugger (usingjdbas an example here):

% jdb -attach localhost:8000

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:

-agentlib:jdwp=transport=dt_android_adb,suspend=y,server=y

Note thetransporthas 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.:

% jdb -attach localhost:8700

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.

SeeDalvik Debugger Supportfor more information about using debuggers with Dalvik.

Working with the desktop build

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/envsetup.sh lunch sim-eng

You should see something like:

============================================ 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= ============================================

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 seeTARGET_BUILD_TYPE=releaseor=debugor possibly nothing there at all. You may want to replace thelunchcommand withchoosecombo Simulator debug sim eng.

Build the world (add a-j4if you have multiple cores):

make

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

% 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:

#!/bin/sh# base directory, at top of source tree; replace with absolute pathbase=`pwd`# configure root dir of interesting stuffroot=$base/out/debug/host/linux-x86/product/sim/systemexport ANDROID_ROOT=$root# configure bootclasspathbootpath=$root/frameworkexport 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 existsexport ANDROID_DATA=/tmp/dalvik_$USERmkdir -p $ANDROID_DATA/dalvik-cacheexec dalvikvm $@

The preparation withdxis the same as before:

% 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

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.

Copyright © 2009 The Android Open Source Project

更多相关文章

  1. Android(安卓)的 ApkTool 反编译 apk xml 和去广告
  2. 解决 Android(安卓)N requires the IDE to be running with Java
  3. 【安卓学习之常见问题】 编译V7问题- 'android:Widget.Material.
  4. android gradle 4.1以上依赖的改变:compile->api和implementation
  5. 描述清点击 Android(安卓)Studio 的 build 按钮后发生了什么
  6. Ubuntu 10.04(64位)下载并编译 Android(安卓)2.2 源码[只有11条命
  7. Android(安卓)反编译工具的各种用法
  8. 64位ubuntu16.04安装Android(安卓)Studio
  9. 庆祝一下,Android视频采集+H264编码成功

随机推荐

  1. Android(安卓)TabActivity中的子Tab Acti
  2. Android(安卓)Studio JNI开发入门教程
  3. Android Q访问公共外部存储受限
  4. Android(安卓)运行时改变 Drawable 的填
  5. android Frame动画基础
  6. Android(安卓)Dex文件格式(一)
  7. Android Studio 检测不到 Genymotion 模
  8. 设置背景图时防止图片拉伸的解决方法
  9. 调试过程中的堆栈打印
  10. Android(安卓)Kotlin(2)之《函数和Lambda表