Import an open source library to a project built on Android Studio

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  //gradle configuration file for Project  |  Settings.gradle//gradle settings, all module will be saved  |  App    //module directory  |__build.gradle module Configuration  |  Module2  //module2 Directory  |__build.gradle module configuration

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 with Gradle (cont.)
Build Android Projects with Gradle

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 file dependecies{} Add the following code:

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 that * represents 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, our previous introduction of the project is divided into three parts group:name:version, We have introduced other packages that are also subject to 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:

Apply plugin: ' Android-library ' repositories {    mavencentral ()}android {    compilesdkversion    Buildtoolsversion "20.0.0"    defaultconfig {        minsdkversion 9        targetsdkversion    }    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 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.

Original address: http://blog.isming.me/2014/12/12/import-library-to-android-studio/, reproduced please indicate the source.

Import an open source library to a project built on Android Studio

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.