这篇文章上次修改于 299 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

背景

在使用百度地图 Android 导航 SDK 配置 Android Studio 工程环境时,有两个 .aar 文件始终没有导入成功,代码里不能识别相关类型,我的开发环境如下:

  • Android Studio

    Android Studio Arctic Fox | 2020.3.1
    Build #AI-203.7717.56.2031.7583922, built on July 27, 2021
    Runtime version: 11.0.10+0-b96-7249189 amd64
    VM: OpenJDK 64-Bit Server VM by Oracle Corporation
    Windows 10 10.0
    GC: G1 Young Generation, G1 Old Generation
    Memory: 1280M
    Cores: 6
    Registry: external.system.auto.import.disabled=true
    Non-Bundled Plugins: com.intellij.plugins.vscodekeymap
  • Gradle

    bundled 203.7717.56.2031.7583922
  • Java

    VERSION_1_8

探索

按照网上说的大多数方法,在模块的 build.gradle 文件中:

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    ...
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation (name: 'onsdk_all', ext:'aar')
    implementation (name: 'NaviTts', ext:'aar')
    ...
}

在 Gradle 同步的时候会报如下错误:

Build was configured to prefer settings repositories over project repositories but repository 'flatDir' was added by build file 'build.gradle'

柳暗花明

解决方法其实很简单,在模块 build.gradle 文件中:

dependencies {
    ...
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    implementation (name: 'onsdk_all', ext:'aar')
    implementation (name: 'NaviTts', ext:'aar')
    ...
}

不需要任何的 flatDir

线索

Adding local .aar files to my gradle build