AndroidAGP8.1.0和JDK 17迁移之旅
前言:
由于我最近写demo的直接把之前的项目从AGP4.2.2升级到8.1.0引发了一些列问题,这里记录一下,前面讲解过迁移DSL方式遇到的问题,这次升级8.1.0也比之前顺利多了,想看DSL迁移的可以参考我之前的文章:
https://blog.csdn.net/u012556114/article/details/130547815
https://juejin.cn/post/7230416597012283453
1.简介:
随着 Android Studio Flamingo 正式版的发布,AGP 8(Android Gradle Plugin 8)也正式进入咱们的视野,这次 AGP 8 相关更新归于「断代式」更新,同时假如想体验 AGP 8,就需求晋级到 Android Studio Flamingo 版别,而晋级到 Flamingo 的话,默许自带的 Java 版别就会变成 JDK 17 所以,这便是你需求适配 AGP8 的首要原因之一。
2.Flamingo 兼容
首先,如下图所示,运用 Flamingo 不一定就要用 AGP 8,它的支撑范围是 3.2- 8.0
,可是,由于 Flamingo 默许自带的 Java 版别是 JDK 17 ,所以默许情况下你最低需求 AGP 7 。
Android Gradle 插件和 Android Studio 兼容性
Android Studio 构建系统以 Gradle 为基础,并且 Android Gradle 插件 (AGP) 添加了几项专用于构建 Android 应用的功能。下表列出了各个 Android Studio 版本所需的 AGP 版本。
如果您的项目不受某个特定版本的 Android Studio 支持,您仍然可以使用旧版 Android Studio 打开和更新项目。
Android Studio 版本 | 所需的 AGP 版本 |
---|---|
Hedgehog | 2023.1.1 | 3.2-8.2 |
Giraffe | 2022.3.1 | 3.2-8.1 |
Flamingo | 2022.2.1 | 3.2-8.0 |
Electric Eel | 2022.1.1 | 3.2-7.4 |
Dolphin | 2021.3.1 | 3.2-7.3 |
支持的 Android API 级别上限
每个版本的 Android Studio 和 AGP 都有支持的 API 级别上限。如果项目所用 Android Studio 或 AGP 的 targetSdk
或 compileSdk
高于支持的最大值,则可能会导致意外问题。我们建议您使用最新的预览版 Android Studio 和 AGP 来处理以预览版 Android OS 为目标平台的项目。您可以安装 Android Studio 的预览版以及稳定版。
API 级别上限如下:
Android Studio 版本 | AGP 版本 | 支持的 API 级别上限 |
---|---|---|
Hedgehog | 2023.1.1 | 8.2 | 34 |
Giraffe | 2022.3.1 | 8.1 | 33 |
Flamingo | 2022.2.1 | 8.0 | 33 |
3.升级Gradle插件版本到8.1.0:
[versions]
agp = "8.1.0"
org-jetbrains-kotlin-android = "1.8.0"
core-ktx = "1.10.1"
junit = "4.13.2"
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"
appcompat = "1.6.1"
material = "1.9.0"
constraintlayout = "2.1.4"
glide = "4.13.0"
glide-compiler = "4.13.0"
kotlin-stdlib = "1.8.21"
kotlin-reflect = "1.8.10"
kotlin-coroutines-core = "1.7.0"
kotlin-coroutines-android = "1.7.0"
lifecycle_version = "2.2.0"
junit_version = "4.13.2"
utilcodex = "1.31.1"
mmkv = "1.3.1"
gson = "2.9.0"
xutils = "3.5.1"
subsampling-scale-image-view = "3.10.0"
4.修改App.gradle依赖:
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
alias(libs.plugins.com.android.application)
alias(libs.plugins.org.jetbrains.kotlin.android)
}
5.新建一个后缀名为.toml的文件:
6.修改BuildConfig配置:
这里配置BuildConfig有2种方式:
6.1 没有配置BuildConfig时:
运行的时候直接报错了,提示找不到BuildConfig。
6.2 在module里面添加配置:
buildFeatures{
buildConfig = true
}
配置好BuildConfig后再次运行,直接构建成功,运行起来了~~
6.3 添加全局的buildConfig配置:
在gradle.properties添加如下配置:
android.defaults.buildfeatures.buildconfig=true
7.添加第三方的依赖库:
utilcodex = "1.31.1"
mmkv = "1.3.1"
gson = "2.9.0"
xutils = "3.5.1"
subsampling-scale-image-view = "3.10.0"
配置第三方依赖库:
dependencies {
implementation (libs.appcompat)
implementation (libs.material)
implementation (libs.glide)
annotationProcessor(libs.glide.compiler)
implementation (libs.subsampling.scale.image.view)
implementation (libs.utilcodex)
implementation (libs.gson)
implementation (libs.mmkv)
implementation (libs.xutils)
}
8.实现效果如下:
9. 完整的依赖库配置如下:
[versions]
agp = “8.1.0”
org-jetbrains-kotlin-android = “1.8.0”
core-ktx = “1.10.1”
junit = “4.13.2”
androidx-test-ext-junit = “1.1.5”
espresso-core = “3.5.1”
appcompat = “1.6.1”
material = “1.9.0”
constraintlayout = “2.1.4”
glide = “4.13.0”
glide-compiler = “4.13.0”
kotlin-stdlib = “1.8.21”
kotlin-reflect = “1.8.10”
kotlin-coroutines-core = “1.7.0”
kotlin-coroutines-android = “1.7.0”
lifecycle_version = “2.2.0”
junit_version = “4.13.2”
utilcodex = “1.31.1”
mmkv = “1.3.1”
gson = “2.9.0”
xutils = “3.5.1”
subsampling-scale-image-view = “3.10.0”
[libraries]
core-ktx = { group = “androidx.core”, name = “core-ktx”, version.ref = “core-ktx” }
junit = { group = “junit”, name = “junit”, version.ref = “junit” }
androidx-test-ext-junit = { group = “androidx.test.ext”, name = “junit”, version.ref = “androidx-test-ext-junit” }
espresso-core = { group = “androidx.test.espresso”, name = “espresso-core”, version.ref = “espresso-core” }
appcompat = { group = “androidx.appcompat”, name = “appcompat”, version = “1.6.1” }
material = { group = “com.google.android.material”, name = “material”, version.ref = “material” }
constraintlayout = { group = “androidx.constraintlayout”, name = “constraintlayout”, version.ref = “constraintlayout” }
glide = {group = “com.github.bumptech.glide”,name = “glide”,version.ref = “glide”}
glide-compiler = {group = “com.github.bumptech.glide”,name = “compiler”,version.ref = “glide-compiler”}
kotlin-stdlib = {group = “org.jetbrains.kotlin”,name = “kotlin-stdlib-jdk7”,version.ref = “kotlin-stdlib”}
kotlin-reflect = {group = “org.jetbrains.kotlin”,name = “kotlin-reflect”,version.ref = “kotlin-reflect”}
kotlin-coroutines-core = {group = “org.jetbrains.kotlin”,name = “kotlinx-coroutines-core”,version.ref = “kotlin-coroutines-core”}
kotlin-kotlinx-coroutines-android = {group = “org.jetbrains.kotlin”,name = “kotlinx-coroutines-androidt”,version.ref = “kotlin-coroutines-android”}
utilcodex = {group = “com.blankj”,name = “utilcodex”,version.ref = “utilcodex”}
mmkv = {group = “com.tencent”,name = “mmkv”,version.ref = “mmkv”}
xutils = {group = “org.xutils”,name = “xutils”,version.ref = “xutils”}
gson = {group = “com.google.code.gson”,name = “gson”,version.ref = “gson”}
subsampling-scale-image-view = {group = “com.davemorrissey.labs”,name = “subsampling-scale-image-view-androidx”,version.ref = “subsampling-scale-image-view”}
[plugins]
com-android-application = { id = “com.android.application”, version.ref = “agp” }
org-jetbrains-kotlin-android = { id = “org.jetbrains.kotlin.android”, version.ref = “org-jetbrains-kotlin-android” }
[bundles]
10.总结:
今天升级到8.1.0也遇到不少问题,由于之前升级到DSL很多语法都很熟悉,所以遇到问题查找资料后很快就解决了,总结如下:
- 升级jdk版本到JDK17和AGP版本到8.1.0
- 新建一个.toml后缀的文件,添加统一依赖库配置
- 修改buildConfig打包配置
- 修改项目的build.gradle配置
- 修改App目录下的build.gradle配置
- 修改settings.gradle配置
11.项目demo的源码地址如下:
(在dev_version_catelog分支)
https://gitee.com/jackning_admin/androi-long-image-view