How Android studio uses AAR
I brief introduction
AAR is a file format similar to jar. But there are differences between them.
Jar: contains only class and manifest files, no resource files.
AAR: contains class files and resource files. To put it bluntly, it is the exclusive "jar" of Android
Packaging the code into an AAR file can speed up Android studio to some extent.
In particular, the module is packaged into an AAR file, which has a significant improvement effect.
II How to get AAR
1. AAR file of module
Packaging modules in an Android studio project into AARS is actually very simple.
Under the directory of each module, there will be such a folder: build \ outputs \ AAR.
Under this folder is the AAR file corresponding to the module.
Generally, there are two AAR files, a debug version and a release version.
It's ok if we choose release.
AAR file diagram
be careful:
The new module does not have this folder. At this point, you can generate this folder in two ways
Run the whole project, and the folder will automatically generate execution commands/ Gradlew assemblyrelease can also generate this folder
2. AAR of remote warehouse
The dependent libraries of remote warehouses are often referenced in projects.
At this time, we can also introduce it into the project in the form of AAR.
The AAR that depends on the library is actually easy to find.
After you have configured a dependency library, click sync now.
Android studio will automatically download the library to Disk C (Windows). Just find this file.
Here I use everything to search. It's easy to find the download folder of this library. Other operating systems can search by themselves. In short, it's OK to find the download folder of this project.
The download folder contains everything needed for the project: including AAR, jar and other files.
This process will be explained in detail later with an example
Note:
Everything is a search software that can instantly search the whole.
Is a very practical software. But unfortunately, only the Windows version.
Official website address: https://www.voidtools.com/
III How to use AAR
To use AAR files, you need to go through the following steps:
1. In the build. Of app Add the following configuration to gradle
2. Copy the AAR file to the app / LIBS directory
3. Add AAR reference in dependencies
compile(name: 'zbar-release',ext: 'aar')
IV Leakcanary project example
Because the module example is relatively simple, the remote code base is selected as the example.
Here, a frequently used memory detection project leakcanary is used as an example.
We usually use dependencies to use this library:
debugCompile 'com. squareup. leakcanary:leakcanary-android:1.3' releaseCompile 'com. squareup. leakcanary:leakcanary-android-no-op:1.3'
When sync now is complete, we can find the project folder on drive C (Windows).
The screenshot of the project folder is as follows:
Example diagram of leakcanary project
Below this folder are two dependent libraries we need: leakcanary Android and leakcanary Android no op. click the leakcanary Android folder, and the directory structure is as follows (1.3 is the corresponding version number):
Leakcanary Android folder
Open the innermost folder and you will find that there are three main types of files:
We use a text editor to open the POM file under the leakcanary Android folder:
To learn more about POM files, click here
When we open the leak Canary Android POM file,
We found that leakcanary Android relies on version 1.3 of leakcanary analyzer.
Similarly, we open the POM file of leakcanary analyzer,
It is found that leakcanary analyzer relies on leakcanary watcher and haha.
Leakcanary watcher and haha do not rely on anything.
In this way, the whole leakcanary Android imports four local things:
In the same way, parse leakcanary Android no OP layer by layer, and import all required files into Android studio. In this way, we can truly localize the whole leakcanary.
The two results are compared as follows:
debugCompile 'com. squareup. leakcanary:leakcanary-android:1.3' releaseCompile 'com. squareup. leakcanary:leakcanary-android-no-op:1.3'
Debugcompile (Name: 'leakcanary-android-1.3', ext: 'AAR') compile files ('libs / leakcanary-analyzer-1.3. Jar ') compile files ('libs / leakcanary-watcher-1.3. Jar') release compile (Name: 'leakcanary-android-no-op-1.3', ext: 'AAR') completely converts the entire leakcanary project into local dependencies.
http://www.jianshu.com/p/59efa895589e