Local dependencies

In some cases, you might still need to manually download a JAR file or a native library. Perhaps you want to create your own library that you can reuse in several projects, without publishing it to a public or private repository. In those cases, it is impossible to use any of the online resources, and you will have to use different ways to add the dependencies. We will describe how to use file dependencies, how to include native libraries, and how you can include library projects in your project.

File dependencies

To add a JAR file as a dependency, you can use the files method that Gradle provides. This is what it looks like:

dependencies {
    compile files('libs/domoarigato.jar')
}

This can get tedious if you have a lot of JAR files, so it might be easier to add an entire folder at once:

dependencies {
    compile fileTree('libs')
}

By default, a newly created Android project will have a libs folder, and declare it to be used for dependencies. Instead of simply depending on all files in the folder, there is a filter that makes sure that only JAR files are used:

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

This means that in any Android project that is created in Android Studio, you can drop a JAR in the libs folder, and it will automatically be included in the compile classpath and the final APK.

Native libraries

Libraries written in C or C++ can be compiled to platform-specific native code. These libraries typically consist of several .so files, one for every platform. The Android plugin supports native libraries by default, all you need to do is create a directory called jniLibs on the module level, and create subdirectories for each platform. Drop the .so files in the applicable directory, and you are good to go.

Your structure should look like this:

app
├── AndroidManifest.xml
└── jniLibs
    ├── armeabi
    │   └── nativelib.so
    ├── armeabi-v7a
    │   └── nativelib.so
    ├── mips
    │   └── nativelib.so
    └── x86
        └── nativelib.so

If this convention does not work for you, you can just set the location yourself in the build file:

android {
    sourceSets.main {
        jniLibs.srcDir 'src/main/libs'
    }
}

Library projects

If you want to share a library that uses Android APIs, or includes Android resources, you need to create a library project. Library projects generally behave the same as application projects. You can use the same tasks to build and test library projects, and they can have different build variants. The difference is in the output. Where an application project generates an APK that can be installed and run on an Android device, a library project generates a .aar file. This file can be used as a library for Android application projects.

Creating and using library project modules

Instead of applying the Android application plugin, the build script applies the Android library plugin:

apply plugin: 'com.android.library'

There are two ways to include a library project in your application. One is to have it as a module inside your project; another is to create a .aar file, which can be reused in multiple applications.

If you set up a library project as a module in your project, you need to add the module to settings.gradle and add it as a dependency to the application module. The settings file should look like this:

include ':app', ':library'

In this case, the library module is called library, and this corresponds to a folder with the same name. To use the library in the Android module, a dependency needs to be added to the build.gradle file of the Android module:

dependencies {
    compile project(':library')
}

This will include the output of the library in the classpath of the application module. We will look at this approach in more detail in Chapter 5, Managing Multimodule Builds.

Using .aar files

If you create a library that you want to reuse in different Android applications, you can build a .aar file, and add it to your project as a dependency. The .aar file will be generated in the build/output/aar/ folder of the module's directory when building the library. To add the .aar file as a dependency, you need to create a folder in the application module, copy the .aar file to it, and add the folder as a repository:

repositories {
    flatDir {
        dirs 'aars'
    }
}

This will make it possible to add any file inside that folder as a dependency. You can reference the dependency as follows:

dependencies {
    compile(name:'libraryname', ext:'aar')
}

This tells Gradle to look for a library with a certain name that has the .aar extension.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset