Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android升级gradle引入aar包

Android升级gradle 后引入aar包报错解决

作者:雮尘

这篇文章主要为大家介绍了Android升级gradle 后引入aar包报错解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

问题

android 在升级 gradle 后,之前引入 aar 包的方式发生了变化,打包的时候会报错。报错信息大概如下:

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

这里记录一下解决的过程。

环境

我这边项目使用的环境信息大概如下:

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)
classpath 'com.android.tools.build:gradle:7.4.2'

解决步骤

1. 新建本地库目录

切换到 project 模式:

在项目根目录右键新建文件夹,名字随便,我这里设置为:LocalRepo

2. 新建 aar 模块

configurations.maybeCreate("default")  
artifacts.add("default", file("staticip.aar"))

记得将 staticip.aar 替换为你自己的 arr 库名。

最后的目录结构如下:

3. 导入模块

编辑 settings.gradle,加入如下内容:

include ':LocalRepo:staticip'

4. 引入模块

在你之前要引入这个 arr 库的模块中,修改它的 build.gradle 文件:

dependencies {
    implementation project(':LocalRepo:staticip')
}

5. 同步项目

最后别忘了 sync 一下:

总结

简单来讲,这个错误就是新版的 gradle 不支持直接导入 aar 库造成的,通过将 aar 库包装为模块的方式引入可以解决该问题。

以上就是Android升级gradle 后引入aar包报错解决的详细内容,更多关于Android升级gradle引入aar包的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文