[java] How to convert AAR to JAR

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?

This question is related to java jar aar

The answer is


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

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.

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"


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
}

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!


 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


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