You’re working on your Android application and realize you have a nice piece of reusable encapsulated logic you’d like to factor out into a library. You still want to be able to develop this library and your application in parallel, so they should be on disk together and somehow linked. So what’s the best way to accomplish this?

Like my previous post, this is a bit of a challenge because there’s no way to do this out of the box, so I’m capturing my learnings here.


After this, you’ll have a second project (for the library) that will contain two modules: the library logic itself, and another for the examples. When you make a change to the library, you’ll need to republish it, and then refresh Gradle on the application side. Publishing will publish to a local Maven repository – no actual uploads will take place.

I won’t be covering how to take this and publish to a real repository – there’s plenty of those online already.

Step by Step

  1. New Project: Create a new project (File > New > New Project...). The Project Location can be anywhere but inside another project. Name and domain are up to you, of course. The name isn’t that significant – this is the name of your example module. But the domain should at least be accurate.
  2. New Module: Create a new module within that project (File > New > New Module...). Choose “Android Library”. Remaining settings are up to you.
  3. Apply Plugin: At the top of your library’s build.gradle file, but below the other plugins already present, add apply plugin: 'maven-publish'. This plugin will enable you to publish to a Maven repository, even local ones.
  4. Publish module: At the end of your library’s build.gradle file, add the following:

     publishing {
         publications {
             library(MavenPublication) {
                 // Don't forget to change these
                 groupId 'name.maxaller'
                 artifactId 'easynsd'
                 version '0.1'
                 pom.withXml {
                     def dependenciesNode = asNode().appendNode('dependencies')
                     configurations.compile.allDependencies.each {
                         if( != null && ( != null || "unspecified".equals( && it.version != null) {
                             def dependencyNode = dependenciesNode.appendNode('dependency')
                             dependencyNode.appendNode('version', it.version)

    Don’t forget to update the groupId, artifactId, and version.

    The library is now set up.

  5. Library example code: Next we need to set up the library example code. Go to the build.gradle of the original module that was created and at the end of the dependencies block there, add compile project(path: ':library'), where ':library' is the name of your library module. Then Sync your Gradle file.


     dependencies {
         // ...
         compile project(path: ':thelib')

    For me, this resulted in Error: A library uses the same package as this project: name.maxaller.easynsd because the AndroidManifest.xml file in my example app module had the same package attribute as the library. So I just changed it to name.maxaller.easynsd.example.

    Go ahead and experiment a bit. Your library should be compiling, and your example code should be able to use it. gradlew clean build should succeed.

    That’s it for library – it’s done. Let’s update your real (non-library non-example) application.

  6. Update application repositories: In your main application’s project’s build.gradle, add mavenLocal() to your allprojects > repositories. It should look something like this:

     allprojects {
         repositories {
  7. Update application dependencies: Next, in your application module’s build.gradle, where your dependencies are, add your module to the dependencies:

     dependencies {
         // ...
         compile 'name.maxaller:easynsd:0.1'

    Almost done! Just a couple more easy steps.

  8. Publish library locally: Switch back to your library project, and then run the publishToMavenLocal Gradle task.
  9. Sync latest changes: Finally, in your main application, hit the Sync Project with Gradle Files button on the menu bar or under Tools > Android.

Whenever you need to make changes to your library, repeat these last two steps.

That’s it. Hope this was helpful.


  • Gradle: 2.14.1
  • Android Build Logic: 25.0.0