2023年Android项目导入本地.aar模块失败求助(Zebra RFID SDK)
问题描述
完全不熟悉Gradle及模块导入操作,查阅过诸多教程但均因过时、与新版Android Studio不兼容而失效。当前尝试导入下载的Zebra RFID SDK(基于.aar文件),官方文档提供的导入方法在Kotlin项目中无法生效。手动导入模块时出现多种错误,按Stack Overflow某回答步骤操作后,仍出现“Failed to resolve: :RFIDAPI3Library”错误。
相关配置代码
app模块build.gradle
plugins { id("com.android.application") id("org.jetbrains.kotlin.android") } android { namespace = "com.example.myapplication" compileSdk = 33 defaultConfig { applicationId = "com.example.myapplication" minSdk = 24 targetSdk = 33 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary = true } } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } buildFeatures { compose = true } composeOptions { kotlinCompilerExtensionVersion = "1.4.3" } packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } } } dependencies { implementation("androidx.core:core-ktx:1.9.0") implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2") implementation("androidx.activity:activity-compose:1.8.1") implementation(platform("androidx.compose:compose-bom:2023.03.00")) implementation("androidx.compose.ui:ui") implementation("androidx.compose.ui:ui-graphics") implementation("androidx.compose.ui:ui-tooling-preview") implementation("androidx.compose.material3:material3") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00")) androidTestImplementation("androidx.compose.ui:ui-test-junit4") debugImplementation("androidx.compose.ui:ui-tooling") debugImplementation("androidx.compose.ui:ui-test-manifest") api (":RFIDAPI3Library") }
项目根目录build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id("com.android.application") version "8.1.3" apply false id("org.jetbrains.kotlin.android") version "1.8.10" apply false }
项目结构

解决方案
针对AGP 8.x+的新版Android Studio,提供两种可靠的.aar导入方式,任选其一即可:
方式一:将aar作为独立模块导入
- 右键项目根目录 →
New→Module→ 选择Import .JAR/.AAR Package→ 点击Next - 点击
File name后的文件夹图标,选中你的RFIDAPI3Library.aar,模块名保持默认RFIDAPI3Library→ 点击Finish - 打开根目录settings.gradle,确认已自动添加:
若未自动添加,手动补全该行include(":app", ":RFIDAPI3Library") - 保持app模块build.gradle中的
api (":RFIDAPI3Library")依赖,点击右上角Sync Now同步
方式二:将aar放入libs目录直接引用
- 在app模块下创建
libs文件夹(无则新建),将RFIDAPI3Library.aar复制进去 - 在app模块的build.gradle的
android块内添加:repositories { flatDir { dirs "libs" } } - 将dependencies块中原有的
api (":RFIDAPI3Library")替换为:api(name: "RFIDAPI3Library", ext: "aar") - 点击
Sync Now同步
排查步骤
- 核对文件名、模块名大小写及拼写是否完全一致
- 执行
Build→Clean Project,再Rebuild Project - 确认Android Studio版本与AGP版本兼容(AGP 8.1.3对应Hedgehog及以上版本)
内容的提问来源于stack exchange,提问作者M.KH
相关产品推荐
相关产品推荐

