34

This question might have been answered somewhere but couldn't find the appropriate one.

I want to know how can I create a common utility library project in Android Studio. I want to write some common classes and some common methods within them to use in Android app projects. Probably like how .dll are in Windows world - a set of common methods that can be shared among multiple consumers.

Thanks in advance.

Harshad Kale
  • 17,446
  • 7
  • 30
  • 44
  • created an example and shared in this answer: http://stackoverflow.com/questions/22243269/how-to-share-a-single-library-source-across-multiple-projects#answer-33876826 – Kevin Scheidt Nov 23 '15 at 17:26

6 Answers6

27

Simplest way to do this :

  1. Right click on your opened project in Android Studio and select New > Module

  2. In the left Pane choose Android Library and click on next.

  3. Enter all details, untick Create Activity, Theme and all if not required.

  4. Choose API level same as your project and Next, Next, Next .

Now you will see an another directory inside your project, build.gradle for library will be automatically configured for you.

If your module/library name is "mylibrary",

include ':mylibrary' 

will be automatically added in settings.gradle file inside root directory of your project.

Now open your main module and insert this line in dependency block :

compile project(':mylibrary')

If you want to use same library in other projects, you have to copy the library module to that particular project using File Explore and have to configure settings.gradle and main module's build.gradle manually.

Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • 1
    Thank you pyus13 and kalehv for the question/answer. – Will Jul 01 '14 at 05:06
  • 1
    I am still waiting for a green tick, it will help other to trust on my answer. – Piyush Agarwal Jul 01 '14 at 13:30
  • 14
    So if I want to reuse a library in several projects, I have to copy the code in each of those projects ? I have been using a library to avoid that in the first place... I can work around this with a symbolic link, but is there any better solution (as in Eclipse, where the library project is in a separate folder outside the project) ? – personne3000 Aug 24 '14 at 10:15
  • 1
    OK figured it out I think: from what I understand in Android studio, sharing a library between several apps is done by creating all the apps as separate modules in the same project. Use New->Module to add apps, and you can rename the default "app" module with Refactor->rename (do it twice for module&folder name and update settings.gradle). – personne3000 Aug 24 '14 at 10:23
19

An old question, but even now there doens't seem to be a propper solution for libraries in AndroidStudio. I've been looking into making the migration step for some time now and this is what I found.

My 'solution'

Lets say we've got a Library (called lib) which contains shared code and an Application project (called app) which wants to use said library's code.

The key is defining a project in settings.gradle in the Project's root (/Some/Path/MyProjects/Project/settings.gradle) This file should already exist and contain something like include ':app'.

We will modify this file to also include the library AND define the library with the following two lines:

/Some/Path/MyProjects/Project/settings.gradle

...
// tell gradle we want to include a module called 'lib'
include 'lib'
// tell gradle there is a module called 'lib', found in a specific directory
// Note the /app on the end, the project structure wraps a project around a module, you want to refer that module!
project (':lib').projectDir = new File('/Some/Path/MyProjects/Library/app')
...

Also edit the projects build.gradle /Some/Path/MyProjects/Project/app/build.gradle to depend on the newly added module lib

/Some/Path/MyProjects/Project/app/build.gradle`

...
dependencies {
    ...
    compile project (':lib') // we want to depend on our library
}
...

Extra

When working with multiple developers or for the sake of flexibility, I use my gradle.properties in my .gradle directory (for *nix based systems usually found in homedir, not sure where Windows looks for it). Do note that you might need to create the directory and file yourself.

In this file you can create, if you like, constants that can be used by you throughout your gradle files. For example, mine contains something like the following:

theconstant=/Some/Path/MyProjects/Library/app

note the seemingly missing quotes (not sure whether thats really needed tho) Now we can replace

project (':lib').projectDir = new File('/Some/Path/MyProjects/Library/app')

with

project (':lib').projectDir = new File(theconstant)

Now you and your team could define this constant (which might differ per machine) and change the path accordingly.

pros

  • No copying of whole libraries anymore!
  • Flexibility, you can edit the library in the project's window
  • Multiple developers working on the same projects can define their own paths
  • Library gets compiled at the projects' compile time (BuildConfig.DEBUG!)

cons

  • None so far

I havn't found the chance to properly test this through, yet this seems like the most elegant solution for the time being! I would like to hear your thoughts on this.

justinvdk
  • 172
  • 2
  • 11
  • This is the con. "project (':lib').projectDir = new File('/Some/Path/MyProjects/Library/app')" The path is not going to be same for everyone if the library is in another git repository. – Prakash Nadar May 11 '16 at 12:43
  • Hence using the constant, which can be set for each developer individually :) We're still using this as "it works" for us. I'm not sure whether better options already exist – justinvdk May 11 '16 at 13:55
2

I'm just doing:

include '..:commons'

in settings.gradle

and:

  compile project(':..:commons')

in build.gradle of the referencing project

so you can place your commonly used lib outside the project

stefan
  • 1,336
  • 3
  • 21
  • 46
1

Your build.gradle should look something like this

apply plugin: 'android-library'

repositories {
    mavenCentral()
} 
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}

android {
compileSdkVersion 18
buildToolsVersion '18.1.1'

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        res.srcDirs = ['res']
    }
}
}

Look at the first line

Bri6ko
  • 1,858
  • 1
  • 18
  • 29
0

when you create a new project in Android Studio, one of the options is "mark as library".
alternatively, just create the project like any other. then when using it elsewhere, in the library's gradle file make sure you change apply-plugin "android" to apply-plugin "android-library" and add it as a dependency to the app that's using it. For more on how to add the created project as a library, see here.

Community
  • 1
  • 1
adityajones
  • 601
  • 1
  • 4
  • 10
0

Simply create it as a normal project, if you want to use it as library open project structure in the app that wants to use it and add the intended library under Dependencies.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
  • Under Dependencies, it asks me Maven/File/Module dependency and none of which gives me option to locate it on my file system. Am I missing something or not understanding? I am new to Android as well as Java. – Harshad Kale Jan 23 '14 at 04:02
  • The project you want to use as library should already be open in Android studio.This is the best way to locate it easily in your `project structure` – Ojonugwa Jude Ochalifu Jan 23 '14 at 04:14
  • file dependencies (i.e. jar files) have to be under your project root, so you'll need to copy it into your project's directory structure. If it's a module dependency, then you'll need to add that module to your project and copy the code in. Right now the only way to have a common library that's outside your project root is to publish it to a local Maven repository. – Scott Barta Jan 23 '14 at 23:24