1. Introduction
As Android developers, we come across situations where we need to convert an AAR file to a JAR file. This normally happens when we want to use a library or module in our Java project that’s only available in the AAR format.
In this tutorial, we’ll learn the steps to convert an AAR file to a JAR file.
2. What Is an AAR File?
An AAR (Android ARchive) file is a package file format used in the Android operating system to distribute and share libraries, resources, and code between Android projects.
It’s similar to the JAR (Java Archive) file format, but it includes additional metadata for Android applications, such as AndroidManifest.xml, resource files, res folder (layout, values, drawable), proguard rules and others.
Android developers use the AAR file format to distribute their libraries and share their code between projects. It allows developers to easily manage their dependencies and integrate external libraries into their Android applications.
AAR files can be included in an Android project using build tools such as Gradle or Maven. These build tools automatically handle the dependencies and integrate them into the build process.
In short, AAR files play an important role in the Android development ecosystem, as they enable developers to build high-quality, efficient, and scalable applications.
3. What Is a JAR File?
A JAR (Java ARchive) file is a package file format used to store and distribute Java class files, resources, and associated metadata. It’s similar to a ZIP file in that it’s a compressed archive format that contains multiple files and folders.
JAR files are commonly used to distribute Java libraries or applications. Any platform that has a Java Runtime Environment (JRE) installed can execute JAR files.
JAR files are created using the jar command-line tool that comes with the Java Development Kit (JDK). They can be signed with a digital certificate to ensure their authenticity and integrity. They can also include a manifest file that provides metadata about the JAR file, such as the version number, author, and dependencies.
JAR files are an important part of the Java development ecosystem. It allows developers to package and distribute their Java code as a single, executable file. This makes it easier to share and distribute Java applications and libraries, and it also simplifies the deployment process for end-users.
4. Convert an AAR File to a JAR File
There may be situations where we need to convert an AAR file to a JAR file, such as when we want to use an Android library in a non-Android Java project. We’ll look at couple of approaches for doing that.
4.1. By Extracting the Contents of AAR Manually Using Zip
The first step in converting an AAR file to a JAR file is to extract the contents of the AAR file. We can do this by changing the extension of the AAR file to .zip. Then we’ll extract its contents using a tool like WinZip or 7-Zip. Once we have extracted the contents of the AAR file, we should see a folder with the same name as the AAR file.
Inside the extracted folder, we’ll see several files and folders. We need to look for the classes.jar file as it contains the Java class files that we want to convert to a JAR file. Then we’ll locate the classes.jar file and copy it to a new folder.
To convert the classes.jar file to a JAR file, we can use the jar command-line tool that comes with the Java Development Kit (JDK). We’ll open command prompt or terminal window as per our operating system. We’ll use the following command in the folder where we copied the classes.jar file.
$ jar -xf classes.jar
This should extract the contents of the classes.jar file into a new folder called classes.
Next, the following command, where mylibrary.jar is the name of the resultant JAR, creates a JAR file:
$ jar -cf mylibrary.jar -C classes/ .
A new JAR file named “mylibrary.jar” will be created in the same folder as the extracted classes. Let’s understand that the -C option tells the jar tool to change to the classes directory before adding its contents to the JAR file.
4.2. Using Android Studio
As Android developers we have access to Android studio. We can also use it to convert an AAR file to a JAR file using following steps:
We’ll navigate to File > New > New Module > Import .JAR/.AAR Package and select the desired AAR file and click Finish.
Generally, build.gradle file contains the following plugin:
apply plugin: 'com.android.library'
If it doesn’t exist, then we’ll add following line. If it exists, then we’ll replace it with the following line:
apply plugin: 'java-library'
Next, we’ll navigate to Build > Make module ‘Module Name’. We should see the JAR file located in the build/libs folder of the module, once the module is built.
5. Conclusion
In this article, we learned about contents and usage of an AAR file and a JAR file. We also saw how we can convert an AAR file to a JAR file manually using ZIP and Android studio.