构建 Android Archive (AAR) 文件时,不支持直接引用本地的 .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 :CyberWinFairyallianceAnd project caused this error: C:\CyberWin\android_lib\cyber_libs_wlzc\serialport-2.1.aar

这个错误提示表明在构建 Android Archive (AAR) 文件时,不支持直接引用本地的 .aar 文件作为依赖项。因为如果这样做,这些本地 .aar 文件中的类和 Android 资源不会被打包到最终生成的 AAR 中,从而导致生成的 AAR 文件损坏。

解决方案

以下是几种可以解决这个问题的方法:

1. 将 .aar 文件发布到本地 Maven 仓库

你可以将本地的 .aar 文件发布到本地的 Maven 仓库,然后通过 Maven 坐标来引用它。

步骤如下:

  • 在项目根目录的 build.gradle 中添加 maven-publish 插件:

收起

groovy

复制代码
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4' // 根据你的实际版本调整
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        mavenLocal() // 添加这一行以支持本地 Maven 仓库
    }
}
  • 创建一个 publish.gradle 文件(例如在项目根目录下),用于发布 .aar 文件到本地 Maven 仓库:

收起

groovy

复制代码
apply plugin: 'maven-publish'

def artifactGroupId = 'com.example' // 修改为你自己的 Group ID
def artifactArtifactId = 'serialport' // 修改为你自己的 Artifact ID
def artifactVersion = '2.1' // 修改为你自己的版本号

task publishLocalAar(type: PublishToMavenLocal) {
    publishing.publications {
        aar(MavenPublication) {
            groupId artifactGroupId
            artifactId artifactArtifactId
            version artifactVersion
            artifact("$rootDir/CyberWin/android_lib/cyber_libs_wlzc/serialport-2.1.aar")
        }
    }
}
  • 在项目根目录的 build.gradle 中应用 publish.gradle:

收起

groovy

复制代码
apply from: 'publish.gradle'
  • 运行 publishLocalAar 任务来发布 .aar 文件到本地 Maven 仓库:

收起

sh

复制代码
./gradlew publishLocalAar
  • 在你的项目的 build.gradle 中通过 Maven 坐标引用这个 .aar 文件:

收起

groovy

复制代码
dependencies {
    implementation 'com.example:serialport:2.1' // 根据你自己的 Group ID、Artifact ID 和版本号调整
}
2. 将 .aar 文件解压并作为模块导入

你可以将 .aar 文件解压,然后将其作为一个模块导入到你的项目中。

步骤如下:

  • 解压 .aar 文件:

    • 将 serialport-2.1.aar 文件重命名为 serialport-2.1.zip。
    • 解压这个 zip 文件到一个目录,例如 serialport-2.1。
  • 在 Android Studio 中导入这个模块:

    • 在 Android Studio 中,选择 File -> New -> Import Module。
    • 选择解压后的 serialport-2.1 目录,然后按照向导完成导入。
  • 在你的项目的 build.gradle 中引用这个模块:

收起

相关推荐
9624566 小时前
从“一键创建 ECS”到完整恢复状态机:ACK 抢占式节点恢复 Android 工具升级复盘
android
LCD液晶屏9 小时前
京瓷液晶屏停产替代:杭州立煌科技工业 LCD 解决方案
android·科技
2501_9327502610 小时前
Android 自定义 View 入门
android
事圆则缓13 小时前
Android 开启线程的几种方式:Thread、线程池、HandlerThread 与协程
android
半摆烂日常1 天前
低代码平台API对接实践:接口鉴权与数据同步的完整实现
android·低代码·rxjava
事圆则缓1 天前
volatile 使用场景全解析:它能保证什么,又解决不了什么
android
蒲公英内测分发1 天前
智能投影仪参加海外展会,未上架的 Android 配套 App 怎么让客户限时试用?
android
ii_best1 天前
按键精灵手机端开发安卓版实战:手写一个可最小化、可拖动的「运行日志悬浮窗」(附完整 源码 + 踩坑记录)
android·ios·智能手机·自动化·ai编程·按键精灵
终端安全笔记1 天前
安卓做 MDM 管控,先分清三种注册入口
android·安全·智能手机
乌萨达1 天前
手机模拟器安卓怎么用?电脑安装应用、玩手游与账号同步指南
android·智能手机·电脑·雷电模拟器