android 导入aar和jar包

4,121 阅读1分钟

将 AAR 或 JAR 文件添加为依赖项

最新导入方式

按以下步骤操作:

  1. 依次选择 File > Project Structure > Dependencies
  2. 在 Declared Dependencies 标签页中,点击 ,然后在菜单中选择 Jar Dependency
  3. 在 Add Jar/Aar Dependency 对话框中,输入 AAR 或 JAR 文件的路径,然后选择要应用依赖项的配置。如果库应适用于所有配置,请选择 implementation 配置。

image.png

image.png 最终会在module的gradle文件中添加

dependencies {
    implementation files('my_path/my_lib.aar')
}

也可以通过添加jar或aar包:

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])
}

以前导入方式

1.将aar包放入libs文件夹中

2.在module的gradle文件中添加

android {
    repositories {
        flatDir {
            dirs "libs"
        }
    }
}
dependencies {
   implementation(name: "AAR包文件名(不带.aar)", ext: "aar")
}

注意:如果aar包在被依赖的module中时,需要在依赖的module的gradle文件中添加

android {
    repositories {
        flatDir {
            dirs "libs", "../被依赖的module名/libs"
        }
    }
}

否则会出现aar找不到编译异常。