Android 升级 gradle 后引入的 aar 包报错 您所在的位置:网站首页 gradle导入依赖后还报错 Android 升级 gradle 后引入的 aar 包报错

Android 升级 gradle 后引入的 aar 包报错

2023-04-22 07:15| 来源: 网络整理| 查看: 265

问题

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: 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 库引入方式:通过直接将 aar 包放到 libs 目录的方式引入。 解决步骤 1. 新建本地库目录

切换到 project 模式:

SMMS

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

SMMS

2. 新建 aar 模块

在 LocalRepo 目录中新建新文件夹,用于存放 aar 模块,我这里取名为:staticip。

将你原来在 libs 中的 aar 库剪切到 LocalRepo 目录下。

在 LocalRepo 目录中新建 build.gradle 文件,填入如下内容:

configurations.maybeCreate("default") artifacts.add("default", file("staticip.aar")) 复制代码

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

最后的目录结构如下:

SMMS

3. 导入模块

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

include ':LocalRepo:staticip' 复制代码 4. 引入模块

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

dependencies { implementation project(':LocalRepo:staticip') } 复制代码 5. 同步项目

最后别忘了 sync 一下:

SMMS

总结

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

❤️ 支持

如果本文对你有帮助,点赞 👍 支持下我吧,你的「赞」是我创作的动力。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有