[关闭]
@act262 2017-05-26T06:49:49.000000Z 字数 3004 阅读 2255

Android Build Tools dx

Android_Tools


java class 转 dex 文件
adb shell 执行java

\Sdk\build-tools\25.0.0\dx.bat,跟随SDK版本

  1. usage:
  2. dx --dex [--debug] [--verbose] [--positions=<style>] [--no-locals]
  3. [--no-optimize] [--statistics] [--[no-]optimize-list=<file>] [--no-strict]
  4. [--keep-classes] [--output=<file>] [--dump-to=<file>] [--dump-width=<n>]
  5. [--dump-method=<name>[*]] [--verbose-dump] [--no-files] [--core-library]
  6. [--num-threads=<n>] [--incremental] [--force-jumbo] [--no-warning]
  7. [--multi-dex [--main-dex-list=<file> [--minimal-main-dex]]
  8. [--input-list=<file>]
  9. [<file>.class | <file>.{zip,jar,apk} | <directory>] ...
  10. Convert a set of classfiles into a dex file, optionally embedded in a
  11. jar/zip. Output name must end with one of: .dex .jar .zip .apk or be a dir
  12. tory.
  13. Positions options: none, important, lines.
  14. --multi-dex: allows to generate several dex files if needed. This option i
  15. exclusive with --incremental, causes --num-threads to be ignored and only
  16. supports folder or archive output.
  17. --main-dex-list=<file>: <file> is a list of class file names, classes defi
  18. d by
  19. those class files are put in classes.dex.
  20. --minimal-main-dex: only classes selected by --main-dex-list are to be put
  21. n
  22. the main dex.
  23. --input-list: <file> is a list of inputs.
  24. Each line in <file> must end with one of: .class .jar .zip .apk or be a di
  25. ctory.
  26. dx --annotool --annotation=<class> [--element=<element types>]
  27. [--print=<print types>]
  28. dx --dump [--debug] [--strict] [--bytes] [--optimize]
  29. [--basic-blocks | --rop-blocks | --ssa-blocks | --dot] [--ssa-step=<step>]
  30. [--width=<n>] [<file>.class | <file>.txt] ...
  31. Dump classfiles, or transformations thereof, in a human-oriented format.
  32. dx --find-usages <file.dex> <declaring type> <member>
  33. Find references and declarations to a field or method.
  34. declaring type: a class name in internal form, like Ljava/lang/Object;
  35. member: a field or method name, like hashCode
  36. dx -J<option> ... <arguments, in one of the above forms>
  37. Pass VM-specific options to the virtual machine that runs dx.
  38. dx --version
  39. Print the version of this tool (1.12).
  40. dx --help
  41. Print this message.

常规JVM执行环境

编写一个java文件,

  1. public class JavaTest {
  2. public static void main(String[] args) {
  3. System.out.println("This is java class main method");
  4. System.out.println("print main method arguments");
  5. for (int i = 0; i < args.length; i++) {
  6. System.out.println("args[" + i + "] = " + args[i]);
  7. }
  8. }
  9. }

编译java文件,未指定编译版本和兼容版本(本地安装JDK8)

  1. javac JavaTest.java

本地运行

  1. java JavaTest

带参数运行:

  1. java JavaTest 0 1 2 3

输出结果:

  1. This is java class main method
  2. print main method arguments
  3. args[0] = 0
  4. args[1] = 1
  5. args[2] = 2
  6. args[3] = 3

转换为Android VM可执行的Dex文件

执行dx命令,把class文件转为dex文件

  1. ~\Android\Sdk\build-tools\25.0.0\dx.bat --dex --output=TestDex.dex JavaTest.class
  2. PARSE ERROR:
  3. unsupported class file version 52.0
  4. ...while parsing JavaTest.class
  5. 1 error; aborting

说明是JDK8直接编译的在这里还不能完全兼容使用,所以需要对JDK编译时做兼容

  1. javac -target 1.7 -source 1.7 JavaTest.java

这里指定JDK编译兼容1.7(Java 7)
再次使用dx命令即可生成TestDex.dex文件

dex文件push到手机任意地方

  1. adb push TestDex.dex /sdcard

执行adb shell命令

  1. adb shell app_process -Djava.class.path=/sdcard/TestDex.dex /sdcard JavaTest

app_process也就AppRuntime,对应的源码在
~/platform/framework/base/cmds/app_process/app_main.cpp

  1. static void app_usage()
  2. {
  3. fprintf(stderr,
  4. "Usage: app_process [java-options] cmd-dir start-class-name [options]\n");
  5. }

可参考pm命令的使用

  1. base=/system
  2. export CLASSPATH=$base/framework/pm.jar
  3. exec app_process $base/bin com.android.commands.pm.Pm "$@"
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注