欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

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

来源: 开发者 投稿于  被查看 46688 次 评论:208

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


目录
  • 问题
  • 环境
  • 解决步骤
    • 1. 新建本地库目录
    • 2. 新建 aar 模块
    • 3. 导入模块
    • 4. 引入模块
    • 5. 同步项目
  • 总结

    问题

    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 模式:

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

    2. 新建 aar 模块

    • LocalRepo 目录中新建新文件夹,用于存放 aar 模块,我这里取名为:staticip
    • 将你原来在 libs 中的 aar 库剪切到 LocalRepo 目录下。
    • LocalRepo 目录中新建 build.gradle 文件,填入如下内容:
    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包的资料请关注3672js教程其它相关文章!

    您可能感兴趣的文章:
    • Android Studio gradle配置packagingOptions打包so库重复
    • Android开发之Gradle 进阶Tasks深入了解
    • Android开发gradle拉取依赖的加速配置
    • Android Gradle模块依赖替换使用技巧
    • Android Gradle同步优化详解

    用户评论