Import open source libraries into projects built on Android Studio

Import open source libraries into projects built on Android Studio

[[126313]]

Since Google released the official version of Android Studio 1.0, more and more people have started to migrate to Android Studio for development. However, many open source libraries and controls on the Internet are still developed based on Eclipse. Many people don’t know how to import them into their own Android Studio projects. Someone on Weibo sent me a private message asking me to write about it. I happened to be back early today, so I’ll write about it. I’ll mainly introduce some common scenarios for importing packages.

Preface

  1. --project //Project directory  
  2. |
  3. build.gradle //Gradle configuration file for the project  
  4. |
  5. settings.gradle //gradle settings will save all modules
  6. |
  7. app //module directory  
  8. |Configuration of __build.gradle module
  9. |
  10. module2 //module2 directory  
  11. |Configuration of __build.gradle module

Just like eclipse projects, gradle/android studio builds can also have modules. Place the module in the project directory, and then add the module in settings.gradle. The easiest way is to use the folder name. For example, in the structure above, the build.gradle file should be as follows:

  1. include ':app' , ':module2'  

For more information about gralde, please refer to my previous articles:

Using gradle to build android project (continued)

Building Android projects with Gradle

Importing Jar Files

This may be very common. You can download the jar package prepared by others, so that you can directly create a libs folder under your main module (I do this here just to be compatible with eclipse), then put the jar file in it, and then add the following code to the dependencies{} in the module's build.gradle file:

  1. compile files( 'libs/name.jar' )

When there are multiple files under the libs folder, you can include these packages with one line of code:

  1. compile fileTree(dir: 'libs' , include: [ '*.jar' ])

When there are files that do not need to be included, you can do this:

  1. compile fileTree(dir: 'libs' , exclude: [ 'android-support*.jar' ], include: [ '*.jar' ])

From the above code we can see that we can use wildcards, + represents one character, * represents 0 to more characters.

Importing Maven libraries

If the author of the open source library has put the code in the Maven library, we can directly introduce it in the gradle configuration, similar to the following:

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

Generally, we can check whether there is such an address on the github page of the open source library, or search for it in the maven library according to the package name. The project we introduced earlier is divided into three parts: group: name: version. We also follow this rule when we introduce other packages.

Import open source libraries built with gradle

This situation is rarely used, because the author of this open source library usually puts it in the Maven library, but it is occasionally used and mentioned here.

First download the file, copy the module folder of the library we need to the directory of our project, then add the folder name in the setting.gradle file, and then add the following code to the build.gradle file in the module that we need to depend on this module:

  1. compile project( ':libmodule' )

That's it.

Importing open source libraries built on Eclipse

The biggest difference between a project built based on Eclipse and a project built based on Android Studio is the directory structure.

First, we copy the module folder to our project directory, then add this module to the settings.gradle file, and then introduce the dependency in the build.gradle file of the module to be used. From this point of view, it seems that there is no difference from introducing the build based on gradle. However, there is no build.gradle file in the project built based on Eclipse, so we need to create a new one and put it under the module. The following is a template:

  1. apply plugin: 'android-library'  
  2. repositories {
  3. mavenCentral()
  4. }
  5. android {
  6. compileSdkVersion 19  
  7. buildToolsVersion "20.0.0"  
  8. defaultConfig {
  9. minSdkVersion 9  
  10. targetSdkVersion 19  
  11. }
  12. sourceSets {
  13. main {
  14. manifest.srcFile 'AndroidManifest.xml'  
  15. java.srcDirs = [ 'src' ]
  16. resources.srcDirs = [ 'src' ]
  17. aidl.srcDirs = [ 'src' ]
  18. res.srcDirs = [ 'res' ]
  19. assets.srcDirs = [ 'assets' ]
  20. jniLibs.srcDirs = [ 'libs' ]
  21. }
  22. }
  23. lintOptions {
  24. abortOnError false  
  25. }
  26. }
  27. dependencies {
  28. compile fileTree(dir: 'libs' , include: [ '*.jar' ])
  29. }

Of course, the configuration will vary depending on the respective SDK and buildtools versions, etc., as well as other factors. You can refer to my previous article.

other

The above are the main centralized import scenarios. You can change the configuration according to your actual situation.

In addition, the warehouse we imported may not be the Maven central warehouse, or it may be a warehouse we built ourselves. We can customize the warehouse address by modifying the repositories in the build.gradle file, for example:

  1. buildscript {
  2. repositories {
  3. jcenter()
  4. mavenCentral()
  5. Maven {
  6. url "https://oss.sonatype.org/content/repositories/snapshots"  
  7. }
  8. }
  9. }

In addition, the buildscript at the project level will also take effect at the module level, so there is no need to configure it in each module.

Original URL: http://blog.isming.me/2014/12/12/import-library-to-android-studio/

<<:  Google won't fix vulnerability affecting 60% of Android phones

>>:  2015 Spring Festival Homecoming High-end App Recommendation

Recommend

2019 Tik Tok promotion and operation strategy!

20% of the videos account for 80% of the views, b...

My understanding of APP operation and promotion!

Today I would like to discuss some issues with my...

How much does it cost to customize the Wuhai online recharge mini program?

There is no fixed price for the customized Wuhai ...

Nine content marketing tips to help you create killer copywriting!

The prevalence of self-media has made us realize ...

How did a brown bear and a polar bear hybridize? What do their babies look like?

On April 23, 2012, the temperature was -25 degree...

"Influence (New Upgraded Edition)" | Speaker: Han Yan

"Influence (New Upgraded Edition)" | Ha...

What are the reasons for the poor results of Baidu’s bidding promotion?

Customers often visit the official website to inq...

Google discontinues Android Auto mobile service, in-car mobile internet upgrades

Google, which is trying to get a piece of the pie...

Why do people often run counterclockwise?

running It is probably one of the most popular fo...

Should B2B companies create video accounts and live broadcasts?

“Should we also do video accounts and live broadc...

Falling asleep to music? Beware of earworms!

Many people have the habit of listening to music ...

Tick ​​smartwatch first test

The Tick smartwatch, which has completely state-ow...

Pharmacy agency operation implementation plan and model

With the continuous development of China's ph...