Import Open Source Library detailed steps tutorial in the Android Studio project

Source: Internet
Author: User
Tags maven central

In this article, we will detail how to import an Eclipse based open Source library into the Android studio project.

Preface

--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:

Include ': App ', ': Module2 '

More information about Gralde can be seen in my previous articles:

Building an Android project using Gradle (cont.)

Building an Android project with Gradle

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:

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:

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 '])

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:

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:

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 to our project directory, then add the module to the Settings.gradle file, and then introduce dependencies in the Build.gradle file in the module you want to use, so see It seems to be no different from the introduction of Gradle based construction. 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:

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:

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

In addition, the buildscript of the project layer also takes effect at the module layer, so it is not configured for each module.




How does Android Studio import Jar,so, as well as third party open source libraries?

Q: Many of the active projects on GitHub have started to be written with Android studio, and this trend seems unstoppable. So I started moving from the Eclipse camp to studio. Just started playing studio all kinds of not adapt, I hope you can greatly give a detailed tutorial ...

Answer one:

There are a variety of problems with importing Android studio in general with eclipse-generated projects, but it's not difficult to fix, just a few more steps to use:

1. After you import the main project, you need to set various paths in the Modules option in File-> project Strucure, such as your SRC, libs, Gen, bin, androidmanifest.xml path, etc.

2. Project in eclipse becomes a module on as, so you introduce the library project in import module, and then you may repeat step 1 to set the path for the library project.

3.jar files are still placed in the Libs folder and then need to be introduced in the Libraries option in Project Strucure

4. The library project and various jars are in, but the main project still complains, this may be because the main project and the dependencies of these library files are not related, or the main project strucure, select Dependencis, Add both the module and the library you need to use

So the key is still in the settings for Project Strucure:)

Another project built with guide, the benefits of what is not in detail, a lot of online search, tutorials can see the use of Android official website guide, interested to know the next


Answer two:

General Android projects imported from Eclipse to Android Studio (AS), there are various problems, the most important thing is "gradle" so far does not support. So library files are packaged into APK.
that is, if you are using a third party library that contains the. so file (typically libs\armeabi\ xxx.so), just "system.loadlibrary" ("XXX") appears in the Code; When the code appears, it will definitely throw an exception unsatisfiedlinkerror here. The

solution is as follows: According to my Google 2 days of results, a more satisfactory and all the people who appear this problem is feasible:
1) in any location on the hard disk to establish an empty folder "Lib" (note that the name is "Lib" rather than "Libs")
2) to the original Copy of the Armeabi folder in the Libs directory to the newly established Lib directory. (The. So file in the Armeabi folder is also copied)
3) The newly created "Lib" directory is packaged into a. zip file and renamed "Armeabi.jar"
4 to put Armeabi.jar in the original Libs directory (and other Three-party jar packs together

After these 4 steps, just update the third party library of the next as, OK.

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.