Android – package aar steps and the most critical precautions!

### Introduction Recently, due to the requirements of the project, the developed modules need to be packaged into aar for other projects to call. After working for a period of time, I found that there are still a lot of things to pay attention to, so record it and help everyone. Don’t make detours. **First of all, what is the aar package? ** ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190911084035526-598653323.png) The simple difference between jar and aar:-*.jar: only contains class files and manifest files , Does not include resource files, such as pictures and other files in all res. -*.aar: Contains all resources, class and res resource files contain all ### New project (no dependencies) Steps to package AAR 1. Create a new Library ![](https://img2018.cnblogs.com/blog/1312938 /201909/1312938-20190911084036712-1060300939.png) ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190911084037090-417644686.png) Then all the way next+finish will be created successfully and will be packaged The files are placed in your Module according to the location of the folder, and then directly click build–>Make Moudle app ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190911084037615-1666035034 .png) After successful packaging, aar will be generated under the corresponding path![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190911084037922-143770850.png) Of course, these should not be We need it, because there will be some dependencies in the development project. And it’s definitely not an empty project. ### How to quickly package AAR for the formed project (with dependencies) We can directly create a Moudle in the project and copy the project to Moudle, or re-copy a current project, and then directly modify the project in the copied project Into AAR. Let me talk about the second method, which is to directly modify the copied project to AAR. We need to make the following changes: **1.** Modify the bulid gradle file under the app: Apply plugin:’com.android.application’ Modify it to apply plugin:’com.android.library’ ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190911084038264-1758201364.png) **2.** Comment under defaultConfig ApplicationId ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190911084038601-887148425.png) 3.AndroidManifest.xml file, modify application ![](https://img2018.cnblogs .com/blog/1312938/201909/1312938-20190911084038871-1587446694.png) and comment and adjust the first activity started action “` “` 4. If you customize the Application yourself, you need to remove it because it is in AAR If there is no Application, then the problem is coming. What should we do with the initialization code in the Application? This is simple, we can write a separate Options class, provide it to the caller, and let him pass to the application, so that we can do some initialization operations in this class. ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190911084039249-298957270.png) The caller passes in application in his application: ![](https://img2018.cnblogs .com/blog/1312938/201909/1312938-20190911084039845-2110291686.png) Then directly build–>Make Moudle app to generate aar! ### Precautions **1.** The main project needs to rely on the remote library that AAR depends on, otherwise ClassNotFound exception will occur. Here maybe your version of a dependent library will conflict, which requires you to coordinate **2 .** It is best to prefix all the resource names with your project name, such as image resources, string, color, dimens, layout, etc. Anyway, all files in the res directory should be named with a uniform prefix to prevent them from being linked to the host app The following resources are duplicated, because the aar reference has the same effect as the source code reference. It is easy to have the problem of resource duplication, so it is very necessary to add a prefix. **3.** If the aar package contains third-party libraries such as WeChat payment and sharing, you need to use it in the main project. Remember to replace applicationId in gradle, or use the package name and key of your main project to get the third party Operation key and id Take sharing as an example. If the shared KEY in your manifest file is different from the shared KEY in the build gradle file of the main project, the package manifest file will be abnormal. **4.** Butterknife needs to use the version above 8.4.0, otherwise an error will be reported; according to my suggestion, it is best not to use it, otherwise you will feel better! **5.** If a class manager is used in aar’s MyAppliction, then the code needs to be extracted to be an independent class manager when packaging into aar. Remember, after packaging into aar, all and application Relevant things must be checked and replaced to avoid null pointer exceptions. **6.** AAR module using butterknife needs to change the resource R to R2 (batch replacement) **7.** Do not have the same name for the layout file, otherwise it will report an exception that the Id cannot be found. Let’s analyze it here today, next time Continue with new content! ![](https://img2018.cnblogs.com/blog/1312938/201909/1312938-20190911084040101-41244044.jpg)

Leave a Comment

Your email address will not be published.