While adding new dependencies to android project especially in Android Studio
in Dependencies
there are three scope options Compile/Provided/APK.
What are the effects of choosing each one, when should we use them ? Besides what the name says.
EDIT:
"Properly handle 'provided' and 'package' scopes to do what they should be doing. 'provided' and 'package' cannot be used with Android Libraries, and will generate an error" .. this is from http://tools.android.com/tech-docs/new-build-system
These properties come from
maven
scopes.They simply indicate how to treat particular dependencies during each step of build process.
compile
- a default approach, it simply means that all dependencies should be available at compile-time. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects. A compile-time dependency is generally required at runtime.package
- declares additional configuration for building an application. You can list plugins that add additional functionality to the build process.
That has some consequences:provided
- it means that runtime environment has these dependencies included. For example, when you look intoandroid.jar
library internals you will seejava.lang.RuntimeException: Stub!
in every method body.provided
andpackage
cannot be used with Android Libraries, and will generate an error.Here is how
sourceSet
looks like:More info about build system: https://www.youtube.com/watch?v=LCJAgPkpmR0
An awesome article about Gradle: http://www.sinking.in/blog/provided-scope-in-gradle/
provided
- compile-time only dependencypackage
- package-time only dependencycompile
- compile-time and package-time dependencyprovided
is commonly used for annotation processing based libraries. Usually these libraries are separated in two artifacts - "annotation" and "compiler". "compiler" isprovided
dependency because you do not need to use it in application, only for compilation; and "annotation" iscompile
dependency - it is used in application code and therefore compiles. Or generated code may require additional dependencies while your application may not. E.g. dagger dependencies configuration:Xavier talks here about the APK scope.