本地包依赖

配置 jar 包需要在 compile 中添加响应依赖。下面的代码添加了 libs 文件夹中的所有 jar 作为依赖。

  1. dependencies {
  2. compile fileTree(dir: 'libs', include: ['*.jar'])
  3. }
  4. android {
  5. ...
  6. }

注意:dependencies DSL 标签是标准 Gradle API 中的一部分,所以它不属于 android 标签。

compile 配置将被用于编译 main application。里面的所有依赖都会被添加到编译 classpath 中,同时也会被打包到最终的 APK 内。以下是添加依赖时可能用到的其他配置:

  • compile 编译主 moudle
  • androidTestCompile 编译主 moudle 的测试
  • debugCompile debug Build Type 的编译
  • releaseCompile release Build Type 的编译

因为不可能去构建没有关联任何 Build Type 的 APK,所以 APK 默认有两个(或更多)的编译配置:compile<buildtype>Compile。创建新的 Build Type 将会自动创建基于该名字的编译配置。如果 debug 版要用自定义库(为了反馈实例化的崩溃信息等),但 release 版不需要,又或者 debug、release 依赖于同一个库的不同版本时,<buildtype>Compile 会非常有用(查看 Gradle documentation 了解更多版本冲突时的处理细节)。