Android项目依赖配置说明(compile, apk or provided)

节选自https://developer.android.google.cn/studio/build/dependencies.html#library_dependency_configurations

Library dependency configurations

Inside the dependencies block, you can declare a library dependency using one of several different dependency configurations (such as compileshown above). Each dependency configuration provides Gradle different instructions about how to use the library. The following list describes each of the configurations you can use for a library dependency in your Android project.

Note: Although the Java Plugin for Gradle offers dependency configurations that are similar to those defined below, you cannot use them in your Android projects—only the following configurations are compatible with the Android plugin for Gradle.

compile(编译时依赖、打包到apk)
Gradle adds the dependency to the compilation classpath and to the APK.

apk(编译时不依赖、打包到apk)
Gradle adds the dependency to the APK only (it is not added to the compilation classpath).

provided(编译时依赖、不打包到apk)
Gradle adds the dependency to the compilation classpath only (it is not added to the APK). This is useful when you’re creating an Android library module and you need the dependency during compilation, but it’s optional to have present at runtime. That is, if you use this configuration, then your library module must include a runtime condition to check whether the dependency is available, and then gracefully change its behavior so it can still function if it’s not provided. This helps reduce the size of the final APK by not adding transient dependencies that aren’t critical.
You might also use this in an Android app module when your dependency is a JAR file that you need at compile-time and that you can safely assume is already available at runtime (and therefore you don’t want to copy it into your APK). Or perhaps you want to compile against the JAR specified with the provided configuration, but use the apk configuration to package a different JAR into the APK, which includes the same APIs you need at runtime.

Note: If you’re creating an Android app module, you cannot use provided for AAR dependencies, only for JARs. In an Android library module, you can use it for both JARs and AARs.