当前位置: 首页> 游戏> 游戏 > 免费云电脑主机_吉林seo排名公司_产品seo标题是什么_营销网站建设推广

免费云电脑主机_吉林seo排名公司_产品seo标题是什么_营销网站建设推广

时间:2025/8/13 5:25:12来源:https://blog.csdn.net/crasowas/article/details/143031575 浏览次数:0次
免费云电脑主机_吉林seo排名公司_产品seo标题是什么_营销网站建设推广

文章目录

  • 前言
  • 开发环境
  • 问题描述
  • 问题分析
    • 1. 适配Java 21
    • 2. 适配AGP 8.0
  • 解决方案
  • 补充内容
  • 最后


前言

Android Studio版本从Koala Feature Drop升级到Ladybug,出现了一系列报错。

开发环境

  • Flutter: 3.24.3
  • Android Studio: 2024.2.1 Patch 1
  • Java: 21.0.3
  • Gradle: 7.4
  • Android Gradle Plugin (AGP): 7.3.1

问题描述

升级Android Studio版本后,Flutter项目运行到Android设备出现报错,截取部分日志如下:

Execution failed for task ':gradle:compileGroovy'.
> BUG! exception in phase 'semantic analysis' in source unit '.../flutter/packages/flutter_tools/gradle/src/main/groovy/app_plugin_loader.groovy' Unsupported class file major version 65[!] Your project's Gradle version is incompatible with the Java version that Flutter is using
for Gradle.If you recently upgraded Android Studio, consult the migration guide at                         
https://flutter.dev/to/to/java-gradle-incompatibility.    

问题分析

日志中Flutter提供了迁移指南,迁移指南中是升级AGP的版本到7.3~7.6.1之间。可是现在项目的AGP版本是符合要求的,这说明这个迁移指南的内容过时了。那怎么办呢?

首先这个问题是升级Android Studio版本导致的,这让我想起一年多前的一个问题类似的问题:Android问题记录 - Unable to make field private final java.lang.String java.io.File.path accessible(持续更新)。倒不是我记性好,而是这篇文章我一直在持续更新。

先看看是不是Android Studio升级了Java版本,这是Koala Feature Drop版本的:

screenshot1

这是Ladybug版本的:

screenshot2

很明显Java版本从17升到了21,接下来开始尝试适配。

1. 适配Java 21

根据Gradle的兼容性要求:

screenshot3

Java 21版本在Gradle 8.5才开始支持,而当前项目的Gradle版本是7.4,看来问题的源头找到了,根本原因还是版本不兼容,只不过是Android Gradle版本和Java版本不兼容

现在开始尝试升级版本解决问题,首先将Gradle版本升级到8.5,打开Android项目下的gradle/wrapper/gradle-wrapper.properties文件修改distributionUrl为:

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

升级了Gradle版本,AGP版本肯定也要升级适配,具体要升到哪个版本请看这篇文章介绍:Android问题记录 - Unable to make field private final java.lang.String java.io.File.path accessible(持续更新)。这里选择升级到8.3版本,打开Android项目下的build.gradle文件,将插件版本设置为满足兼容要求的版本:

id "com.android.application" version "8.3.0" apply false

重新运行项目,还是报错,这主要是AGP 8.0版本改动比较大,需要继续适配。

2. 适配AGP 8.0

以下报错适配只是我遇到的,也许你还遇到了其他的,建议自行查看AGP 8.0的版本说明:Android Gradle Plugin 8.0.0。
当然,你也可以尝试AGP Upgrade Assistant完成迁移,只是不太好用,在后续补充内容中有介绍。

  1. 因为多环境打包或代码中用到了BuildConfig,但是从AGP 8.0开始,默认不生成BuildConfig,所以需要手动开启
> Product Flavor 'xxx' contains custom BuildConfig fields, but the feature is disabled.To enable the feature, add the following to your module-level build.gradle:`android.buildFeatures.buildConfig true`或者类似这样的报错:
/xxx/xxx.java:20: 错误: 找不到符号
import com.android.application.BuildConfig;

按照错误提示,在app/build.gradle中增加以下内容:

android.buildFeatures.buildConfig true
android {...
}

当然,你也可以这样做:

android {buildFeatures {buildConfig = true}
}
  1. 需要在构建脚本中指定命名空间,其实就是将AndroidManifest.xml中的命名空间迁移到这里
> Could not create an instance of type com.android.build.api.variant.impl.ApplicationVariantImpl.> Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

app/build.gradle中指定命名空间:

android {namespace = "com.example.application"
}

同时移除AndroidManifest.xml文件中过时的package="pro.yaomaitong.app"
完成以上操作后,你还是可能会遇到这个报错,但是请仔细看看,那应该是其他子项目(模块)的报错,可以在Android项目下的build.gradle中增加以下内容解决:

allprojects {subprojects {afterEvaluate { project ->if (project.hasProperty('android')) {project.android {if (namespace == null) {namespace project.group}}}}}
}

解决方案

请看问题分析中的适配Java 21适配AGP 8.0

补充内容

  1. AGP Upgrade Assistant

这是官方提供的升级助手,点击Android Studio菜单栏的[Tools] -> [AGP Upgrade Assistant]唤起。从7.3.1升级到8.0.0是这样的:

screenshot4

点击Run selected steps完成迁移,迁移完成后还可以撤销迁移。是不是感觉好像还不错?这里有一个问题,如果你的项目没能被识别为Android项目(例如卡在Gradle sync now报错),点击升级助手是没反应的,Android Studio的日志是这么说的:

Unable to obtain application's Android Project

这就难受了,项目需要通过AGP升级助手迁移完成后才能识别为Android项目,但是使用升级助手的前提是能被识别为Android项目,所以这升级助手最好提前用提前迁移。至于以上迁移截图是通过新建Android测试项目得到的。

  1. Kotlin版本需不需要升级

通常是不需要的。因为是Gradle去适配Kotlin,低版本的Kotlin只会要求更低版本的Gradle,高版本的Gradle会兼容低版本的Kotlin。具体版本要求请看:兼容性说明。

最后

如果这篇文章对你有所帮助,点赞👍收藏🌟支持一下吧,谢谢~


本篇文章由@crasowas发布于CSDN。

关键字:免费云电脑主机_吉林seo排名公司_产品seo标题是什么_营销网站建设推广

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: