apache / cordova-android

Apache Cordova Android
https://cordova.apache.org/
Apache License 2.0
3.64k stars 1.54k forks source link

Cordova-Android 13.0.0 build fail with exception: "General error during semantic analysis: Unsupported class file major version 61" #1726

Closed FranGhe closed 2 days ago

FranGhe commented 1 month ago

I'm workig to update my app to Android14 (API-Level34) and as always when I have to update... it's a headache. This is my timing actions: 1) I updated my "android-targetSdkVersion" a 34 in config.xml

<preference name="android-targetSdkVersion" value="34"/> 2) I removed my old platform by "cordova platform remove android" 3) I added my new platform by "cordova platform add android" Creating Cordova project for the Android platform: Path: ../../myFolder/myApp/platforms/android Package: it.myApp Name: MyApp Activity: MainActivity Android Target SDK: android-34 Android Compile SDK: 34 Subproject Path: CordovaLib Subproject Path: app Android project created with cordova-android@13.0.0 4) I updated my cordova-plugins 5) I updated my JDK14 to JDK17, updated my JAVA_HOME and restart my laptop 6) I installed new Android Studio to Koala. 7) I opened Koala which compiled the project and downloaded Android Gradle Plugin 8.3.0 8) By Koala AGP Upgrade assistant I updated Android Gradle Plugin 8.3.0 to Gradle 8.7 9) By Koala SDK Manager I updated SDK Platforms and SDK Tools to Android API34

Than I build the project by "cordova build" and:

FAILURE: Build failed with an exception.

  • Where: Settings file '/Users/myName/myFolder/myApp/platforms/android/tools/settings.gradle'
  • What went wrong: Could not compile settings file '/Users/myName/myApp/platforms/android/tools/settings.gradle'. startup failed: General error during semantic analysis: Unsupported class file major version 61 java.lang.IllegalArgumentException: Unsupported class file major version 61 at groovyjarjarasm.asm.ClassReader.(ClassReader.java:196) at groovyjarjarasm.asm.ClassReader.(ClassReader.java:177) ... at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56) at java.base/java.lang.Thread.run(Thread.java:842) 1 error
  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
  • Get more help at https://help.gradle.org BUILD FAILED in 971ms Command failed with exit code 1: gradle -p /Users/myName/myFolder/myApp/platforms/android/tools wrapper --gradle-version 8.7

What's wrong?

breautek commented 1 month ago

The error is occurring on the gradle wrapper install which uses the system gradle.

General error during semantic analysis: Unsupported class file major version 61

Means that it doesn't support the JDK environment, major version 61 is the internal java code for JDK 17. So this leads me to believe you're running a rather old version of gradle at the system level.

What version is your system gradle? e.g. what does gradle -v print?

Most of the time system gradle doesn't matter as long as it can install the wrapper, but if it's dated enough to fail on the wrapper install, it will be time to upgrade. I recommend using the latest gradle 8.x release available, which at the time of writing, is version 8.9. Alternatively you can install 8.7 which I know is compatible with cordova-android@13. I think the absolute minimum is 8.5, due to AGP requirements.

Let me know if this helps.

FranGhe commented 1 month ago

You are (as always) very very helpful... thank you!

Yes, I needed to update my system Gradle.

I mistakenly thought that it was enough to update the Android Studio Gradle... but Cordova prepares the project for Android Studio, so it must have the necessary tools first...

So... this should be the correct planning, right? 1) Update Java to JDK17 and set Enviroment Variables (JAVA_HOME) 2) Update Gradle to 8.7 and set Enviroment Variables (PATH) 3) Update config.xml () 4) Update Cordova-Android platform (remove and add newer) 5) Update Cordova-Plugins (remove and add) 6) Update Android Studio at least to JellyFish 7) By Android Studio AGP Upgrade assistant - update Android Gradle Plugin 8.3.0 to Gradle 8.7 8) By Android Studio SDK Manager - update SDK Platforms and SDK Tools to Android API34

... than "cordova build"

breautek commented 1 month ago

By Android Studio AGP Upgrade assistant - update Android Gradle Plugin 8.3.0 to Gradle 8.7

The AGP and Gradle are two different things.

Gradle is the build system. AGP (Android Gradle Plugin) is a plugin for that build system to build android applications and kibraries. I would avoid updating the AGP, they tend to introduce breaking changes. Android Studio will prompt you to update it, but avoid doing so.

Everything else looks right, going by memory... I'm mobile atm.

Krantikc commented 1 month ago

I am using gradle 8.7 and java 17 in my docker image. I am getting following error while executing cordova build android in docker container which is using beevelop/android image.

220.8 Could not create service of type ScriptPluginFactory using BuildScopeServices.createScriptPluginFactory().

Could anyone help me. Thanks in advance

igorsantos07 commented 2 days ago

I would like to add to the Google Search results that upgrading Gradle, as pointed by Norman, did solve the completely different error message I got, which I must say is the same marked as off-topic from @Krantikc (so I guess it's not _that_off-topic and, I guess, what helped Google bring me here):

FAILURE: Build failed with an exception.

* What went wrong: Could not create service of type ScriptPluginFactory using BuildScopeServices.createScriptPluginFactory(). > Could not create service of type PluginResolutionStrategyInternal using BuildScopeServices.createPluginResolutionStrategy().

I was running Gradle 4.4.1, which worked fine with SDK 33 and cordova-android 13. When I moved to SDK 34, it complained about JDK 11:

FAILURE: Build failed with an exception.

* Where: Build file '/home/[...]/my-project-folder/platforms/android/app/build.gradle' line: 20

* What went wrong: A problem occurred evaluating project ':app'. > Failed to apply plugin 'com.android.internal.application'. > Android Gradle plugin requires Java 17 to run. You are currently using Java 11. Your current JDK is located in /usr/lib/jvm/java-11-openjdk-amd64 You can try some of the following options:

  • changing the IDE settings.
  • changing the JAVA_HOME environment variable.
  • changing org.gradle.java.home in gradle.properties.

Then I upgraded to 17 and that new error started popping up, and it went away with Gradle 8.7, which I downloaded as instructed from that releases' page, and properly placed it above the system one on my $PATH.

Also, I had to, as usual, remove and re-add the android platform, as mentioned by the OP; otherwise, I would be getting the following complaint:

WARNING:We recommend using a newer Android Gradle plugin to use compileSdk = 34

This Android Gradle plugin (7.4.2) was tested up to compileSdk = 33

Thanks, Norman! (not tagging you, since you definitely are busier enough without having to read messages where you don't need to act upon)

breautek commented 2 days ago

Closing because I don't believe there is a bug here or anything actionable by Cordova.