Java Android 存档库 (aar) 与标准 jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23915619/
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
Android Archive Library (aar) vs standard jar
提问by Xtreme Biker
I've been reading some articles about the new adoption of Gradle as the standard build system for Android apps. Well, coming from standard Java development I usually depend on jarfiles in order to build my project. However it seems that Android has also aarpackages, which are the equivalent to the dllfiles in a Windows OS, as mentioned here:
我一直在阅读一些关于新采用 Gradle 作为 Android 应用程序的标准构建系统的文章。好吧,来自标准 Java 开发,我通常依赖jar文件来构建我的项目。然而似乎安卓也有AAR包,这是相当于DLL在Windows操作系统文件,提到这里:
First, you have to realize that the Android platform does not allow application-level “shared libraries”. In the “traditional” programming language platforms, C, C++, Java, you name it, we have this mechanism of sharing runtime libraries. (E.g., DLL on Windows, DSO on Unix, Jar on JVM, etc.). On Android, however, you cannot do that, unless you are Google or a handset manufacturer (See Footnote 1 below). As an application developer, this can be a fundamental limitation. “Sharing” or “reusing” codes, both at build time and run time, is a very important part of software engineering practice. This is rather hard (not impossible, just harder) on Android because of the aforementioned limitation.
首先,您必须意识到Android平台不允许应用程序级“共享库”。在“传统”编程语言平台中,C、C++、Java 等等,我们都有这种共享运行时库的机制。(例如,Windows 上的 DLL、Unix 上的 DSO、JVM 上的 Jar 等)。但是,在 Android 上,您不能这样做,除非您是 Google 或手机制造商(请参阅下面的脚注 1)。作为应用程序开发人员,这可能是一个基本限制。在构建时和运行时“共享”或“重用”代码是软件工程实践中非常重要的一部分。由于上述限制,这在 Android 上相当困难(并非不可能,只是更难)。
However, I have some doubts around this concept. I mean, when should a developer be interested including aardependencies in its application? Are this dependencies tightened to some SDK minimum version?
但是,我对这个概念有些怀疑。我的意思是,开发人员什么时候应该有兴趣在其应用程序中包含aar依赖项?此依赖项是否已收紧到某些 SDK 最低版本?
For example, in one project I access a COM port, which I use NDK precompiled.solibraries for. Do I have to create an aar if I want to share this utility?
例如,在一个项目中,我访问了一个 COM 端口,我使用NDK 预编译的.so库。如果我想共享此实用程序,是否必须创建一个 aar?
采纳答案by unify
AAR
files are more similar to Jar
s than to Dll
s for the following reason:
AAR
出于以下原因,文件更类似于Jar
s 而不是Dll
s:
Dll
s can be shared across applications where asAAR
s and jars are packaged in with your app.
Dll
s 可以在应用程序之间共享,因为AAR
s 和 jars 与您的应用程序打包在一起。
AAR
s vs Jar
s:
AAR
s 对Jar
s:
The main difference between a
Jar
and aAAR
is thatAAR
s include resources such aslayouts, drawables
etc. This makes it a lot easier to create self-contained visual components. For example if you have multiple apps that use the same login screen, withJar
s you could share classes but not the layout, styles, etc., you still had to duplicate them. WithAAR
s everything is bundled in one neat package.
之间的主要区别
Jar
和AAR
是AAR
小号包括资源,如layouts, drawables
等。这使得很多更容易地创建自包含的可视化组件。例如,如果您有多个使用相同登录屏幕的应用程序,使用Jar
s 您可以共享类但不能共享布局、样式等,您仍然必须复制它们。随着AAR
s 一切都捆绑在一个整洁的包装中。
In conclusion, AAR
s are a big step in the right direction.
总之,AAR
s是朝着正确方向迈出的一大步。
Note:
Similar attempts were made with apk-lib
s but they are now obsolete as AAR
s are much better.
注意:
对apk-lib
s进行了类似的尝试,但它们现在已经过时了,因为AAR
s 更好。
回答by ggghhhjjj
The statement "The main difference between a Jar and a AAR is that AARs include resources such as layouts, drawables etc." does not correspond to the JAR file specification and therefore is not a truth. According the JAR file specification:
声明“ Jar 和 AAR 之间的主要区别在于 AAR 包括布局、可绘制等资源”不符合 JAR 文件规范,因此不是事实。根据JAR 文件规范:
JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory.
JAR 文件是一种基于流行的 ZIP 文件格式的文件格式,用于将多个文件聚合为一个。JAR 文件本质上是一个 zip 文件,其中包含一个可选的 META-INF 目录。
As you can see, there is no content limitation which forbids including resources such as layouts, drawables etc. in a JAR file. For more detail see article 5.3 "Creation and Loading" of The Java? Virtual Machine Specification.
如您所见,没有内容限制禁止在 JAR 文件中包含布局、可绘制等资源。更多详细信息请参见 The Java? 的 5.3 “创建和加载”文章?虚拟机规范。
So on the question Android Archive Library (aar) vs standard jar. The answer depends on what build tool are you using.
所以关于 Android Archive Library (aar) vs 标准 jar 的问题。答案取决于您使用的是什么构建工具。
If you are using Android Studio as a build tool (respectively as a project organizer) you'd definitely better use *.aar files to share encapsulated resources between Android projects. AAR file format is a part of Android Studio build and as it's commented in the other comments here its user interface supports aar format for Android Libraries.
如果您使用 Android Studio 作为构建工具(分别作为项目组织者),您绝对最好使用 *.aar 文件在 Android 项目之间共享封装的资源。AAR 文件格式是 Android Studio 构建的一部分,正如这里的其他评论中所评论的那样,其用户界面支持 Android 库的 aar 格式。
But except Android Studio the rest of the world does not know what is that thing aar file (artifact). For example, if your Android build is based on Maven the preferred file for resources sharing will be jar because that is the native Maven java project artifact and there is no limitation what to put in the standard jar file. In addition, there is a way to explain Maven any file format, include aar by using lifecycle enhancement with a new component. A simple example is available here How do I create a new packaging type for Maven?
但是除了 Android Studio 之外,世界其他地方都不知道 aar 文件(工件)是什么东西。例如,如果您的 Android 构建基于 Maven,则资源共享的首选文件将是 jar,因为这是本机 Maven java 项目工件,并且没有限制在标准 jar 文件中放置什么。此外,还有一种方法可以解释 Maven 任何文件格式,通过使用具有新组件的生命周期增强来包含 aar。此处提供了一个简单示例如何为 Maven 创建新的打包类型?
回答by Audrius Meskauskas
The citation in the question has nothing common with the current reality. Of course it is possible to use external libraries in Android and there are lots libraries available. Maybe they wanted to say that each application must bundle all libraries it needs, but reusing the library at the build time (static linking) is really not a problem.
问题中的引文与当前的现实没有任何共同之处。当然,可以在 Android 中使用外部库,并且有很多可用的库。也许他们想说每个应用程序必须捆绑它需要的所有库,但是在构建时重用库(静态链接)确实不是问题。
.aar
differs from .jar
no more than .jar
differs from .zip
. It has certain concepts on which kind of content should be expected there, but both .jar
and .aar
most often contain compiled classes and they resources. .aar
just specifies that the library is Android specific and has some expected structure, reasonable for such libraries (well, .jar
also has some expected structure).
.aar
不同于.jar
不超过.jar
不同于.zip
。它有一些关于应该在那里期望什么样的内容的概念,但两者.jar
并且.aar
最常包含编译的类和它们的资源。.aar
只是指定库是 Android 特定的,并且具有一些预期的结构,对于此类库是合理的(嗯,.jar
也有一些预期的结构)。
The view that .aar is only supported by Android studio is also deprecated. Such libraries can be deployed to Maven Central, and tools like gradle can reference them using @aar suffix, for instance:
.aar 仅受 Android Studio 支持的观点也已弃用。这些库可以部署到 Maven Central,像 gradle 这样的工具可以使用 @aar 后缀来引用它们,例如:
dependencies {
compile ('io.github.andviane:uncover:2.0.1@aar')
..
}
to reference thisMaven central deployment.
引用此Maven 中央部署。