@act262
2017-05-26T06:49:49.000000Z
字数 3004
阅读 2413
Android_Tools
java class 转 dex 文件
adb shell 执行java
在\Sdk\build-tools\25.0.0\dx.bat
,跟随SDK版本
usage:
dx --dex [--debug] [--verbose] [--positions=<style>] [--no-locals]
[--no-optimize] [--statistics] [--[no-]optimize-list=<file>] [--no-strict]
[--keep-classes] [--output=<file>] [--dump-to=<file>] [--dump-width=<n>]
[--dump-method=<name>[*]] [--verbose-dump] [--no-files] [--core-library]
[--num-threads=<n>] [--incremental] [--force-jumbo] [--no-warning]
[--multi-dex [--main-dex-list=<file> [--minimal-main-dex]]
[--input-list=<file>]
[<file>.class | <file>.{zip,jar,apk} | <directory>] ...
Convert a set of classfiles into a dex file, optionally embedded in a
jar/zip. Output name must end with one of: .dex .jar .zip .apk or be a dir
tory.
Positions options: none, important, lines.
--multi-dex: allows to generate several dex files if needed. This option i
exclusive with --incremental, causes --num-threads to be ignored and only
supports folder or archive output.
--main-dex-list=<file>: <file> is a list of class file names, classes defi
d by
those class files are put in classes.dex.
--minimal-main-dex: only classes selected by --main-dex-list are to be put
n
the main dex.
--input-list: <file> is a list of inputs.
Each line in <file> must end with one of: .class .jar .zip .apk or be a di
ctory.
dx --annotool --annotation=<class> [--element=<element types>]
[--print=<print types>]
dx --dump [--debug] [--strict] [--bytes] [--optimize]
[--basic-blocks | --rop-blocks | --ssa-blocks | --dot] [--ssa-step=<step>]
[--width=<n>] [<file>.class | <file>.txt] ...
Dump classfiles, or transformations thereof, in a human-oriented format.
dx --find-usages <file.dex> <declaring type> <member>
Find references and declarations to a field or method.
declaring type: a class name in internal form, like Ljava/lang/Object;
member: a field or method name, like hashCode
dx -J<option> ... <arguments, in one of the above forms>
Pass VM-specific options to the virtual machine that runs dx.
dx --version
Print the version of this tool (1.12).
dx --help
Print this message.
编写一个java文件,
public class JavaTest {
public static void main(String[] args) {
System.out.println("This is java class main method");
System.out.println("print main method arguments");
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "] = " + args[i]);
}
}
}
编译java文件,未指定编译版本和兼容版本(本地安装JDK8)
javac JavaTest.java
本地运行
java JavaTest
带参数运行:
java JavaTest 0 1 2 3
输出结果:
This is java class main method
print main method arguments
args[0] = 0
args[1] = 1
args[2] = 2
args[3] = 3
执行dx命令,把class文件转为dex文件
~\Android\Sdk\build-tools\25.0.0\dx.bat --dex --output=TestDex.dex JavaTest.class
PARSE ERROR:
unsupported class file version 52.0
...while parsing JavaTest.class
1 error; aborting
说明是JDK8直接编译的在这里还不能完全兼容使用,所以需要对JDK编译时做兼容
javac -target 1.7 -source 1.7 JavaTest.java
这里指定JDK编译兼容1.7(Java 7)
再次使用dx命令即可生成TestDex.dex
文件
将dex
文件push到手机任意地方
adb push TestDex.dex /sdcard
执行adb shell命令
adb shell app_process -Djava.class.path=/sdcard/TestDex.dex /sdcard JavaTest
app_process也就AppRuntime,对应的源码在
~/platform/framework/base/cmds/app_process/app_main.cpp
static void app_usage()
{
fprintf(stderr,
"Usage: app_process [java-options] cmd-dir start-class-name [options]\n");
}
可参考pm命令的使用
base=/system
export CLASSPATH=$base/framework/pm.jar
exec app_process $base/bin com.android.commands.pm.Pm "$@"