Gradle

Import an external module to Android Project in Android Studio

Alex Zhukovich 2 min read
Import an external module to Android Project in Android Studio
Table of Contents

Today I want to talk about an importing external module to Android project in Android Studio.

Let's imagine, you have a "Projects" folder with two subfolders (FirstApplication and simpledateformatter). In this case a simpledateformatter it’s just a module which you want to use in the FirstApplication.

Example of project folder

Import library via IDE (Android Studio)

First and the simplest way it’s import your additional module via IDE. (File -> New -> Import module OR File -> Project Structure -> "+"). Unfortunately it’s not the best way to do it because you can find copy of your additional module into main project folder.

Imported module via IDE

Of course, it’s working correctly and you can use and modify your library, but in this case you will update just module into subfolder of you main project. It’s not the best ways for your module, because probably you want to use you additional module not just for one project. In this case, the best way it’s configure gradle files for using additional folder without copy whole module to you project.

Configure gradle from import project

You can configure gradle files in your project(s) and your applications can use one folder with you additional project without creating subfolders into project folder. It’s better way for support you additional module, because if you add some changes to your additional library all your project will use this module with all changes.

We need to open settings.grade file in "Main project" and we need to add few lines:

include ':simpledateformatter'
project(':simpledateformatter').projectDir = new File(settingsDir, '../simpledateformatter')

After it, we need to add a dependency to build.gradle file in the project, we need to add to this files next line:

compile project(':simpledateformatter')

After these changes, you will use additional module from default folder of additional module.

Imported module in Android Studio

You need to adapt path to your additional module to your situation. In situation where you have your "Main project" and "additional module" in the same folder everything works correctly.

Example of project folder

Full source code of settings.gradle file:

include ':app'
include ':simpledateformatter'
project(':simpledateformatter').projectDir = new File(settingsDir, '../simpledateformatter')

Full source code of build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.alexzh.tutorial.firstapplication"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile project(':simpledateformatter')
}

Note: You have other ways for use last version of you library:

  • repositories (maven , jcentral, etc)
  • aar, jar files

Mobile development with Alex

A blog about Android development & testing, Best Practices, Tips and Tricks

Share
More from Mobile development with Alex

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Mobile development with Alex.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.