How do I import an open source library in an Android studio project?

Source: Internet
Author: User
Tags maven central

Two days ago, Google released the official version of Android Studio 1.0, and more people began migrating to Android studio for development. However, many of the open source libraries, controls, and so on on the web have been developed on an Eclipse-based basis, and many people don't know how to import into their own Android-based Studio projects, and here are some of the most common scenarios for guiding the package.

Objective

As with projects in Eclipse, Gradle/android Studio builds can have module, put Moudle under the project directory, and then add the module to Settings.gradle, the simplest way is to use the folder name. For example, the structure above, the Build.gradle file should read as follows:

Importing Jar files

This can be very common to download to someone else's well-done Jar package so that you can create the Libs folder directly under your main module (I'm doing this just to be compatible with Eclipse), then put the jar file in, and then in the module's Build.gradle Add the following code to the dependecies{} in the widget:

When there are multiple files under the Libs folder, you can include them in one line of code:

This can be done when there are files that do not need to be included:

As you can see from the code above, we can use wildcards, + to represent a character, * to represent 0 to more characters.

Import a library in Maven

If the open Source Library author has put the code in the Maven library, we can introduce it directly in the Gradle configuration, similar to the following:

Generally we can see on the Github page of the open source Library whether there is such an address, or to the MAVEN library according to the package name search there is no, we first introduced the project in three parts group:name:version, we introduce other packages also have to abide by this rule.

Import open source libraries built by Gradle

This situation is less used, because this open source library, the author is generally put into the Maven library, but occasionally also used here also mention.

First download the file, copy the module folder of the library we need into the directory of our project, then add the folder name in the Setting.gradle file, and then in the module we need to rely on Build.gradle Add the following code to the file:

That's all you can do.

Import an open source library built on Eclipse

The big difference between projects built on Eclipse and projects built on Android Studio is that the directory structure is different.

We first copy the module folder to our project directory, then add the module to the Settings.gradle file and then introduce the dependency in the Build.gradle file in the module to be used, so to see, It seems no different from the introduction of Gradle-based builds. However, there are no build.gradle files in the Eclipse-based project, so we need to create a new one to put underneath the module, and here is a template:

Of course, depending on the respective SDK and Buildtools version and so on, as well as other, the configuration will change, you can see my previous article.

Other

The above is the main centralized import scene, you can according to their own situation and then change the configuration and so on.
In addition, we import the warehouse may not be the MAVEN central Warehouse, or maybe we built a warehouse, we can customize the warehouse address, modify the Build.gradle file in the repositories, such as:

In addition, the buildscript of the project layer will be in effect at the module layer, so it is not configured in each module.

How do I import an open source library in an Android studio project?

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.