How to import open source libraries in an Android studio project _android

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 online open Source Library, control, etc. or the previous development based on eclipse, many people do not know how to import into their own based on the Android studio project, micro-blog also someone dms me, let me write, just come back earlier today, write it. The main introduction of some of the common guide package scene.

Objective

Copy Code code as follows:

--project//project directory
|
Gradle configuration file for Build.gradle//project
|
Settings.gradle//gradle Settings, all of the module will be saved
|
App//module Directory
Configuration of |__build.gradle Module
|
Module2//module2 Directory
Configuration of |__build.gradle Module

As with projects in Eclipse, Gradle/android Studio builds can also have module, place moudle under the project directory, and then add the module to Settings.gradle, the easiest way is to use the folder name. For example, our structure above, the Build.gradle file should read as follows:
Copy Code code as follows:

Include ': App ', ': Module2 '

Import Jar File

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

Copy Code code as follows:

Compile files (' Libs/name.jar ')

When you have more than one file under the Libs folder, you can include the packages in a single line of code:
Copy Code code as follows:

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

This can be done when there are files that do not need to be included:
Copy Code code as follows:

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

You can see from the above code that we can use wildcards, + to represent one character, * representing 0 to many characters.

Import a library from Maven

If the source library author has put the code into the MAVEN library, we can introduce it directly in the Gradle configuration, similar to the following:

Copy Code code as follows:

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

In general, we can see in the Open Source Library GitHub page for such an address, or to the MAVEN library according to the package name search is not, we introduced this project in three parts of the group:name:version, we introduce other packages also adhere to this rule.

Import open source libraries built by Gradle

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

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

Copy Code code as follows:

Compile project (': Libmodule ')

That's it.

Import an open source library built on eclipse

A big difference between the projects built on Eclipse and the projects built on the Android studio is that the directory structure is different.
We first copy the module folder under our project directory, Then add this module to the Settings.gradle file, and then introduce dependencies into the Build.gradle file in the module you want to use, so it seems like the introduction of Gradle builds is no different. However, there are no build.gradle files in the project built on eclipse, so we need to create one for ourselves and put it under module, and here is a template:

Copy Code code as follows:

Apply plugin: ' Android-library '
repositories {
Mavencentral ()
}
Android {
Compilesdkversion 19
Buildtoolsversion "20.0.0"
Defaultconfig {
Minsdkversion 9
Targetsdkversion 19
}
sourcesets {
Main {
Manifest.srcfile ' Androidmanifest.xml '
Java.srcdirs = [' src ']
Resources.srcdirs = [' src ']
Aidl.srcdirs = [' src ']
Res.srcdirs = [' res ']
Assets.srcdirs = [' Assets ']
Jnilibs.srcdirs = [' Libs ']
}
}
lintoptions {
Abortonerror false
}
}
dependencies {
Compile Filetree (dir: ' Libs ', include: [' *.jar '])
}

Of course, depending on the SDK and Buildtools version, and so on, and so on, the configuration will change, and you can look at my previous article.

Other

The above is the main centralized import scene, oneself can according to own actual situation then change configuration and so on.

In addition, the warehouse we import may not be a MAVEN central repository, or maybe we built our own warehouse, we can customize the warehouse address, modify the repositories in the Build.gradle file, for example:

Copy Code code as follows:

Buildscript {
repositories {
Jcenter ()
Mavencentral ()
Maven {
URL "Https://oss.jb51.net/content/repositories/snapshots"
}
}
}

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.