Mobile development

Alex Zhukovich

Import an external module to Android Project in Android Studio


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

As example you have folder Projects with two subfolders FirstApplication and simpledateformatter. In this case 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 simplest way it’s import your additional module via IDE. (File / New / Import module OR File / Project Structure / + ) Unfortunately it’s not a better way 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 a better ways for your module, because probably you want to use you additional module not just for one project. In this case better 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.

For it need to open settings.grade file in “Main project” and need to add some lines:

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

After it need to add dependency to build.gradle file in your project, for it 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

Of course you must adapt path to your additional module to your situation. As example in situation where you have your Main project and additional module in the same folder all 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 builg.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: Of course you have some other ways for use last version of you library

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

I hope it was helpful for you. If you want get more article about mobile development join to Facebook community or follow me.
Thank you for your time and have a nice day.

Share Share on Reddit0Share on VKTweet about this on TwitterShare on LinkedIn0Share on Google+29Share on Facebook3Flattr the authorEmail this to someoneShare on Tumblr0
« »

© 2017 Mobile development. Theme by Anders Norén.