Android 2022.2.1 Flamingo[火烈鳥] 升級指南
Android Studio編輯器更新后會提供更多新特性的猛,支持更高版本的gradle
同時(shí)...也會帶來新的報(bào)錯(cuò)
今天這篇文章主要介紹一些老項(xiàng)目升級到火烈鳥后需要更改的配置
首先就是
Gradle全局配置
老項(xiàng)目在火烈鳥打開時(shí)首先映入眼簾的就是
非常經(jīng)典的gradle版本不匹配導(dǎo)致的錯(cuò)誤
而適應(yīng)新版本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'
在原本的基礎(chǔ)上添加了==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版本調(diào)整為適配版本即可
配置修改完成后點(diǎn)擊sync gradle同步配置即可
命名空間(namespace)
[圖片上傳失敗...(image-7d2696-1686040492793)]
sync時(shí)出現(xiàn)該錯(cuò)誤尿庐,只需要去對應(yīng)module的build.gradle中的android區(qū)塊加入namespace字段即可
...
android {
...
namespace 'xxx.xxx.xxx'
...
}
namespace中填寫的信息需與對應(yīng)module的AndroidManifest文件下的 ==package== 保持一致
配置依賴項(xiàng)
新版gradle修改了依賴項(xiàng)的導(dǎo)入方式也颤,所以老項(xiàng)目遷移過來時(shí)原有的aar包依賴都會提示找不到文件
老版本時(shí)引入libs文件夾文件我們會添加以下代碼進(jìn)行指明路徑
repositories {
flatDir {
dirs 'libs'
}
}
在新版本時(shí)直接將該代碼刪除尊惰,在對應(yīng)module的gradle下的 ==dependencies== 添加配置信息
// 依賴其他module
implementation project(":moduleName")
// 導(dǎo)入全部jar包
implementation fileTree(dir: 'libs', include: ['*.jar'])
// 導(dǎo)入全部aar包
implementation fileTree(dir: 'libs', include: ['*.aar'])
// 導(dǎo)入指定jar包
implementation files("libs/xxx.jar")
// 導(dǎo)入指定aar包
implementation files("libs/xxx.aar")
在新版gradle中如果使用了導(dǎo)入全部jar、aar包的方式拟杉,就無需再導(dǎo)入單獨(dú)的包,他會將路徑下所有包添加進(jìn)依賴
如果路徑下存在沖突的包量承,則需要使用導(dǎo)入指定依賴包的方式
Java配置
新版gradle對java版本有最低要求搬设,推薦使用JDK17版本
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
開啟BuildConfig
新版gradle默認(rèn)不生成BuildConfig文件,如果需要使用 ==buildConfigField== 添加全局變量撕捍,則需要在對應(yīng)module的buildFeatures中開啟BuildConfig
android {
...
buildFeatures {
...
buildConfig = true
...
}
...
}
結(jié)語
完成以上配置信息拿穴,老項(xiàng)目的遷移基本就完成了,新版本的編輯器的報(bào)錯(cuò)信息詳細(xì)了許多忧风,還有一些其他細(xì)枝末節(jié)的問題導(dǎo)致編譯出錯(cuò)可以根據(jù)報(bào)錯(cuò)信息進(jìn)行調(diào)整
如果還有什么疑問可以在評論區(qū)提出默色,歡迎交流~~