98

Situation:

I intend to use a Java library and I only have an AAR file from a Maven repository but I need the JAR file.

Background story:

I tried to compile a library, but its Gradle structure was unstable. So I asked for a compiled jar file, and its developer handed me an aar file from some Maven repository (the developer couldn't compile his own project). The Gradle configuration was a mess, it depended on multiple libraries and Gradle has thrown some exceptions.

I tried to use it in my IDEA IDE, but it couldn't see it. A library project should be able to be compiled into a jar file, right?

Question:

What should I do to convert that AAR file to a JAR file?

Nestor
  • 8,194
  • 7
  • 77
  • 156
  • 1
    You can just rename it back to .jar but without knowing more details about your environment this may or may not be what you need to do, an AAR is an Axis library with specific uses. – Pedantic Jan 28 '14 at 21:42
  • 1
    You'll be laughing, but I had to add some non-regular word because SO wouldn't let my question be posted as it was so simple-worded. And I guess, it tells everything that the library's developer can't compile his own project using Gradle... – Nestor Jan 28 '14 at 21:52

8 Answers8

155

The AAR file consists of a JAR file and some resource files (it is basically a standard zip file with a custom file extension). Here are the steps to convert:

  1. Extract the AAR file using standard zip extract (rename it to *.zip to make it easier)
  2. Find the classes.jar file in the extracted files
  3. Rename it as you like and use that jar file in your project
Nestor
  • 8,194
  • 7
  • 77
  • 156
  • 59
    Interesting that this answer is accepted, because simply renaming a jar and copy it to your project's libs directory will not work due to the fact that the resource files are missing from the jar. – ChuongPham Oct 26 '14 at 14:07
  • Following the tutorial for Spotify Android SDK, this answer was the remedy. This is because I develop projects in Netbeans, which seems to be not in compliance with the AAR extension. The remark of ChuongPham seems to be related not to this answer, but to the answer of AWT, and then I agree partly with ChuongPham, i.e. simple rename from AAR to JAR is not working. However, I also partly disagree because "resources" is not relevant for the classes.jar file. – carl Jan 09 '15 at 21:36
  • 12
    I had a hard time finding out how to extract it on mac, in case others too: just change .aar to .zip and double click! – Bastien Beurier Jan 29 '15 at 23:16
  • 5
    so what is the solution for creating a resource based jar ? – 2cupsOfTech Jul 08 '15 at 20:40
  • 2
    This solution only extract the jar file, but resources not contain in it. Something would be wrong. – bitristan Jan 05 '16 at 08:04
  • @2cupsOfTech See my answer about resource based .aar-files – OneWorld Jan 06 '16 at 15:31
  • `jar xf example.aar` should extract the contents without issue. – ManoDestra Jul 07 '16 at 21:31
22

.aar is a standard zip archive, the same one used in .jar. Just change the extension and, assuming it's not corrupt or anything, it should be fine.

If you needed to, you could extract it to your filesystem and then repackage it as a jar.

1) Rename it to .jar
2) Extract: jar xf filename.jar
3) Repackage: jar cf output.jar input-file(s)
jhocking
  • 5,527
  • 1
  • 24
  • 38
AWT
  • 3,657
  • 5
  • 32
  • 60
  • When I run the jar xf, I get nothing: C:\Program Files\Java\jdk1.7.0_25\bin>jar xf c:\dev\1\android-maps-utils- 0.2.1.jar C:\Program Files\Java\jdk1.7.0_25\bin> – Nestor Jan 28 '14 at 22:56
  • 1
    dude, step 1 is enough. Just rename it to .jar . In my view steps (2) and (3) are useless – Dr Deo Feb 21 '15 at 19:47
  • @DrDeo If I just rename it, the eclipse project is not recognizing it as a library. Is it normal? Any clues? – Tanasis Aug 10 '17 at 20:36
  • 1
    If you just rename it and don't repackage it with jar, it's not going to have a manifest and it won't recognize it as a valid jar file, I believe. – AWT Aug 11 '17 at 01:31
16

As many other people have pointed out, just extracting the .jar from the .aar file doesn't quite cut it as resources may be missing.

Here are the steps that worked for me (context of Android, your mileage may vary if you have other purposes):

  1. Rename the .aar file to .zip and extract.
  2. The extracted folder is an ADT project that you can import in Eclipse with some minor modifications (see below)!
  3. In the extracted folder rename the contained file classes.jar to whatever you like (in this example myProjectLib.jar) and move it to the lib folder within the extracted folder.
  4. Now in order for Eclipse to accept it you need to put two files into the extracted folder root:
    • .project
    • .classpath
  5. To do that, create a new Android dummy project in Eclipse and copy over the files, or copy over from an existing Android project.
  6. Open the .project file and look for the XML name tag and replace the contents of it with myProjectLib (or whatever you called your jar file above) and save.
  7. Now in Eclipse you can File -> New -> Project -> Android Project from existing source.. and point to the extracted folder content.
  8. After import right click on the newly created project, select Properties -> Android, and check Is Library.
  9. In your main project that you want to use the library for, also go to Properties -> Android and add the newly added myProjectLib to the list of dependencies.
rsp1984
  • 1,877
  • 21
  • 23
  • 1
    This helped me. But in order to get if fully working, i needed to create an empty "src" folder between step 6 and 7. – Bernhard Krenz Jan 15 '18 at 21:29
  • I second that an empty src folder needs to be created. Also, once this procedure works (beautifully, because the steps are very clear), I had to delete its proguard.cfg references: https://stackoverflow.com/a/74728030/2597758 + delete the old android-support-v4.jar from the app's libs folder. – WebViewer Dec 08 '22 at 09:39
8

For those, who want to do it automatically, I have wrote a little two-lines bash script which does next two things:

  1. Looks for all *.aar files and extracts classes.jar from them
  2. Renames extracted classes.jar to be like the aar but with a new extension

    find . -name '*.aar' -exec sh -c 'unzip -d `dirname {}` {} classes.jar' \;
    find . -name '*.aar' -exec sh -c 'mv `dirname {}`/classes.jar `echo {} | sed s/aar/jar/g`' \;
    

That's it!

ddmytrenko
  • 796
  • 7
  • 16
  • 1
    Sure, any aar2jar converting will not contain resources and manifest inside. We can say (not definitely but close to) that `aar=jar+res+manifest+libs`. Let `rest=res+manifest+libs`, than `jar(aar)=aar-rest`. – ddmytrenko Jul 15 '15 at 14:08
  • That's what I really want! Thank's a lot. But it really increase the disk usage. because every support library has many version. only unzip the latest verion may be better – iptton Feb 16 '17 at 09:15
6

Android Studio (version: 1.3.2) allows you to seamlessly access the .jar inside a .aar.

Bonus: it automatically decompiles the classes!

Simply follow these steps:

  1. File > New > New Module > Import .JAR/.AAR Package to import you .aar as a module

  2. Add the newly created module as a dependency to your main project (not sure if needed)

  3. Right click on "classes.jar" as shown in the capture below, and click "Show in explorer". Here is your .jar.

access .jar from .aar

Sébastien
  • 13,831
  • 10
  • 55
  • 70
  • 1
    first must select project instead of android in project manager then external libraries will show – Samad May 06 '17 at 10:53
6

Resource based .aar-projects

Finding the classes.jar file inside the .aar file is pretty trivial. However, that approach does not work, if the .aar-project defined some resources (example: R.layout.xyz)

  • Therefore deaar from CommonsGuy helped me to get a valid ADT-friendly project out of an .aar-file. In my case I converted subsampling-scale-image-view. It took me about an hour to set up ruby on my PC.

  • Another approach is using android-maven-plugin for Eclipse/ADT as CommonsGuy writes in his blog.

  • Yet another approach could be, just cloning the whole desired project as source from git and import it as "Existing Android project"

OneWorld
  • 17,512
  • 21
  • 86
  • 136
5
 The 'aar' bundle is the binary distribution of an Android Library Project. .aar file 
 consists a JAR file and some resource files. You can convert it
 as .jar file using this steps

1) Copy the .aar file in a separate folder and Rename the .aar file to .zip file using 
 any winrar or zip Extractor software.

2) Now you will get a .zip file. Right click on the .zip file and select "Extract files". 
 Will get a folder which contains "classes.jar, resource, manifest, R.java,
 proguard(optional), libs(optional), assets(optional)".

3) Rename the classes.jar file as yourjarfilename.jar and use this in your project.

Note: If you want to get only .jar file from your .aar file use the above way. Suppose If you want to include the manifest.xml and resources with your .jar file means you can just right click on your .aar file and save it as .jar file directly instead of saving it as a .zip. To view the .jar file which you have extracted, download JD-GUI(Java Decompiler). Then drag and drop your .jar file into this JD_GUI, you can see the .class file in readable formats like a .java file.

enter image description here enter image description here

anand krish
  • 4,281
  • 4
  • 44
  • 47
  • Does this mean that we can use the AAR file like a JAR file just by renaming it from .aar to .jar? – Soumya Dec 21 '15 at 21:08
  • @Soumya, no, you cannot. – ddmytrenko Dec 22 '15 at 08:48
  • 2
    Actually the topic of the conversation is wrong. We cannot convert AAR to JAR or vice-versa. The only thing we can do is "extract JAR from AAR". And I have wrote how to do this automaticaly using two lines of Bash code here: http://stackoverflow.com/a/29252592/746529 – ddmytrenko Dec 22 '15 at 08:51
1

If you are using Gradle for your builds - there is a Gradle plugin which allows you to add aar dependency to your java|kotlin|scala|... modules.

https://github.com/stepango/aar2jar

plugins {
    id 'java'
    id 'com.stepango.aar2jar' version “0.6” // <- this one
}

dependencies {
    compileOnlyAar "com.android.support:support-annotations:28.0.0" // <- Use any AAR dependencies
}
Stepango
  • 4,721
  • 3
  • 31
  • 44
  • Trying to understand, is it possible to make 'compileOnlyAar' for android app/lib plugin? i know that it's not about your library, but maybe you know is it possible since, android plugin doesn't contain artifact configuration like 'compileOnlyAar'. You can understand why i need this functionality here (https://stackoverflow.com/questions/60998072/how-to-make-an-equivalent-of-compileonly-for-aar-artifacts) – HeyAlex Apr 05 '20 at 18:14
  • your quotes are broken, and I can't edit because the edit queue is full – Nailuj29 Nov 12 '20 at 22:13