Android Studio packaging AAR

Notice

The dependent Android Studio version is 4.2.2

The usage method may be different for higher Android Studio versions. The version of gradle and the version of gradle plugins will affect the usage method.

Based on this, this article can only be used as a reference, not as the only answer. If you want to rely entirely on this article, the Android Studio version also needs to be installed as 4.2.2.

About AAR

Android AAR file is the abbreviation of Android Archive file, which is an Android application archive file format similar to the JAR file format. AAR files contain compiled code and resource files that can be referenced and used by other Android applications. AAR files can contain multiple class libraries and resource files, which can be referenced and used in the project using Gradle dependencies. It is widely used in Android Studio and can be used in Android projects through the Gradle build system. AAR files are commonly used to share code and resources in Android applications to improve code reusability and build efficiency.

How to compile and generate AAR

To package Android Archive (AAR) files, you can follow these steps:

1. In Android Studio, open the project to be packaged as AAR.

If there is no target project, you can create a new blank project.

2. Select the File menu and then select New Module.

Or in the root directory (need to switch to project mode)

You can also create a new module directly by right-clicking Module.

3. In the Create New Module dialog box, select Android Library.

4. Enter a name for the module and other options, then click Finish.

5. Edit the code and resource files of the library project.

In the successfully created module, you can check in build.gradle whether the plug-in is referenced as com.android.library

6. Select the Build menu and select Make Module 'library_name'.

In addition, you can also package it directly in the tool built by gradle

In android studio, in the gradle tool in the right sidebar, find the target module and find other in Tasks

You can choose the packaging mode you want (debug and release)

7. Find the generated AAR file in the "build/outputs/aar/" directory of the project to share it with other applications or libraries.

This way, your Android library can be packaged as an AAR file and used with other applications or libraries.

8. Detailed explanation of AAR files

The AAR file has the file extension .aar and the Maven artifact type is aar. The file itself is a ZIP file. The only required entry is /AndroidManifest.xml.

AAR files can also contain one or more of the following optional entries:

  • /classes.jar
  • /res/
  • /R.txt
  • /public.txt
  • /assets/
  • /libs/name.jar
  • /jni/abi_name/name.so(in which abi_name is Android supporting ABI no one)
  • /proguard.txt
  • /lint.jar
  • /api.jar
  • /prefab/(用于导込原生库

The difference between AAR and JAR

AAR and JAR are both Java packaging formats, but in Android development, they have some differences:

1. AAR (Android Archive) is an Android-specific packaging format that contains all resources of the Android library project, such as layouts, pictures, XML files, etc. JAR (Java Archive) is a standard Java packaging format, which only contains Java class files and some static resource files, such as icons, configuration files, etc.

2. AAR files can contain multiple JAR files and individual resource files, while JAR files can only contain Java class files and static resource files.

3. In Android Studio, library projects using AAR format can automatically import all resources of the library project, thus simplifying the workload of project development and maintenance.

4. In addition, AAR format library projects can also specify dependencies. That is to say, if an AAR library project depends on another AAR library project, then when the library project is used, the library project it depends on will also be automatically Import.

Therefore, AAR format library projects are more suitable for Android development than JAR format library projects.

How to cite an AAR

In Android Studio, you can reference an aar file by following these steps:

1. Place the aar file in the libs folder of the project.

2. Add the following code to the project’s build.gradle file:

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    implementation(name:'XXXXXX_1.1.0', ext:'aar')
}

3. After Gradle sync of the project, you can use the classes, methods, etc. in this library.

If you are using Android plugin 3.0 or higher, you need to change implementation to api.

api files('libs/XXXXXXX_1.2.0_release.aar')

Regarding implementation and API, a simple understanding is the difference between public and private in the class.

Implementation means that the reference is only effective for the current module. Even if other modules depend on this module, they cannot use other dependencies referenced by implementation. Is the keyword of module private dependency.

api means public reference, generally used for basic modules and public modules. After one reference, other modules reference the module, and there is no need to reference other dependencies.

Guess you like

Origin blog.csdn.net/mozushixin_1/article/details/134159900