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, a lot of open source libraries, controls, and so on the previous development based on eclipse, many people do not know how to import into their own Android-based studio projects, micro-blog also someone private messages to me, let me write, just came back earlier today, write it. The main introduction of some of the common guide package scenarios.

Objective
--project//project directory |build.gradle//project Gradle configuration file |settings.gradle//gradle settings, all Module|app//module directories will be saved |__ Configuration of the Build.gradle module |module2//module2 directory |__build.gradle module

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:

Include ':app ', ': Module2 '

More about Gralde's knowledge can be seen in my previous articles:

Build Android projects using Gradle (cont.) build Android projects with Gradle

Importing JAR files

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

Compile files (' Libs/name.jar ')

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

Compile Filetree (dir: ' Libs ', include: [' *.jar '])

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

Compile Filetree (dir: ' Libs ', exclude: [' Android-support*.jar '], include: [' *.jar '])

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:

Compile ' com.github.dmytrodanylyk.android-process-button:library:1.0.1 '

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 our project directory, and then add the folder name in the Setting.gradle file. Then add the following code to the Build.gradle file in the module that we need to rely on for this model:

Compile project (': Libmodule ')

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 will first copy the module folder to our project directory, Then add this module to the Settings.gradle file and then introduce dependencies into the Build.gradle file in the module that you want to use, so it seems to be no different from the introduction of Gradle-based constructs. 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:

Buildscript {repositories {jcenter () mavencentral () Maven {URL "Https://oss.sonatype.org/content/repositories/snapshots"}}}

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.