How do I add a library project to the Android Studio and use it?(Some asked dont’t take effect)

I’ll show you how to do so with the map sample.

Say you have imported the map sample in Android Studio, and created the corresponding maps module.

1. Create an android library module

You need to create an additional library module for GMS, which includes all the classes the map sample needs from Google API to compile your map module.

The library project is located at:

$ANDROID_SDK/extras/google/google_play_services/libproject/google-play-services_lib

To do so, go to File -> Import module and select the location described above or the location of your library project.

Then use the right option between Create module from existing sources or Import module from external model. In my case it’s from existing sources.

Android Studio detects the src/res directories, so far so good.

It also detects the library directories, which will save you the manual import of any jar you may have in lib/ (Like google-play-services.jar in my case).

[EDIT: It does actually save you only half the time because it creates the libraries but don’t link them in you module dependencies]

2. Update your main module dependencies – Library module

Now, the tricky part is that you have only created the module, not linked to your project as a dependency.

Do do so, go to File -> Project structure -> Modules

Select your main module, maps in my case, and go to the dependencies tab on the right hand side. Click the + and 3. Module dependency.

Select the module your main module depends on and click OK.

3. Update your main module dependencies – Jars

Now for unexplained reason the activities in my maps module still cannot find the API classes. I also have to manually add the google-play-services.jar dependency on the Module configuration (as in 2., but select Library instead of Module dependency);

This point can surely be automatized with gradle but I haven’t yet looked into it. When you use a new IDE, the fewer tools you use, the easier it is to track configuration errors. I’d suggest adding only one tool at a time 🙂

Leave a Comment