Android Studio升级Gradle Plugin升级导致项目运行失败问题

文章讲述了在升级AndroidStudio后,旧项目遇到的各种运行错误,如IllegalAccessError,主要原因是Gradle、JDK和AS版本不匹配。解决方案是检查并更新项目中的Gradle版本、build.gradle配置以及JDK版本,确保它们与AndroidStudio版本兼容。作者提供了一份版本对应表,帮助开发者找到合适的搭配,并提到从低版本升级到7.0+通常可以解决问题。
摘要由CSDN通过智能技术生成

背景&错误

升级Android Studio
旧项目无法运行,奇奇怪怪什么错误都有
例如:

java.lang.IllegalAccessError: class org.gradle.api.internal.tasks.compile.processing.AggregatingProcessingStrategy (in unnamed module @0x390ea9fb) cannot access class com.sun.tools.javac.code.Symbol$ClassSymbol (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.code to unnamed module @0x390ea9fb

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':BaziPaipan_lib:compileDebugJavaWithJavac'.
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:200)
	at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:263)
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeIfValid(ExecuteActionsTaskExecuter.java:198)
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:179)
	at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:109)
	at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46)
	at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:62)
	at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57)
	at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:56)
	at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:36)
	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.executeTask(EventFiringTaskExecuter.java:77)
	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:55)
	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:52)
	at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:200)
	at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:195)
	at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:75)
	at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:68)
	at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:153)
	at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:68)
	at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:62)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor.lambda$call$2(DefaultBuildOperationExecutor.java:76)
	at org.gradle.internal.operations.UnmanagedBuildOperationWrapper.callWithUnmanagedSupport(UnmanagedBuildOperationWrapper.java:54)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:76)
	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:52)
	at org.gradle.execution.plan.LocalTaskNodeExecutor.execute(LocalTaskNodeExecutor.java:41)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:372)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:359)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:352)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:338)
	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.lambda$run$0(DefaultPlanExecutor.java:127)
	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.execute(DefaultPlanExecutor.java:191)
	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.executeNextNode(DefaultPlanExecutor.java:182)
	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:124)
	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
	at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
	at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
Caused by: java.lang.RuntimeException: java.lang.IllegalAccessError: class org.gradle.api.internal.tasks.compile.processing.AggregatingProcessingStrategy (in unnamed module @0x390ea9fb) cannot access class com.sun.tools.javac.code.Symbol$ClassSymbol (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.code to unnamed module @0x390ea9fb
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.invocationHelper(Unknown Source)
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(Unknown Source)

又例如:
在这里插入图片描述

排查解决

初步排查判断是旧项目Gradle版本问题导致。因为最近创建的项目,升级AS之后无影响,只有旧项目跑不起来。

查阅资料发现,随着AS gradle插件版本的升级,项目中gradle plugin版本、gradle版本、jdk版本等均需要配合一起升级,否则编译不过。比如类似这种报错提示:

build.gradle 中配置的gradle版本太低:

Unsupported Java. 
Your build is currently configured to use Java 17.0.6 and Gradle 6.7.1.

gradle.properties中配置的gradle版本很高,builde.gradle中配置没有匹配:

Could not resolve all dependencies for configuration ':classpath'.
   > Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven5(http://mvn.mob.com/android)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.5.1/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details. 

gradle.properties中配置的版本太低,匹配不上AS版本或者gradle版本

Unsupported Gradle. 
The project uses Gradle 2.14.1 which is incompatible with Android Studio 2022.2.

其实以上错误,基本上都是版本不匹配导致,那么,匹配上基本就能解决了。

从官网整理了一份各版本对应表:

AGP版本(build.gradle中)Gradle最低版本(gradle.properties中)最低JDK版本(项目配置)SDK Build Tools
8.18.01730.0.3
8.08.01730.0.3
7.47.51130.0.3
7.37.41130.0.3
7.27.3.31130.0.3
7.17.21130.0.3
7.07.0.21130.0.2
4.2.0+6.7.11.730.0.2
4.1.0+6.5+1.729.0.2
4.0.0+6.1.1+1.729.0.2
3.6.0 - 3.6.45.6.4+1.728.0.3
3.5.0 - 3.5.45.4.1+1.728.0.3
3.4.0 - 3.4.35.1.1+1.728.0.3
Android Studio 版本所需 AGP 版本
Giraffe | 2022.3.13.2-8.1
Flamingo | 2022.2.13.2-8.0
Electric Eel | 2022.1.13.2-7.4
Dolphin | 2021.3.13.2-7.3
Chipmunk | 2021.2.13.2-7.2
Bumblebee | 2021.1.13.2-7.1
Arctic Fox | 2020.3.13.1-7.0

方案

修改项目配置,对应上版本,很多老项目还在4.x版本,所以不兼容报错,一般升级7.0+就能解决,这也是谷歌倒推着我们开发者们往上升级,旧版的一些api慢慢都会废弃掉。

从4.x升级至7.0+的话,配置改动会大一点,主要是依赖仓库的配置、上传发布maven等日常配置,教程网上一堆,自行搜索即可。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值