Android升級(jí)gradle 后引入aar包報(bào)錯(cuò)解決
問(wèn)題
android 在升級(jí) gradle 后,之前引入 aar 包的方式發(fā)生了變化,打包的時(shí)候會(huì)報(bào)錯(cuò)。報(bào)錯(cuò)信息大概如下:
Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR. Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error). The following direct local .aar file dependencies of the :commonlib project caused this error: /Users/projectName/libs/staticip.aar
這里記錄一下解決的過(guò)程。
環(huán)境
我這邊項(xiàng)目使用的環(huán)境信息大概如下:
- Android Studio:
Android Studio Flamingo | 2022.2.1
Build #AI-222.4459.24.2221.9862592, built on March 31, 2023
Runtime version: 17.0.6+0-17.0.6b802.4-9586694 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 13.3.1
GC: G1 Young Generation, G1 Old Generation
Memory: 3072M
Cores: 8
Metal Rendering is ON
Registry:
editor.focus.mode.color.light=415967
ide.editor.tab.selection.animation=true
external.system.auto.import.disabled=true
ide.text.editor.with.preview.show.floating.toolbar=false
ide.new.editor.tabs.vertical.borders=true
ide.balloon.shadow.size=0
editor.focus.mode.color.dark=415967
gradle.version.catalogs.dynamic.support=true
Non-Bundled Plugins:
idea.plugin.protoeditor (222.4459.16)
com.intellij.marketplace (222.4459.28)
com.mallowigi (80.1.0)
com.chrisrm.idea.MaterialThemeUI (7.14.2)
- gradle 版本:
classpath 'com.android.tools.build:gradle:7.4.2'
- aar 庫(kù)引入方式:通過(guò)直接將 aar 包放到
libs目錄的方式引入。
解決步驟
1. 新建本地庫(kù)目錄
切換到 project 模式:

在項(xiàng)目根目錄右鍵新建文件夾,名字隨便,我這里設(shè)置為:LocalRepo

2. 新建 aar 模塊
- 在
LocalRepo目錄中新建新文件夾,用于存放 aar 模塊,我這里取名為:staticip。 - 將你原來(lái)在
libs中的 aar 庫(kù)剪切到LocalRepo目錄下。 - 在
LocalRepo目錄中新建build.gradle文件,填入如下內(nèi)容:
configurations.maybeCreate("default")
artifacts.add("default", file("staticip.aar"))
記得將 staticip.aar 替換為你自己的 arr 庫(kù)名。
最后的目錄結(jié)構(gòu)如下:

3. 導(dǎo)入模塊
編輯 settings.gradle,加入如下內(nèi)容:
include ':LocalRepo:staticip'
4. 引入模塊
在你之前要引入這個(gè) arr 庫(kù)的模塊中,修改它的 build.gradle 文件:
dependencies {
implementation project(':LocalRepo:staticip')
}
5. 同步項(xiàng)目
最后別忘了 sync 一下:

總結(jié)
簡(jiǎn)單來(lái)講,這個(gè)錯(cuò)誤就是新版的 gradle 不支持直接導(dǎo)入 aar 庫(kù)造成的,通過(guò)將 aar 庫(kù)包裝為模塊的方式引入可以解決該問(wèn)題。
以上就是Android升級(jí)gradle 后引入aar包報(bào)錯(cuò)解決的詳細(xì)內(nèi)容,更多關(guān)于Android升級(jí)gradle引入aar包的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Material Design系列之Behavior上滑顯示返回頂部按鈕
這篇文章主要為大家詳細(xì)介紹了Material Design系列之Behavior上滑顯示返回頂部按鈕的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間(示例代碼)
這篇文章主要介紹了Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間的示例代碼,需要的朋友可以參考下2017-05-05
Android中如何實(shí)現(xiàn)清空搜索框的文字
本文主要介紹Android中實(shí)現(xiàn)清空搜索框的文字的方法。項(xiàng)目中的有關(guān)搜索的地方,加上清空文字的功能,目的是為了增加用戶體驗(yàn),使用戶刪除文本更加快捷。需要的朋友一起來(lái)看下吧2016-12-12
Android開(kāi)發(fā)手冊(cè)Button按鈕實(shí)現(xiàn)點(diǎn)擊音效
這篇文章主要為大家介紹了Android開(kāi)發(fā)手冊(cè)Button按鈕實(shí)現(xiàn)點(diǎn)擊音效示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
在Android設(shè)備上搭建Web服務(wù)器的方法
本篇文章主要介紹了在Android設(shè)備上搭建Web服務(wù)器的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Android 詳解自定義圓角輸入框和按鈕的實(shí)現(xiàn)流程
對(duì)于安卓程序員來(lái)說(shuō),自定義view簡(jiǎn)直不要太重要,畢竟有很多功能,譬如圓形頭像這些,用單純的原生非常難以實(shí)現(xiàn),而用自定義view,簡(jiǎn)直分分鐘,今天我們來(lái)實(shí)現(xiàn)自定義圓角輸入框和按鈕,大家可以跟著練習(xí),掌握技巧2021-11-11
android原生實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能
這篇文章主要為大家詳細(xì)介紹了android原生實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
解決webview調(diào)用goBack()返回上一頁(yè)自動(dòng)刷新閃白的情況
本文主要介紹了解決webview調(diào)用goBack()返回上一頁(yè)自動(dòng)刷新閃白的情況。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
Android實(shí)現(xiàn)從相冊(cè)選擇照片功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)從相冊(cè)選擇照片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

