编译,提供,APK - Android依赖范围

时间:2022-06-03 11:15:02

While adding new dependencies to android project especially in Android Studio in Dependencies there are three scope options Compile/Provided/APK.

当向android项目添加新的依赖项时,特别是在依赖项中的android Studio中,有三个范围选项编译/提供/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

正确地处理“提供”和“打包”范围,以完成它们应该做的事情。“提供”和“包”不能与Android库一起使用,并将产生一个错误。这是来自http://tools.android.com/tech-docs/new-build-system

3 个解决方案

#1


41  

  • 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'

#2


33  

These properties come from maven scopes.

这些属性来自maven作用域。

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.

    提供——这意味着运行时环境包含了这些依赖项。例如,当你研究android的时候。jar库内部,您将看到java.lang。RuntimeException:存根!在每一个方法体。

    That has some consequences:

    • You can develop Android applications locally, without having complete Android environment.
    • 您可以在本地开发Android应用程序,而不需要完整的Android环境。
    • Your APK you must run it on an android device or an emulator because they provide implementation of these methods.
    • 您的APK必须在android设备或模拟器上运行,因为它们提供了这些方法的实现。
    • Your apps that reference the SDK classes will build properly, because the jar provides the class metadata.
    • 引用SDK类的应用程序将正确构建,因为jar提供了类元数据。
    • Unless you use some library that provides artifacts (e.g. Robolectric), you have to run tests on your emulator/device.
    • 除非使用提供工件的库(例如Robolectric),否则必须在模拟器/设备上运行测试。

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

提供和包不能与Android库一起使用,并将产生错误。

Here is how sourceSet looks like:

sourceSet是这样的:

编译,提供,APK - Android依赖范围

More info about build system: https://www.youtube.com/watch?v=LCJAgPkpmR0

关于构建系统的更多信息:https://www.youtube.com/watch?v=LCJAgPkpmR0

An awesome article about Gradle: http://www.sinking.in/blog/provided-scope-in-gradle/

关于等级的一篇很棒的文章:http://www.sinking.in/blog/provi- scopygradle/

#3


16  

Xavier talks here about the APK scope.

泽维尔在这里谈到了APK范围。

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

在Android插件中,等价的运行时称为apk。你可以做

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

附件{apk文件('libs/foo.jar')}

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

它只会打包,但不会出现在编译类路径中。

#1


41  

  • 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'

#2


33  

These properties come from maven scopes.

这些属性来自maven作用域。

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.

    提供——这意味着运行时环境包含了这些依赖项。例如,当你研究android的时候。jar库内部,您将看到java.lang。RuntimeException:存根!在每一个方法体。

    That has some consequences:

    • You can develop Android applications locally, without having complete Android environment.
    • 您可以在本地开发Android应用程序,而不需要完整的Android环境。
    • Your APK you must run it on an android device or an emulator because they provide implementation of these methods.
    • 您的APK必须在android设备或模拟器上运行,因为它们提供了这些方法的实现。
    • Your apps that reference the SDK classes will build properly, because the jar provides the class metadata.
    • 引用SDK类的应用程序将正确构建,因为jar提供了类元数据。
    • Unless you use some library that provides artifacts (e.g. Robolectric), you have to run tests on your emulator/device.
    • 除非使用提供工件的库(例如Robolectric),否则必须在模拟器/设备上运行测试。

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

提供和包不能与Android库一起使用,并将产生错误。

Here is how sourceSet looks like:

sourceSet是这样的:

编译,提供,APK - Android依赖范围

More info about build system: https://www.youtube.com/watch?v=LCJAgPkpmR0

关于构建系统的更多信息:https://www.youtube.com/watch?v=LCJAgPkpmR0

An awesome article about Gradle: http://www.sinking.in/blog/provided-scope-in-gradle/

关于等级的一篇很棒的文章:http://www.sinking.in/blog/provi- scopygradle/

#3


16  

Xavier talks here about the APK scope.

泽维尔在这里谈到了APK范围。

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

在Android插件中,等价的运行时称为apk。你可以做

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

附件{apk文件('libs/foo.jar')}

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

它只会打包,但不会出现在编译类路径中。