Jacob
Jacob

Reputation: 15297

Compile, Provided, APK - Android dependency scope

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

Upvotes: 43

Views: 27289

Answers (4)

akarahman
akarahman

Reputation: 378

With gradle 6.5.1 provided gives the below error

Could not find method provided() for arguments [directory '....'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

I used compileOnly to use the dependencies at only compile time and not to inlcude in the final build artifact

Upvotes: 0

Kamil Lelonek
Kamil Lelonek

Reputation: 14784

These properties come from maven scopes.

They simply indicate how to treat particular dependencies during each step of build process.

  1. 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.

  2. package - declares additional configuration for building an application. You can list plugins that add additional functionality to the build process.

  3. provided - it means that runtime environment has these dependencies included. For example, when you look into android.jar library internals you will see java.lang.RuntimeException: Stub! in every method body.

    That has some consequences:

    • You can develop Android applications locally, without having complete Android environment.
    • Your APK you must run it on an android device or an emulator because they provide implementation of these methods.
    • Your apps that reference the SDK classes will build properly, because the jar provides the class metadata.
    • Unless you use some library that provides artifacts (e.g. Robolectric), you have to run tests on your emulator/device.

provided and package cannot be used with Android Libraries, and will generate an error.

Here is how sourceSet looks like:

enter image description here

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/

Upvotes: 36

aows
aows

Reputation: 526

Xavier talks here about the APK scope.

in the Android plugin, The equivalent (sort of) of runtime is called apk. You can do

dependencies { apk files('libs/foo.jar') }

and it'll only get packaged but won't be on the compile classpath.

Upvotes: 16

Kirill Boyarshinov
Kirill Boyarshinov

Reputation: 6173

  • provided - compile-time only dependency
  • package - package-time only dependency
  • compile - compile-time and package-time dependency

provided is commonly used for annotation processing based libraries. Usually these libraries are separated in two artifacts - "annotation" and "compiler". "compiler" is provided dependency because you do not need to use it in application, only for compilation; and "annotation" is compile 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:

compile 'com.squareup.dagger:dagger:1.2.2'
provided 'com.squareup.dagger:dagger-compiler:1.2.2'

Upvotes: 42

Related Questions