Android Modular programming referencing the local AAR

Source: Internet
Author: User

As projects become more and more, the reuse of code becomes extremely important, and it is time to do modular programming, which is to make a separate module of some common components or libraries, and other items to be referenced directly. Android libraries are the most common for Android development, and the way to refer to the Android library before Gradle appears is cumbersome, but with gradle everything becomes very simple and convenient.

Aar

What is AAR? As everyone knows jar file put, if you have an Android library project, you can easily export jar files and then easily reference in other projects, AAR and jar similar, the difference is an Android The jar file exported by the library project cannot contain resource files, such as some drawable files, XML resource files, and so on, so this is a big limitation, before Gradle we refer to Android with resource files The library has to import the entire library into the reference, but with Gradle, the Android Library project can be directly exported to AAR, and then other items are referenced directly and conveniently like the jar.

Exporting AAR

First the Gradle script for the Android Library project only needs to be declared at the beginning

apply plugin: ‘com.android.library‘

After that, and the same method as the export apk file, execute ./gradlew assemblerelease, then you can generate an AAR file in the Build/outputs/aar folder

Referencing a local AAR

How do I refer to a local AAR file after generating AAR? The local AAR file is not as simple as referencing a jar file, and there is no official solution. Fortunately some of the foreign predecessors summed up the method, the following Test.aar file as an example to detail the next method

1. Place the AAR file in a file directory, for example, in the Libs directory

2, add the following content to the app's Build.gradle file

repositories  { flatdir  { dirs   "Libs '  // this  way  we  can  find  the   aar  file  in  libs  folder  }  }  

3, after adding a gradle dependency in other projects, it is convenient to reference the library

dependencies  { compile   ( name  : ' Test '    Ext  :   )  }   

The above method is pro-test effective.

Summarize

Of course, the most common way through gradle is to upload aar to mavencentral or jcenter, which is easier to quote, but for some companies the internal library does not want to be public, and the traditional way of referencing the library is too cumbersome, Referring to the local AAR file is very handy, and then the generic module needs to be made into a library, whether it's updated or modified only to be packaged into AAR and then used by other projects, which is a very effective way to improve code reuse for Android development.

Thanks for sharing, original from: http://stormzhang.com/android/2015/03/01/android-reference-local-aar/

Android Modular programming referencing the local 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.