Methods for referencing local AAR files in Android projects _android

Source: Internet
Author: User

As the project becomes more and more, the reuse of code becomes more and more important, so it is necessary to do modular programming, that is, to make some common components or class libraries into a separate module, other projects directly to the reference. The Android library is the most common for Android development, and it's cumbersome to quote the Android library before Gradle, but with gradle everything becomes very easy.

Aar

What is AAR? Everyone knows jar file put, if you have an Android library project that can easily export jar files and then easily referenced in other projects, AAR and jars are similar, and 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 there are a lot of limitations, and before Gradle we're going to refer to Android with resource files. The library must import the entire library into the reference, but with Gradle, the Android Library project can be exported directly to AAR, and other projects are directly and conveniently referenced as a way to reference jars.

Export AAR

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

Copy Code code as follows:

Apply plugin: ' Com.android.library '

It then executes the./gradlew assemblerelease in the same way as exporting the APK file, and then you can generate an AAR file in the Build/outputs/aar folder

Reference to the local AAR

The next step after the AAR is generated is how to refer to the local AAR file? The local AAR file is not as simple as referencing the jar file, nor does the official offer a solution. Fortunately, some foreign predecessors summed up the method, the following to the Test.aar file as an example to detail the following methods

1, the Aar file in a file directory, such as the Libs directory

2, in the app Build.gradle file add the following content

Copy Code code as follows:

repositories {
Flatdir {
Dirs ' Libs '//this way we can find the. aar file in Libs folder
}
}

3, then in other projects to add a gradle rely on the convenience of quoting the library
Copy Code code as follows:

dependencies {
Compile (name: ' Test ', ext: ' AAR ')
}

The above method is effective.

Summarize

Of course, the most common way to pass the gradle is to upload aar to mavencentral or Jcenter, which is more convenient to quote, but for some companies inside the library do not want to open, and the traditional way of quoting the library is too cumbersome, It would be convenient to refer to a local AAR file, and then a generic module would simply have to be made into a library, and it would be a great way to improve code reuse for Android development, regardless of whether updates or modifications need to be packaged into AAR and then used by other projects.

Related Article

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.