Java 编译、提供、APK - Android 依赖范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28472785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compile, Provided, APK - Android dependency scope
提问by Jacob
While adding new dependencies to android project especially in Android Studio
in Dependencies
there are three scope options Compile/Provided/APK.
而尤其是在增加新的依赖于Android项目Android Studio
中Dependencies
有三个作用域选项编译/供应/ 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
“正确处理 'provided' 和 'package' 范围以做他们应该做的事情。'provided' 和 'package' 不能与 Android 库一起使用,并且会产生错误”.. 这是来自http://tools。 android.com/tech-docs/new-build-system
回答by Kirill Boyarshinov
provided
- compile-time only dependencypackage
- package-time only dependencycompile
- compile-time and package-time dependency
provided
- 仅编译时依赖package
- 仅包时依赖compile
- 编译时和包时依赖
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:
provided
通常用于基于注释处理的库。通常这些库被分成两个工件——“注释”和“编译器”。“编译器”是provided
依赖,因为你不需要在应用程序中使用它,只用于编译;并且“注释”是compile
依赖项——它在应用程序代码中使用,因此可以编译。或者生成的代码可能需要额外的依赖项,而您的应用程序可能不需要。例如匕首依赖配置:
compile 'com.squareup.dagger:dagger:1.2.2'
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
回答by aows
Xavier talks hereabout the APKscope.
Xavier在这里谈到了APK范围。
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.
在 Android 插件中,运行时的等效(某种)称为 apk。你可以做
依赖项 { apk 文件('libs/foo.jar')}
它只会被打包,但不会在编译类路径上。
回答by Kamil Lelonek
These properties come from maven
scopes.
这些属性来自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.- 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 provideimplementation 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.
compile
- 默认方法,它只是意味着所有依赖项都应该在编译时可用。编译依赖项在项目的所有类路径中都可用。此外,这些依赖关系会传播到依赖项目。运行时通常需要编译时依赖项。package
- 声明用于构建应用程序的附加配置。您可以列出为构建过程添加附加功能的插件。
这有一些后果:provided
- 这意味着运行时环境包含这些依赖项。例如,当您查看android.jar
库内部结构时,您会java.lang.RuntimeException: Stub!
在每个方法主体中看到。- 无需完整的Android环境,即可在本地开发Android应用。
- 您的 APK 必须在 android 设备或模拟器上运行它,因为它们提供了这些方法的实现。
- 引用 SDK 类的应用程序将正确构建,因为 jar 提供类元数据。
- 除非您使用一些提供工件的库(例如Robolectric),否则您必须在模拟器/设备上运行测试。
provided
and package
cannot be used with Android Libraries, and will generate an error.
provided
并且package
不能与 Android 库一起使用,并且会产生错误。
Here is how sourceSet
looks like:
下面是它的sourceSet
样子:
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/
一篇关于 Gradle 的精彩文章:http: //www.sinking.in/blog/provided-scope-in-gradle/