找Android项目所用的Java版本

林一二2021年10月20日 17:48
Ref: stackoverflow
Keywords:
  • Java 1.2 uses major version 46
  • Java 1.3 uses major version 47
  • Java 1.4 uses major version 48
  • Java 5 uses major version 49
  • Java 6 uses major version 50
  • Java 7 uses major version 51
  • Java 8 uses major version 52 (又称为jdk 1.8)
  • Java 9 uses major version 53
  • Java 10 uses major version 54
  • Java 11 uses major version 55
  • Java 12 uses major version 56
  • Java 13 uses major version 57
  • Java 14 uses major version 58
  • Java 15 uses major version 59
  • Java 16 uses major version 60
  • Java 17 uses major version 61

自己的 version 号通过报错可以知道:

Execution failed for task ':feature:transformClassesWithModifyCodeForDebug'.
> Unsupported class file major version 61

先到项目文件夹里用系统访达的搜索框搜 .class 找到个 class 文件,注意不是 Java 文件,然后用 javap -v 来显示里面的元信息,找到 major 这一行。

cd xxxxx/build/intermediates/fat-aar/merge_classes/debug/com/bykv/vk/component/ttvideo/b
% javap -verbose ./a.class | grep "major"
  major version: 51

一开始感觉是 Gradle 的版本与 Java 冲突了,但是我通过 Android Studio 的「File-Project Structure-SDK Location」可以看出,所用的 Java SDK 是内置的 1.8 版本,和其他能正常跑的同事一样。

但是根据 gradle-wrapper.properties 里写的

distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

这个 6.5 对应的是 JDK 14,其实 JDK 18 是跑不了它的呀? Gradle Java Compatibility Matrix

不过有俩,另一个地方写的是

distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

3.3 对应的是 Java8

然而到OpenJDK历史版本下载了 JDK 14,然后放到Mac上Java安装位置这里之后:

/Library/Java/JavaVirtualMachines/

然后把 gradle JDK 设置成 JDK 14 后,还是不行,还是报 Unsupported class file major version 61,这明确地说明这个报错不是来自 gradle 了。

删除项目重装后,报了个警告,其中的 Select a JDK 点开来和上面的 Project Structure-SDK Location 一样。

Gradle sync failed: Cause: invalid type code: 00 (5 s 129 ms)

Project open_ad_sdk is using the following JDK location when running Gradle:
				/Users/xxx/Library/Java/JavaVirtualMachines/corretto-1.8.0_275/Contents/Home
				Using different JDK locations on different processes might cause Gradle to
				spawn multiple daemons, for example, by executing Gradle tasks from a terminal
				while using Android Studio.
				More info...
				Select a JDK

删除项目重新 clone 了一下,并切到之前用的分支上,重新 sync 报错:

4:14 下午	Gradle sync started

4:14 下午	Gradle sync failed: Unable to start the daemon process.
				The project uses Gradle 3.3 which is incompatible with Java 11 or newer.
				Possible solution:
				- Upgrade Gradle wrapper to 4.8 version and re-import the project
				(5 s 397 ms)

4:30 下午	Gradle sync failed: Could not initialize class com.android.sdklib.repository.AndroidSdkHandler (337 ms)

这时候我发现点了「Upgrade Gradle wrapper」之后,git 居然没有变动,说明这是一个本地的配置,那或许可以把它删了来使用适配当前 JDK 的 gradle 版本? 果然删了这个 gradle/wrapper/gradle-wrapper.properties 文件,git 也没变动,然后重新 sync 就报了一个不一样的错:

5:42 下午	Gradle sync failed: Could not find method leftShift() for arguments [fat_aar_3xtqbr3wdyl3rghsdq101olxh$_run_closure4@c38e6c2] on task ':adhost:embedLibraryResources' of type org.gradle.api.DefaultTask. (27 s 225 ms)

这个解决起来比较简单,参考穿山甲 Android SDK 编译

Code
* Java 1.2 uses major version 46
* Java 1.3 uses major version 47
* Java 1.4 uses major version 48
* Java 5 uses major version 49
* Java 6 uses major version 50
* Java 7 uses major version 51
* Java 8 uses major version 52 (又称为jdk 1.8)
* Java 9 uses major version 53
* Java 10 uses major version 54
* Java 11 uses major version 55
* Java 12 uses major version 56
* Java 13 uses major version 57
* Java 14 uses major version 58
* Java 15 uses major version 59
* Java 16 uses major version 60
* Java 17 uses major version 61

自己的 version 号通过报错可以知道:

```sh
Execution failed for task ':feature:transformClassesWithModifyCodeForDebug'.
> Unsupported class file major version 61
```

先到项目文件夹里用系统访达的搜索框搜 `.class` 找到个 class 文件,注意不是 Java 文件,然后用 javap -v 来显示里面的元信息,找到 major 这一行。

```shell
cd xxxxx/build/intermediates/fat-aar/merge_classes/debug/com/bykv/vk/component/ttvideo/b
% javap -verbose ./a.class | grep "major"
  major version: 51
```

一开始感觉是 Gradle 的版本与 Java 冲突了,但是我通过 Android Studio 的「File-Project Structure-SDK Location」可以看出,所用的 Java SDK 是内置的 1.8 版本,和其他能正常跑的同事一样。

但是根据 gradle-wrapper.properties 里写的

```env
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
```

这个 6.5 对应的是 JDK 14,其实 JDK 18 是跑不了它的呀? [[Gradle Java Compatibility Matrix|https://docs.gradle.org/current/userguide/compatibility.html]]

不过有俩,另一个地方写的是

```env
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
```

3.3 对应的是 Java8

然而到[[OpenJDK历史版本|https://jdk.java.net/archive/]]下载了 JDK 14,然后放到[[Mac上Java安装位置|https://stackoverflow.com/questions/15826202/where-is-java-installed-on-mac-os-x]]这里之后:

```
/Library/Java/JavaVirtualMachines/
```

然后把 gradle JDK 设置成 JDK 14 后,还是不行,还是报 `Unsupported class file major version 61`,这明确地说明这个报错不是来自 gradle 了。

删除项目重装后,报了个警告,其中的 Select a JDK 点开来和上面的 Project Structure-SDK Location 一样。

```
Gradle sync failed: Cause: invalid type code: 00 (5 s 129 ms)

Project open_ad_sdk is using the following JDK location when running Gradle:
				/Users/xxx/Library/Java/JavaVirtualMachines/corretto-1.8.0_275/Contents/Home
				Using different JDK locations on different processes might cause Gradle to
				spawn multiple daemons, for example, by executing Gradle tasks from a terminal
				while using Android Studio.
				More info...
				Select a JDK
```

删除项目重新 clone 了一下,并切到之前用的分支上,重新 sync 报错:

```log
4:14 下午	Gradle sync started

4:14 下午	Gradle sync failed: Unable to start the daemon process.
				The project uses Gradle 3.3 which is incompatible with Java 11 or newer.
				Possible solution:
				- Upgrade Gradle wrapper to 4.8 version and re-import the project
				(5 s 397 ms)

4:30 下午	Gradle sync failed: Could not initialize class com.android.sdklib.repository.AndroidSdkHandler (337 ms)
```

这时候我发现点了「Upgrade Gradle wrapper」之后,git 居然没有变动,说明这是一个本地的配置,那或许可以把它删了来使用适配当前 JDK 的 gradle 版本?
果然删了这个 `gradle/wrapper/gradle-wrapper.properties` 文件,git 也没变动,然后重新 sync 就报了一个不一样的错:

```log
5:42 下午	Gradle sync failed: Could not find method leftShift() for arguments [fat_aar_3xtqbr3wdyl3rghsdq101olxh$_run_closure4@c38e6c2] on task ':adhost:embedLibraryResources' of type org.gradle.api.DefaultTask. (27 s 225 ms)
```

这个解决起来比较简单,参考[[穿山甲 Android SDK 编译|https://bytedance.feishu.cn/docs/doccnnhrvvPm4xb2TPCgFuAvSSb]]