为什么 Android 仍将 DX 保留在其更新的替代品 D8 上?
cd cmdline-tools/build-tools/30.0.3 && ls d*
结果:
d8 dexdump dx
这是我使用的程序(文件Primes.java):
class Primes {
private static boolean isPrime(int p)
{
for (int i=2; i<p; i++) {
for (int j=2; j<p; j++) {
if (i*j == p) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
for (int i=2; i<100; i++) {
if (isPrime(i)) {
System.out.format("%d ", i);
}
}
}
}
以下是我将其编译为.dex文件的方法:
javac Primes.java
结果:
cmdline-tools/build-tools/30.0.3/dx --dex --output dx/classes.dex Primes.class
cmdline-tools/build-tools/30.0.3/d8 --output d8 Primes.class
cmdline-tools/build-tools/30.0.3/dexdump -d dx/classes.dex > dx.human
cmdline-tools/build-tools/30.0.3/dexdump -d d8/classes.dex > d8.human
然后我清理地址,注册名称和评论以便能够比较:
cat dx.human | awk "/\'main\'/,/catches/" | sed "s/.*|[0-9a-f]*: //g" | sed "s/\/\/.*//g" | sed "s/Ljava.*//g" | sed "s/v[0-9]/v/g" | sed "s/v[0-9]/v/g" > dx.main.human
cat d8.human | awk "/\'main\'/,/catches/" | sed "s/.*|[0-9a-f]*: //g" | sed "s/\/\/.*//g" | sed "s/Ljava.*//g" | sed "s/v[0-9]/v/g" | sed "s/v[0-9]/v/g" > d8.main.human
那时它们几乎相同,但不完全一样:
$ diff -u dx.main.human d8.main.human
--- dx.main.human 2021-04-12 19:49:37.110476376 +0300
+++ d8.main.human 2021-04-12 19:49:44.650101698 +0300
@@ -2,11 +2,11 @@
...
- registers : 7
+ registers : 5
...
const/4 v, #int 2
const/16 v, #int 100
if-ge v, v, 001f
@@ -14,13 +14,13 @@
move-result v
if-eqz v, 001c
sget-object v,
-const-string v, "%d " <----------------+
const/4 v, #int 1 |
new-array v, v, [ |
const/4 v, #int 0 |
invoke-static {v}, |
move-result-object v |
aput-object v, v, v |
+const-string v, "%d " <----------------+
invoke-virtual {v, v, v},
add-int/lit8 v, v, #int 1
goto 0001