[Project Combat] Introducing third-party libraries or packaging in Android development (packaging and importing methods of Android JAR and AAR file formats)

1. Background

In Android development, we often need to introduce third-party libraries or package our Android code into reusable libraries. Among them, Android JAR and AAR are two common library file formats. The packaging and importing methods of these two file formats will be introduced in detail below, as well as the differences, advantages and disadvantages, and applicable scenarios between them.

Second, the difference between JAR and AAR file formats

Both JAR and AAR are used to package Android code or libraries into a reusable file format, but there are some differences between them:

2.1 JAR file format

JAR (Java Archive) is a Java archive file format, and
JAR files require manual resolution of dependencies.

2.2 AAR file format

AAR (Android Archive) is an Android-specific archive file format.
The construction and release of AAR files is more convenient because it can automatically resolve dependencies,

3. Packaged into JAR and AAR

2.1 Packaged into JAR

JAR is used to package Java class files, resource files, and metadata into one file.
In Android development, we can package the Android code into a JAR file.
To package a JAR file, you need to use the Java jar command or build tools such as Maven, Gradle, etc.

A JAR file contains only Java class files, resource files, and metadata.

2.2 Packaging AARs

AARs are specifically designed to package Android libraries and their dependencies into one file.
The AAR file contains
(1) the jar file of the Android library
(2) the lib file in the aar file (including the native library)
(3) the res resource folder
(4) the java source code and other dependencies.
Packaging AAR files typically uses the Gradle build tool.

4. How to introduce JAR and AAR

AAR files can be imported directly, while JAR files need to be decompressed into jar files before they can be imported.

4.1 Importing JARs

Introducing a JAR file is relatively simple, just put it in the lib directory of the Android project, and then reference the JAR file in the project.

If you use the Maven or Gradle build tool, you need to add the corresponding dependencies in the build configuration file.

4.2 Introducing AARs

The method of introducing an AAR file is similar to introducing a JAR file, just put it in the lib directory of the Android project, and then reference the AAR file in the project.

If you use the Maven or Gradle build tool, you need to add the corresponding dependencies in the build configuration file.

Five, the advantages and disadvantages of JAR and AAR

5.1 JAR

  • Advantages of JAR:
    JAR can be used across platforms because it only contains Java class files, resource files, and metadata, and does not depend on Android-specific APIs and libraries.
    Building and publishing a JAR file is relatively simple, as it does not require dealing with Android-specific dependencies and resource files.
  • Disadvantages of JAR:
    It is cumbersome to manually resolve dependencies.
    Android-specific resource files and library files are not included, which may not meet the needs of Android development.

5.2 AAR

  • Advantages of AAR:
    AAR includes the jar file of the Android library, the lib file in the aar file (including the native library), the res resource folder, the java source code, and other dependencies, which can better meet the needs of Android development.
    Building and publishing of AAR files is more convenient because it automatically resolves dependencies.

  • Disadvantages of AAR:
    AAR is an Android-specific archive file format and cannot be used across platforms.
    The construction and release of AAR files depend on the Gradle build tool, which may not be suitable for some projects that do not use Gradle.

6. Applicable scenarios of JAR and AAR

According to the advantages and disadvantages of JAR and AAR, we can choose the appropriate library file format according to the actual situation:

  • If you want to create a cross-platform library, or if you want to manually handle dependencies, then you can choose the JAR format.
  • If you want to create a library specifically for Android and need to automatically resolve dependencies, then you can choose the AAR format.

Guess you like

Origin blog.csdn.net/wstever/article/details/132601122