Android Studio packaging and referencing AAR

Source: Internet
Author: User

Android Studio packaging and referencing aar1, overview

In the development of larger Android projects, we often encounter the way in which projects, jar packages, and so on are referenced to each other. In general, we resolve this by configuring dependencies in the Gradle file, such as:

Some of the basic dependencies of Gradle are configured as follows :

    • compile fileTree(dir: ‘xxx‘, include: [‘*.jar‘, "*.xxx"]): All files that match the extension in a directory are dependent;
    • compile ‘com.xx.xx:ProjectName:Version‘:配置Maven ' library as a dependency; in the MAVEN Repository center you can search for the libraries you want to use.
    • compile project(‘:AnotherModule‘): Configuring another module as a dependency of this module, the dependent module must be imported into the current project;
    • compile files(‘xxx.jar‘): Configure a jar package as a dependency.

It looks good, and the basic generic configuration already exists. Generally for medium and small projects, this kind of development method is completely no problem. But sometimes a and B two projects, want to refer to another common module C, and this module may be a more complex Android module, may contain a number of topics, UI, resource files, and so on, if the module relies on the way to configure, unavoidably Some difficulties, because both A and B are imported C, and you should always be concerned about C changes.

Fortunately, Android Studio provides a aar way to package the library, we can library package the C as, output AAR files, and then in a and B, configure the AAR library dependency, can be resolved.

2. Introduction to AAR files

To output an AAR file, the Module must be configured as a library, as in the Gradle file:

    • Output AAR: apply plugin: ‘com.android.library‘ ;
    • Output apk: apply plugin: ‘com.android.application‘ .

Once the Module is configured as a library, the build outputs an AAR file that can be found in the appropriate directory, depending on the channel and BuildType. For example, the configuration of BuildType for debug, the output is: [ModuleName]/build/outputs/aar/[ModuleName]-debug.aar . An AAR file is actually a zip package, and unlike a jar, it packs some resource files, third-party library files, so files, and so on, while the code files are compiled and compressed classes.jar in. Like what:

3, the way to import AAR reference

This method is relatively simple, open Project Structure , add a new Module, and then select Import *.JAR or *.AAR Package the way to import:

After the import, under your project, a folder will be generated, which is the AAR file and the configuration file for Android Studio.

You can then configure dependencies in the Gradle, and the other module can refer to this module, depending on how it compile project is used.

缺点:The dependent AAR cannot be F3, unable to see the contents of the resource file and the directory hierarchy and so on.

4. Referencing in a way that uses configuration dependencies

Gradle there is actually another dependency that can refer to AAR:

    • compile(name: ‘xxx‘, ext: ‘aar‘)

The AAR file needs to be placed in the Libs directory of the reference Module, similar to the generic jar file, and then added to the Gradle dependency configuration, to build the project, in the Module build/intermediates/exploded-aar directory, you can see some temporary file generation:

It looks like a full reference to the extracted version of AAR. Android Studio After installing the anti-compilation plug-in, you can follow through the F3 to the class file, if you have to be dependent on the Module source code, you can also Attach Source associate the sources to view. In addition, you can easily view the resource files in the AAR.

In addition, this dependency mode updates the AAR, the resulting temporary files will also change, do not worry about the changes are not synchronized problems.

5. Summary

In summary, the method of generating AAR files is introduced, and the two methods of AAR dependence are verified by actual combat, and the second one is simple and easy to use by configuring Gradle dependency, which can be done by a single line of code.

Android Studio packaging and referencing AAR

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.