Android 2022.2.1 Flamingo[火烈鸟] 升级指南
Android Studio编辑器更新后会提供更多新特性,支持更高版本的gradle
同时...也会带来新的报错
今天这篇文章主要介绍一些老项目升级到火烈鸟后需要更改的配置
首先就是
Gradle全局配置
老项目在火烈鸟打开时首先映入眼帘的就是
非常经典的gradle版本不匹配导致的错误
而适应新版本gradle的话,我们也需要同步更改一些gradle的全局配置
老版本的配置都是写在Project层级的build.gradle文件里面
在新版本后原本build.gradle中的依赖仓库配置需要迁移到setting.gradle中,build.gradle只保留gradle版本信息
旧版setting.gradle配置如下
rootProject.name = "My Application"
include ':app'
新版setting.gradle配置如下
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Application"
include ':app'
在原本的基础上添加了==pluginManagement== 和 ==dependencyResolutionManagement==
用于取代旧版build.gradle中的 ==buildscript== 和 ==allprojects==
旧版本build.gradle配置如下
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
新版本build.gradle配置如下
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
==dependencies== 中的gradle版本信息替换为新的格式,使用 ==plugins==
原有的仓库信息迁移到setting.gradle中
最后在Project Structure中将android gradle plugin与gradle版本调整为适配版本即可
配置修改完成后点击sync gradle同步配置即可
命名空间(namespace)
[图片上传失败...(image-7d2696-1686040492793)]
sync时出现该错误,只需要去对应module的build.gradle中的android区块加入namespace字段即可
...
android {
...
namespace 'xxx.xxx.xxx'
...
}
namespace中填写的信息需与对应module的AndroidManifest文件下的 ==package== 保持一致
配置依赖项
新版gradle修改了依赖项的导入方式,所以老项目迁移过来时原有的aar包依赖都会提示找不到文件
老版本时引入libs文件夹文件我们会添加以下代码进行指明路径
repositories {
flatDir {
dirs 'libs'
}
}
在新版本时直接将该代码删除,在对应module的gradle下的 ==dependencies== 添加配置信息
// 依赖其他module
implementation project(":moduleName")
// 导入全部jar包
implementation fileTree(dir: 'libs', include: ['*.jar'])
// 导入全部aar包
implementation fileTree(dir: 'libs', include: ['*.aar'])
// 导入指定jar包
implementation files("libs/xxx.jar")
// 导入指定aar包
implementation files("libs/xxx.aar")
在新版gradle中如果使用了导入全部jar、aar包的方式,就无需再导入单独的包,他会将路径下所有包添加进依赖
如果路径下存在冲突的包,则需要使用导入指定依赖包的方式
Java配置
新版gradle对java版本有最低要求,推荐使用JDK17版本
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
开启BuildConfig
新版gradle默认不生成BuildConfig文件,如果需要使用 ==buildConfigField== 添加全局变量,则需要在对应module的buildFeatures中开启BuildConfig
android {
...
buildFeatures {
...
buildConfig = true
...
}
...
}
结语
完成以上配置信息,老项目的迁移基本就完成了,新版本的编辑器的报错信息详细了许多,还有一些其他细枝末节的问题导致编译出错可以根据报错信息进行调整
如果还有什么疑问可以在评论区提出,欢迎交流~~