34

In Android systems or development enviroments, what are the differences between AAR, JAR, DEX, and APK files? What is the purpose of each one?

AFAIK, JAR are just like a collection of .class files (like in Java).

AAR are JAR files + resources. But what's its usage case? To be used to distribute development libraries for Android?

APK seems to be similar to packages like .deb or .rpm. Is it the only way to install applications on Android?

What about DEX files? Compiled applications... but what's the difference with a JAR or AAR file?

Moreveor, when distributing .so (i.e. native code) files for Android, what's the best way to do it?

sapito
  • 1,472
  • 3
  • 17
  • 26

2 Answers2

46

JAR (Java Archive)

JAR is a package file format designed for distribution of Java application on its platform. It contains compiled Java class files + some more files like MANIFEST. Basically it is just an ZIP archive with some restrictions.

DEX (Dalvik Executable)

DEX is binary file format, so it is compiled. We could say, that .dex file is for DVM (Dalvik Virtual Machine) something like .class files for JVM.

DEX file format is really generated from java CLASS files by dex compiler from Android SDK. This compiler translates JVM bytecode to DVM bytecode and put all class files to one dex file.

APK (Android Application Package)

APK is file format designed for distributing Android application on its platform. It has some similarities with JAR format. Again it is simply just a ZIP archive, but APK files have pre-defined specific structure. For example, it always must contains file named AndroidManifest.xml and many more. Also this package aggregates compiled classes in dex format.

AAR

AAR is the binary distribution of an Android Library Project. It has similar structure as APK.

giusti
  • 3,156
  • 3
  • 29
  • 44
hradecek
  • 2,455
  • 2
  • 21
  • 30
  • Thanks was really helpful.The AAR information is mostly not known by many people – Hargun Singh Oct 03 '17 at 17:36
  • To add a bit more to AAR (Android ARchive) - https://developer.android.com/studio/projects/android-library – ThisCompSciGuy Sep 11 '20 at 00:07
  • I found a `classes.jar` after I unzipped the AAR. I found the manifest and class files after I unzipped `classes.jar`. Using `unzip file` was helpful in learning, after reading this. – Ben Butterworth Nov 29 '20 at 15:35
  • I feel like this answer misses out on the difference between AAR and APK. `However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. ` [source](https://developer.android.com/studio/projects/android-library) – Ben Butterworth Dec 02 '20 at 16:06
  • The `.aar` files internally contains `.class files` + android resources + proguard files + extra meta data, all in zipped format. – iCantC Oct 04 '22 at 14:18
21

JAR are just like a collection of .class files (like in Java).

That is because it is a Java JAR.

AAR are JAR files + resources

Correct.

But what's its usage case? To be used to distribute development libraries for Android?

Correct.

APK seems to be similar to packages like .deb or .rpm. Is it the only way to install applications on Android?

Generally speaking, yes.

What about DEX files?

DEX files contain the cross-compiled Dalvik bytecodes representing the application code. Developers write Java, but Android runs Dalvik bytecodes. In the APK, you will find a DEX file representing those bytecodes.

Moreveor, when distributing .so (i.e. native code) files for Android, what's the best way to do it?

I do not know what you mean by "distributing".

If you mean "distributing an app", your .so files will be in the APK.

If you mean "distributing as a library for other developers", use an AAR or just an Android project in source form.

Bondax
  • 3,143
  • 28
  • 35
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    So for example AAR libraries aren't compiled into Dalvik bytecodes? If the are, what's the difference with DEX? If they aren't, should they be converted into Dalvik when linking the application? – sapito Nov 04 '15 at 22:51
  • @sapito: "So for example AAR libraries aren't compiled into Dalvik bytecodes?" -- correct. If you look inside one (they're just a ZIP archive), you will see a JAR inside. "should they be converted into Dalvik when linking the application?" -- yes. – CommonsWare Nov 04 '15 at 22:56
  • @CommonsWare What do you mean by "look inside" an AAR file? It isn't a folder that you can inspect. – IgorGanapolsky May 20 '16 at 20:10
  • 1
    @IgorGanapolsky: It's a ZIP-style archive, readable by a wide range of ZIP utilities. – CommonsWare May 20 '16 at 20:30
  • @CommonsWare Apparently not by default on Mac. – IgorGanapolsky May 20 '16 at 20:38
  • 3
    @IgorGanapolsky: Then make a copy of the AAR file, give the copy a ZIP extension, and try opening that. Or, find a better ZIP utility for the Mac. – CommonsWare May 20 '16 at 20:39