Skip to content

Instantly share code, notes, and snippets.

@daniellevass
Last active August 30, 2020 00:48
Show Gist options
  • Save daniellevass/2c46cf33e3814e685b47 to your computer and use it in GitHub Desktop.
Save daniellevass/2c46cf33e3814e685b47 to your computer and use it in GitHub Desktop.
Getting Started with a library project in Android Studio

Getting Started with a library project in Android Studio

So we're working on creating Android Material Awesome, a library which will hopefully incorperate the benefits of Material Design, Twitter's Bootstrap, and FontAwesome. What we really wanted is a project other people can easily include into their projects using gradle dependencies. To do this we needed to create a standalone library project so we could make it as lightweight as possible for including as a dependency, and a sample app that would use it for testing. These are the steps we took to get started in Android Studio (version 1.1).

Create Projects

The first thing we needed to do was to create two new projects, with all the default settings (Blank Activity etc). One for our sample app, and one for our library. We added both of ours into the same GitHub repo, however you can save them wherever you like.

Fix Up Library Project

####1. Rename Module

Initially we'll focus on our library project. Annoyingly Android Studio automatically creates you a module called app. Modules really should be uniquely named, so you'll need to right click on the module name, click refactor and rename to something useful. We called ours library.

####2. Rename Folder

Unfortunately, this doesn't fully rename the root folder. So using Finder (OS X) or File Explorer (Windows), you'll need to navigate to the module folder. Rename it to match what you named your module you named it e.g. library.

Imgur

Android Studio calls my module library, but Finder still says app

####3. Drag build.gradle into Android Studio

This'll break your project! To fix it, you need to go back to Android Studio, and open the build.gradle for the module. It'll have a little warning at the top asking you to add it to the project. Click this and wait for gradle to synchronise again! All your files should magically reappear again!

Imgur

Drag the highlighted file back into Android Studio

####4. Synchronise

You should see a yellow/gold banner appear at the top of your file, click it to add file to your project and wait for Android Studio to synchronise. Once it's done, you should see all your files have magically reappeared again!

Imgur

click the yellow/gold banner at the top to add file to project and synchronise

####5. Make Android Library

Whilst you still have your build.gradle file open, you'll need to change the first line :

apply plugin: 'com.android.application'

to say:

apply plugin: 'com.android.library'

####6. Remove applicationId

You'll also need to remove applicationId in defaultConfig before synchronising again.

Imgur

The end library build.gradle file

Add Library project into useful app

####1. settings.gradle

Imgur

####2. build.gradle

Imgur

Conclusion

Now you can create all the library projects you want to and use them in any of your other apps! We'll probably write another post depending on how hard it is to publish our library into bintray ( https://bintray.com/howbintrayworks )

@pakedge
Copy link

pakedge commented Jun 4, 2015

Sorry this doesn't work for me at all. I created the project, then renamed app to library inside Android Studio, then renamed the folder app to library in finder. Next I tried dragging library/build.gradle into the left hand window inside Android Studio. Nothing happened, no warning message, nothing. Double clicking on the build.gradle inside Android Studio just opens the build.gradle file there. Now what?

@pakedge
Copy link

pakedge commented Jun 4, 2015

ANd where exactly does it make the JAR file? It never makes any JAR file that I can see.

@apradanas
Copy link

I tried this using Android Studio 1.2.1.1

  • I still need to rename module in settings.gradle after step 1
  • I got the same problem with @pakedge after dragging library/build.gradle. No warning message. I simply run Sync Project with Gradle Files (Tools > Android > Sync Project with Gradle Files), and it solved the problem.

@alibehzadian
Copy link

I am using Android Studio 1.2 and I have some problems:
1- I need to refactore files in .idea folder and rename all 'app's to 'mylibrary'.
2- After doing all these steps, my main project does know nothing about library project.
Someone mentioned that your way only works in Android Studio 1.1 and before.

@nabilsouk
Copy link

Thanks a lot!

@ratty3697
Copy link

Error:FAILURE: Build failed with an exception.

  • What went wrong:
    Task 'generateDebugSources' not found in project ':app'.
  • Try:
    Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

@humayunajmal
Copy link

Dragging the build.gradle didn't work for me. I changed ':app' to ':library' in the settings.gradle . Sync again works for me.. but thanks

@ambrice
Copy link

ambrice commented Nov 16, 2015

In Android Studio 1.4.1 I only needed to do steps 1, 5, and 6.

@hitesh-dhamshaniya
Copy link

Thanks for your post it's really helpful.

I had project as you describe, I am facing a problem, I can't use resource of lib project. i.e in Lib project I have a activity with it's layout xml file, but when I try to call that activity from other project which used that libs, I am getting error. something like resource not found.

How can I build lib project so that I can use all resource of lib project in other application.

@pori
Copy link

pori commented Feb 11, 2016

It seems a bit extraneous to rename the module in the Library Project. I understand wanting to have it named library but leaving as-is removes the need for steps 1 through 4.

@hgsingh
Copy link

hgsingh commented Feb 24, 2016

You are a life saver! Thanks!

@HimanshuLikhyani
Copy link

One can directly create an Android Studio Library Project from command line using the "android" tool available in the tools directory of the sdk:
./android - create lib-project -n "mylibrary" -t 1 -k com.himanshu.android -p /Users/himanshu/Desktop/libraryproject -g -v 2.0.0

n = name of the project
t = target name (run "./android list targets" to choose available targets)
k = package name
p = path where you want to create the project
g = use gradle or not
v = if yes, use which version of gradle

Copy link

ghost commented Jul 9, 2016

Nice article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment