如需向您的项目添加依赖项,请在 build.gradle 文件的 dependencies 代码块中指定依赖项配置,如 implementation。
例如,应用模块的以下 build.gradle 文件包含三种不同类型的依赖项:
apply plugin: 'com.android.application'
android { ... }
dependencies {
// Dependency on a local library module
implementation project(":mylibrary")
// Dependency on local binaries
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Dependency on a remote binary
implementation 'com.example.android:app-magic:12.3'
}
本地库模块依赖项
implementation project(':mylibrary')
这声明了对一个名为“mylibrary”(此名称必须与在您的 settings.gradle
文件中使用 include:
定义的库名称相符)的 Android 库模块的依赖关系。在构建您的应用时,构建系统会编译该库模块,并将生成的编译内容打包到 APK 中。
本地二进制文件依赖项
// Dependency on local binaries
implementation fileTree(dir: 'libs', include: ['*.jar'])
Gradle 声明了对项目的 module_name/libs/ 目录中 JAR 文件的依赖关系(因为 Gradle 会读取 build.gradle 文件的相对路径)。
或者,您也可以按如下方式指定各个文件:
implementation files('libs/foo.jar', 'libs/bar.jar')
远程二进制文件依赖项
implementation 'com.example.android:app-magic:12.3'
这实际上是以下代码的简写形式:
implementation group: 'com.example.android', name: 'app-magic', version: '12.3'
这声明了对“com.example.android”命名空间组内的 12.3 版“app-magic”库的依赖关系。
依赖项配置
向注释处理器传递参数
如果需要向注释处理器传递参数,您可以使用模块构建配置中的 AnnotationProcessorOptions
代码块执行此操作。例如,如果要以键值对形式传递基元数据类型,您可以使用 argument
属性,如下所示:
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
argument "key1", "value1"
argument "key2", "value2"
}
}
}
}
使用 CommandLineArgumentProvider
可让您或注释处理器作者将增量构建属性类型注释应用于每个参数,从而提高增量构建和缓存整洁构建的正确性和性能。
例如,下面的类实现了 CommandLineArgumentProvider
并注释了处理器的每个参数。此外,此示例还使用了 Groovy 语言语法,并且直接包含在模块的 build.gradle
文件中。
class MyArgsProvider implements CommandLineArgumentProvider {
// Annotates each directory as either an input or output for the
// annotation processor.
@InputFiles
// Using this annotation helps Gradle determine which part of the file path
// should be considered during up-to-date checks.
@PathSensitive(PathSensitivity.RELATIVE)
FileCollection inputDir
@OutputDirectory
File outputDir
// The class constructor sets the paths for the input and output directories.
MyArgsProvider(FileCollection input, File output) {
inputDir = input
outputDir = output
}
// Specifies each directory as a command line argument for the processor.
// The Android plugin uses this method to pass the arguments to the
// annotation processor.
@Override
Iterable<String> asArguments() {
// Use the form '-Akey[=value]' to pass your options to the Java compiler.
["-AinputDir=${inputDir.singleFile.absolutePath}",
"-AoutputDir=${outputDir.absolutePath}"]
}
}
android {...}
创建一个实现 CommandLineArgumentProvider
的类后,您需要对其进行初始化并使用 annotationProcessorOptions.compilerArgumentProvider
属性将其传递给 Android 插件,如下所示。
// This is in your module's build.gradle file.
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
// Creates a new MyArgsProvider object, specifies the input and
// output paths for the constructor, and passes the object
// to the Android plugin.
compilerArgumentProvider new MyArgsProvider(files("input/path"),
new File("output/path"))
}
}
}
}