Methods for importing open source libraries in the Android Studio project

  • 2020-06-23 01:53:12
  • OfStack

Two days ago, Google released the official version of Android Studio 1.0 and more people started migrating to Android Studio. However, many open source libraries and controls on the Internet are still developed based on Eclipse before. Many people do not know how to import them into their Android Studio project. There are also private messages on Weibo asking me to write. This paper mainly introduces 1 common 1 some guide package scenarios.

preface


--project   // Project directory
  |
  build.gradle  // The project gradle The configuration file
  |
  settings.gradle / / gradle That will save all of them module
  |
  app    //module directory
  |__build.gradle module The configuration of the
  |
  module2  //module2 directory
  |__build.gradle module The configuration of the

As with item 1 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, in our structure above, the build. gradle file should look like this:

include ':app', ':module2'

Import the Jar file

It is possible to download the jar package and create the libs folder directly under your own main module (just to be compatible with eclipse), put the jar file in, and add the following code to dependecies{} in build.gradle:


compile files('libs/name.jar')

When there are multiple files under the libs folder, you can include these packages in 1 sentence:

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

When a file does not need to be included, you can do this:

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

As you can see from the code above, we can use wildcards, with + for 1 character and * for 0 to more than one character.

Import the libraries 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, as follows:


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

In general, we can see if there is such an address on the github page of the open source library, or search the maven library for it by package name. The project we introduced earlier has three parts, group:name:version. We also introduced other packages according to this rule.

Import the open source library built by gradle

This is rarely used, because this open source library, author 1, is generally included in the maven library, but occasionally it is used.

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


compile project(':libmodule')

That will do.

Import the open source library built on Eclipse

A big difference between projects built on Eclipse and those built on Android Studio is the directory structure.
We first copied the module folder under our project directory, then added the module to the ES91en.gradle file, and then introduced the dependency to the ES95en.gradle file in the module to use, which seems to be no different from the gradle-based build. However, there is no build.gradle file in the project based on Eclipse, so we need to create a new one to put under module. 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, there will be configuration changes depending on the respective sdk and buildtools versions and so on, among others, check out my previous posts.

other

The above is the main centralized import scenario, you can change the configuration according to your actual situation, and so on.

In addition, the warehouse we imported may not be the maven central warehouse, or it may be a warehouse built by ourselves. We can customize the warehouse address and modify repositories in ES115en. gradle file.


buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven {
            url "https://oss.ofstack.com/content/repositories/snapshots"
        }
    }
}


Related articles: