关键词:Android cmdline-tools 历史版本、Android cmdline-tools 最小JDK版本、JDK 对应 major version、JDK LTS 信息
由于 JDK8 是一个常用的、较低的版本,因此只需要关注 JDK8 及以上版本的运行情况。
最终结论:(在 Mac 上,基于 JDK8 测试的结果)
cmdline-tools 版本 | SdkManagerCliSettings | AndroidLocationsProvider | 最低 JDK |
---|---|---|---|
11.0 | 61 (JDK 17) | 55 (JDK 11) | 17 |
10.0 | 52 (JDK 8) | 55 (JDK 11) | 11 |
9.0 | 52 (JDK 8) | 55 (JDK 11) | 11 |
8.0 | 52 (JDK 8) | 52 (JDK 8) | 8 |
7.0 | 52 (JDK 8) | 52 (JDK 8) | 8 |
6.0 | 52 (JDK 8) | 52 (JDK 8) | 8 |
通过上表可以看出,在 cmdline-tools 内部,各个 Jar 的编译目标 JDK 版本存在不一致的情况。因此,在实际运行时,需要将所有 Jar 文件的编译目标 JDK 版本的最大值作为 cmdline-tools 运行时的最小的 JDK 版本。
Android 官网的 cmdline-tools 提供的最新版本是11.0(截止2023年10月24日),也就是说,其 Runtime 的 JDK 版本必须是 JDK17 及以上。
$ java -version
java version "1.8.0_351"
$ cd path/cmdline-tools/9.0/bin
$ ./sdkmanager --list
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/prefs/AndroidLocationsProvider has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
由上述报错信息可知,com/android/prefs/AndroidLocationsProvider
要求最低使用 version 55.0(JDK 11)。
但是其他 class (比如 lib/sdklib/libsdkmanager_lib.jar
内的 com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings
)仍旧是 version 52.0 (JDK 8)。
这里的思路是:
直接切换到cmdline-tools对应版本,然后基于 runtime JDK = 8 的环境,执行 ./sdkmanager --version
检查是否报错,如报错,查看要求的最低 version 为多少。(低于JDK8的不再验证)
验证的脚本:majorVersion.sh
使用方法:./majorVersion.sh path/cmdline-tools/11.0
#!/bin/bash
$1/bin/sdkmanager --version
$1/bin/sdkmanager --version 2>&1 | grep "class file"
jar_file=/lib/sdklib/libsdkmanager_lib.jar
echo "-> $jar_file"
# 使用 javap 命令解析 JAR 文件内指定的 class
output=$(javap -verbose -classpath "$1${jar_file}" com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings 2>&1)
#echo "$output"
# 查找主要版本号
major_version=$(echo "$output" | grep 'major version')
# 打印主要版本号
echo "--> $major_version"
##---
jar_file=/lib/common/tools.common.jar
echo
echo "-> $jar_file"
# 使用 javap 命令解析 JAR 文件内指定的 class
output=$(javap -verbose -classpath "$1${jar_file}" com.android.prefs.AndroidLocationsProvider 2>&1)
# 查找主要版本号
major_version=$(echo "$output" | grep 'major version')
# # 打印主要版本号
echo "--> $major_version"
验证结果:
# 11.0
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/sdklib/tool/sdkmanager/SdkManagerCli has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
-> /lib/sdklib/libsdkmanager_lib.jar
major version: 61
-> /lib/common/tools.common.jar
major version: 55
# 10.0
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/prefs/AndroidLocationsProvider has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
-> /lib/sdklib/libsdkmanager_lib.jar
major version: 52
-> /lib/common/tools.common.jar
major version: 55
# 9.0
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/prefs/AndroidLocationsProvider has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
-> /lib/sdklib/libsdkmanager_lib.jar
major version: 52
-> /lib/common/tools.common.jar
major version: 55
# 8.0
-> /lib/sdklib/libsdkmanager_lib.jar
major version: 52
-> /lib/common/tools.common.jar
major version: 52
官网地址:https://developer.android.com/studio
cmdline-tools 版本 | 最低 JDK | Mac | Linux | Windows |
---|---|---|---|---|
11.0(latest) | 17 | commandlinetools-mac-10406996_latest.zip | commandlinetools-linux-10406996_latest.zip | commandlinetools-win-10406996_latest.zip |
10.0 | 11 | commandlinetools-mac-9862592_latest.zip | commandlinetools-linux-9862592_latest.zip | commandlinetools-win-9862592_latest.zip |
9.0 | 11 | commandlinetools-mac-9477386_latest.zip | commandlinetools-linux-9477386_latest.zip | commandlinetools-win-9477386_latest.zip |
8.0 | 8 | commandlinetools-mac-9123335_latest.zip | commandlinetools-linux-9123335_latest.zip | commandlinetools-win-9123335_latest.zip |
下载地址的生成规则:https://dl.google.com/android/repository/文件名
Java SE 21 = 65 (0x41 hex),
Java SE 20 = 64 (0x40 hex),
Java SE 19 = 63 (0x3F hex),
Java SE 18 = 62 (0x3E hex),
(LTS) Java SE 17 = 61 (0x3D hex),
Java SE 16 = 60 (0x3C hex),
Java SE 15 = 59 (0x3B hex),
Java SE 14 = 58 (0x3A hex),
Java SE 13 = 57 (0x39 hex),
Java SE 12 = 56 (0x38 hex),
(LTS) Java SE 11 = 55 (0x37 hex),
Java SE 10 = 54 (0x36 hex),[4]
Java SE 9 = 53 (0x35 hex),[5]
(LTS) Java SE 8 = 52 (0x34 hex),
Java SE 7 = 51 (0x33 hex),
Java SE 6.0 = 50 (0x32 hex),
Java SE 5.0 = 49 (0x31 hex),
JDK 1.4 = 48 (0x30 hex),
JDK 1.3 = 47 (0x2F hex),
JDK 1.2 = 46 (0x2E hex),
JDK 1.1 = 45 (0x2D hex).
资料来源:Java class file
资料来源:
如果有什么建议或者问题可以随时联系我,共同探讨学习:
- Github: likfe
- CSDN:他叫自己Mr.张
- 掘金:cafeting
- 微博:cafeting