154

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 jar files in order to build my project. However it seems that Android has also aar packages, which are the equivalent to the dll files in a Windows OS, as mentioned here:

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.

However, I have some doubts around this concept. I mean, when should a developer be interested including aar dependencies in its application? Are this dependencies tightened to some SDK minimum version?

For example, in one project I access a COM port, which I use NDK precompiled .so libraries for. Do I have to create an aar if I want to share this utility?

Aritz
  • 30,971
  • 16
  • 136
  • 217

5 Answers5

248

AAR files are more similar to Jars than to Dlls for the following reason:

Dlls can be shared across applications where as AARs and jars are packaged in with your app.

AARs vs Jars:

The main difference between a Jar and a AAR is that AARs include resources such as layouts, 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, with Jars you could share classes but not the layout, styles, etc., you still had to duplicate them. With AARs everything is bundled in one neat package.

In conclusion, AARs are a big step in the right direction.

Note:
Similar attempts were made with apk-libs but they are now obsolete as AARs are much better.

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
unify
  • 6,161
  • 4
  • 33
  • 34
  • @unify . Is there any way so we can prevent aar files- obfuscate the library? – abh22ishek Nov 30 '15 at 06:16
  • 1
    I'm confused by this answer. The first block quote says that "aars and jars are packaged in with your app", not "shared across applications"; yet the second block quote implies that aars let you share classes and other resources between multiple apps? – LarsH Aug 01 '16 at 21:33
  • 6
    @LarH I think it means that you can share the code (aar files) between your applications, but it will be included in every package separately to let users install the applications independently. If 2 applications are installed and both use the same aar, the android runtime might be clever enough to load it only once. – Habib Aug 16 '16 at 17:18
  • 2
    I have big problem with AAR files. I want make an AAR file and give it to some merchant to have payment solutions with our business. and i don't want they can reflect our class members and they can't have method call from their apps. I there any way to protect my code from reflection. – Mohammad moradyar Feb 03 '17 at 09:54
  • 7
    @Mohammadmoradyar you can use Proguard on your library to obfuscate your classes but as with any Java-based bytecode, there's no way to stop people from decompiling your code. – unify Apr 13 '17 at 18:16
16

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

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.

So on the question Android Archive Library (aar) vs standard jar. The answer depends on what build tool are you using.

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.

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?

Community
  • 1
  • 1
ggghhhjjj
  • 169
  • 1
  • 3
  • I would say then that the difference is that the JAR is meant to be a way to share any kind of resource in the java ecosystem, while the AAR is Android focused and [forces you to a concrete layout](https://developer.android.com/studio/projects/android-library.html#aar-contents). – Aritz May 05 '17 at 06:39
  • I think author of the chosen answer (@unify) should make some clarification about this. – lfree Jul 12 '18 at 01:01
  • 2
    I don't know about the greater Java world, but in the Android world jar files couldn't include resources. That is, even if resources were in the .jar archive, they wouldn't be brought over to the project that includes them: https://stackoverflow.com/q/2474904/211292 – ThomasW Oct 04 '18 at 02:47
12

JAR vs AAR

JAR – Java Archive. Contains .class, java resources

AAR – Android Archive. Contains classes.jar, and other Android related files like AndroidManifest.xml, R.txt, proguard.txt, res folder(layout, values, drawable) and others.

Android works with both files jar and aar. As a library developer usually aar is used for sharing Android resources, otherwise - jar like more lightweight and common way of distributing an artefact.

Since aar contains jar you can use it[About]

Difference:

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
9

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.

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

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:

dependencies {
    compile ('io.github.andviane:uncover:2.0.1@aar')
    ..
}  

to reference this Maven central deployment.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
-2

The main difference is aar is splitted inside android to jar.

If your app will be used only in user app only in android studio then aar is preferred.

If you are planning for app to communicate with c/c++ compiled lib.so file jar is preferred.

Using jar and aar based on requirements aar working easy, jar more flexibility.

This is as my knowledge upto android 5 lollipop. New version i am not aware.

Jerald
  • 1
  • 1