Android Studio and Library Projects

This is basically a quick brain-dump post. I have previously attempted to get into Android application development, but with only 8-10 hours of “free” time per month, it was difficult to get traction with an app before the goog machine overhauled everything. This happened repeatedly. I had some great ideas for a mobile app over the weekend, and thought I’d give it another go and guess what? Everything is overhauled: Android Studio is the next big thing, ant builds are out and gradle builds are in. I decided to force myself to overcome my annoyances and try out Android Studio.

Android Studio is beautiful and operationally stable, although very buggy. I’ve found four bugs in less than two days. The biggest bug and usability issue, however, is in creating an Android Library project and adding a reference to it in another project.

Here are the steps I took to reference another project. These steps may not be accurate, but I don’t care. I figured they may help save someone some time.

  1. Create an Android library project using Android Studio.

    There’s a bug in Android Studio 0.1.3 which apparently does not mark android library projects as library projects, so navigate to MyLibProject/MyLib and change the android plugin reference to:

    apply plugin: 'android-library'
  2. Create a main application project, in my case MyApplication2
  3. Add library as git submodule or nested directory under MyApplication2
  4. Edit settings.gradle in the main project to:
    include ':MyLibProject:MyLib’, ':MyApplication2'
  5. Edit MyApplication2/build.gradle to compile the lib project in the dependencies task:
    compile project(':MyLibProject:MyLib’)
  6. Navigate to your library subdirectory and execute:
    gradle clean && gradle assemble
  7. Press CTRL+ALT+SHIFT+S to open the Project Structure dialog
  8. Create a new module, change module name and point content root to MyLibProject
  9. Change Package name to the package name of your lib and press Finish
  10. Click MyApplication2 (not MyApplication2Project) in the Project Structure dialog and select the Dependencies tab.
  11. Click the green plus icon and select Library|Java
  12. Select MyLibProject/MyLib/build/bundle/release folder, choose Project Library, and hit ok
  13. Save. The library should now be usable.

These instructions may seem a bit hurried, but it should get the job done. I’ve run through numerous attempts at different options, and these are the only ones that seem to have stuck.

I might also mention, I’ve created an ANDROID_HOME environment variable to load in my shell which points to the sdk directory under the android-studio installation. I’ve also downloaded gradle-1.6 to ~/bin, and symlinked gradle to ~/bin/gradle which adds gradle to my path.

flattr this!

3 thoughts on “Android Studio and Library Projects”

  1. In the 1. step build.gradle is the file to edit.

    In the 6. I executed ./gradlew clean && ./gradlew assemble which seems to be working.

    Now I’m stuck at:
    Gradle: A problem occurred evaluating root project ‘TabTestProject’.
    > Could not find method compile() for arguments [project ':TabsSimplifiedProject'] on root project ‘TabTestProject’.

    Any suggestions on how to solve this?

    1. Turns out I was editing the wrong build.gradle file. Under step 5 the correct file to edit is the one within the src folder. However now I’m stuck at:
      Gradle: A problem occurred configuring project ‘:TabTest’.
      > Failed to notify project evaluation listener.
      > Configuration with name ‘default’ not found.

      1. Sebastian,

        I received the same error message, but changing the value mentioned in step 1 was the resolution. I tested ran through these steps numerous times to verify they worked.

        Maybe this is an issue with the version of gradle? I have gradle installed on my system. Here’s the info:

        $ gradle –version

        Gradle 1.6

        Gradle build time: Tuesday, May 7, 2013 9:12:14 AM UTC
        Groovy: 1.8.6
        Ant: Apache Ant(TM) version 1.8.4 compiled on May 22 2012
        Ivy: 2.2.0
        JVM: 1.7.0_25 (Oracle Corporation 23.25-b01)
        OS: Linux 3.8.0-25-generic amd64

Comments are closed.