How To Start Making your own Android Library project with Android Studio
So we’re working on creating Android Material Awesome, a library which will hopefully incorporate 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).
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 or File Explorer, you’ll need to navigate to the module folder. Rename it to match what you named your module you named it e.g. library.
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!
Drag the highlighted file back into Android Studio (into the dark grey space)
4. Add gradle file
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!
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’
apply plugin: ‘com.android.library’
6. Remove applicationId
You’ll also need to remove applicationId in defaultConfig before synchronising again.
The end library build.gradle file
Add Library project into an actual app
Next, we need to open the actual app project that’s going to use our new library.
Firstly, we need to open the settings.gradle file. You need to add two extra lines to include your new module name, and the relative path to the actual files.
include 'awesomematerial' project(':awesomematerial').projectDir = new File(settingsDir, '../Test_Library/awesomematerial')
Note how we’re now also using our new module name, this is where Android Studio would’ve complained if we’d had two modules with the same name!
If Android Studio can’t find your project, you’ll get an incredibly unhelpful error message. Make sure you’ve got the path exactly correct (it also doesn’t need an ending slash).
my settings.gradle file
Finally, all we need to do is to add the project as a dependency into our build.gradle file like so:
compile project (path : ':awesomematerial')
my build.gradle file
If you add or edit any of the code in your library project, it should automatically appear as changed in your project app. No refreshing needed :-)
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. (This was super hard so here’s the follow up post!)