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?
Solution 1:
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:
- Extract the AAR file using standard zip extract (rename it to *.zip to make it easier)
- Find the classes.jar file in the extracted files
- Rename it as you like and use that jar file in your project
Solution 2:
.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)
Solution 3:
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):
- Rename the .aar file to .zip and extract.
- The extracted folder is an ADT project that you can import in Eclipse with some minor modifications (see below)!
- 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.
- Now in order for Eclipse to accept it you need to put two files into the extracted folder root:
- .project
- .classpath
- To do that, create a new Android dummy project in Eclipse and copy over the files, or copy over from an existing Android project.
- 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.
- Now in Eclipse you can File -> New -> Project -> Android Project from existing source.. and point to the extracted folder content.
- After import right click on the newly created project, select Properties -> Android, and check Is Library.
- 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.