For a while I have been working on a library for Android projects. Nothing fancy just some common code I have used in a couple of games. Until today I was doing it in Eclipse and making a jar file. Android Studio has been out for a while but I hadn’t bothered switching over. Today Eclipse broke so it was time to swap. Honestly it was a pain to figure all this out. Hopefully by posting this it isn’t so much of a pain for you. This guide will show you how to make a project that will contain your library as well as make a second project that uses that library. Neat huh.

Here is a list of the versions of the things I had when doing this. I can’t promise it will work for any other version but I will try keep this up to date.

Android Studio 0.8.9

Maven 3.2.2

Computer, Macbook Air 2013

Step 1: Find your Maven Repository Path

This one is pretty easy but you need it later. run the following:

mvn -X

you will get a whole bunch of output the line you are looking for looks like this:

[DEBUG] Using local repository at /Users/elliot/.m2/repository

You should copy the location of this repository or at least remmeber how to find it because we are going to use it later.

Step 2: Make a new project in android studio.

Open Android Studio, make a new project. Call it whatever you like, I made mine a phone project (not wear or glass etc) but I’m not sure it matters. Don’t add and Activity and then press finish.

Once you project has been created we will Add/Edit some files.

Add a java class and call it anything. Yep anything. Make a function that does something, or ntohing I dont mind. We are making this file just to eb sure it imported correctly. Normally this class and any others you write would become part of your library.

Open your AndroidManifest.xml file and remove the application element.

Open the build.gradle file within the app directory. Not the global one. This file should have more than one line in it.

Change the line at the top that reads

apply pligin com.android.application

to

apply plugin ‘com.android.library’

below that add

apply plugin ‘maven’

Now down the bottom of the same file add the following code:

1 2 3 4 5 6 7 8 9 10 uploadArchives { repositories { mavenDeployer { repository(url: "file:////Users/elliot/.m2/repository") //Taken from mvn -X pom.groupId = 'net.smitec.lib' pom.artifactId = 'test-lib' pom.version = '0.0.1' } } }

Change the pom. lines to fit your library name, version etc. Change the repository(...) line to use the maven local repository location we found in step 1.

Now run the gradle task uploadArchives . You can do this by opening the gadle tab on the right of Android Studio.

Check the output in the terminal. If there is no complaining you’re done. Congratulations

Step 3: Using the library

Make or open a new project in Andrdoi Studio.

Open the build.gradle file for the application (again not the global one).

Add the following within the android{...} section:

1 2 3 repositories { mavenLocal() }

and now finally in dependencies add:

compile ‘net.smitec.lib:test-lib:0.0.1’

Changing here all parts to match those set in your library.

If you now run a gradle sync your library should appear in the External Libraries section in your project tree. You should now be able to use this code in your project.

So hopefully that sorted things for you. If not drop me an email/tweet and I’ll check it out. Also I’ve uploaded two projects, one a library and one a tester so you can see everything for youself. Those you can find at https://github.com/smitec/android-studio-library-example if you need.