Use Android Studio to create AAR package and reference it in other projects

Use Android Studio to create AAR files and reference them in other projects

AAR (Android Archive) is the Android archive file, which can package all .class files, manifest files, resource files, etc., for reference by other projects.

Below, take the creation of a custom control and make it into an AAR package (.aar suffix) for other projects to call (use Android Studio version 4.1.1) as an example:

1. Create an Android project, the project name is CustomControl.
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

2. Create a new module (File -> New -> New Module), select the module type as Android Library and the module name as customlibrary.
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

3. Create a directory named res and its related directories (such as drawable, layout, values, etc.) to store the corresponding resource files.
Insert picture description here
Insert picture description here

4. Create a custom control CustomView layout and Java files (including a TextView and an EditView).
Insert picture description here
Insert picture description here
Insert picture description here

5. Compile into an AAR package (click to run the assemble of the corresponding module under Gradle, and you can see that the release version and debug version of the AAR package are generated in the directory of the corresponding module).
Insert picture description here
Insert picture description here

6. Create a new project named TestAar, and copy the AAR package generated before to the libs directory of the project.
Insert picture description here
Insert picture description here

7. Import the AAR package (File -> New -> New Module -> Import .JAR/.AAR Package).
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

8. Quote the AAR package in the project (File -> Project Structure).
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

9. After quoting, you can see that dependencies are automatically added in build.gradle.
Insert picture description here

10. Then you can call the custom control CustomView in the AAR package in the project.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42267486/article/details/109920430