Thursday, July 4, 2013

Using ActionBarSherlock in Android Studio (0.1.9)

Update 05 Aug 2013 : A better way to use Action Bar on Android 2.1 and above, see Using Action Bar on Android 2.1 (API Level 7) and above using Support Library (Rev. 18) in Android Studio 0.2.3

Preparing Android Studio Packages

Make sure the following packages are installed:


Preparing the Action Bar Sherlock Library

Download the Action Bar Sherlock library from https://api.github.com/repos/JakeWharton/ActionBarSherlock/zipball/4.3.1 or go to http://actionbarsherlock.com/ to get the latest version.

Creating the Android Project

Create a project in Android Studio called YourProject. Click Next to continue until you have created the project.




Your Project Root directory should look like this:


Importing Action Bar Sherlock Library to your Project

Open the Action Bar Sherlock Library you have downloaded earlier and extract it somewhere (for example to your Desktop). After extraction, the directory should look like this:


Copy the directory called "actionbarsherlock" to your Project Root directory. After copying, your Project Root directory should look like this:


Fixing your Gradles

First, modify the settings.gradle to tell Gradle that we have to include another project on our build. settings.gradle is located on your Project Root (YourApplicationProject/settings.gradle).

Let us include the actionbarsherlock directory in your settings.gradle:


Now that we have included the actionbarsherlock directory, we need to create a build.gradle file in the actionbarsherlock directory. We can copy the build.gradle file from our Application Directory (Note that there are two build.gradle file currently in our Project Directory. One is located at the Project Root Directory YourApplicationProject/build.gradle, another one is located at the Application Directory YourApplicationProject/YourApplication/build.gradle. The later is what we need to copy to YourApplicationProject/actionbarsherlock).

After copying, the actionbarsherlock directory should look like this:


Modify the copied build.gradle from the actionbarshelock directory. For the first change, plugin is changed from android to android-library. The second change will tell Gradle that this library is needed and include it once as compared to the original code where it will always include the library file even though it might have been included already on other build.gradle. This could cause issues when compiling the project. The last code block tells Gradle where are the relevant files and directory for it to compile the library.



Lastly, we need to modify the build.gradle from our Application Directory (YourApplicationProject/YourApplication/build.gradle) to include the actionbarsherlock library project and replace the compile file line with compile to prevent the errors when including libraries more than once.


Now your Gradle are ready to compile. But before you can start coding, we need to configure Android Studio and tell about the library.

Adding Action Bar Sherlock library to Android Studio

Go to File then Project Structure. In Project Settings, click the Modules from the list and then click the "+" plus sign on the next list as seen below:



Choose Import Module and Select the actionbarsherlock directory. Click OK when finished. On the Import Module dialog box, click Next until finished. Apply and then OK on the Project Structure Dialog Box. In Android Studio 0.1.9, I have to restart the IDE for the modules to appear in the List.

After restarting, Go to File then Project Structure again, and then select YourApplication from the list. On the right side, select Dependencies tab and click the "+" sign and choose Module Dependency.



Select the actionbarsherlock and then click Apply then OK to quit the Project Structure. Restart again the Android Studio.

You can now start coding your project using the Action Bar Sherlock.

Testing the Action Bar Sherlock Library

In MainAcitivity.java, lets change the Activity to SherlockActivity. We need to remove the onCreateOptionsMenu override method as this does not exists.


Finally we need to modify the AndroidManifest.xml of our application to change the theme to the Action Bar Sherlock library themes.


Your application will now able to run the Action Bar Sherlock library for Android 2.X and above. This is an emulator running on Android 2.2:

I hope this helps you to get started with Action Bar Sherlock Library in Android Studio (0.1.9).

Updated on 2013/07/16 : Here is the source code for the whole project in a zip file.

16 comments:

  1. I m following this tutorial.

    but on step Adding Action Bar Sherlock library to Android Studio im getting the error

    Gradle Location is incorrect.

    here is link of my question on stackoverflow regrading this

    http://stackoverflow.com/questions/17623439/gradle-location-is-incorrect-error-while-importing-actionbarsherlock-in-android?noredirect=1#comment25657522_17623439

    plesase answer this. thank you

    ReplyDelete
    Replies
    1. I have replied to your question on Stack Exchange. Try to use this path:

      C:\Users\Sir John Qadir\.gradle\wrapper\dists\gradle-1.6-bin\\gradle-1.6

      Delete
  2. I have this error

    Gradle: A problem occurred evaluating project ':actionbarsherlock'.
    > No signature of method: org.gradle.api.java.archives.internal.DefaultManifest.srcFile() is applicable for argument types: (java.lang.String) values: [AndroidManifest.xml]

    :(

    ReplyDelete
    Replies
    1. I'm not sure exactly what could be the error, but updated my blog to include the source files if you would like to double check with it. Thanks!

      Delete
  3. After I follow your guide, I faced this error.

    Error: Gradle:Execution failed for task ':xx:compileDebug'.>
    Compiliation failed. see the compiler error output for details.

    and in addition, My project's manifest file is gone. Just left actionbarSherlock's manifest file.

    I don't know what I should do

    ReplyDelete
    Replies
    1. Try to download the source code I uploaded above and compile that. It should work okay. Then from there you can figure out why your project is not working.

      Delete
  4. I tried it use your source code, but It isn't work. Is it right that after import project, created 6 project folder?
    I'm so new in android studio, I didn't know well TT

    ReplyDelete
  5. Ah, and modify build.gradle / gradle.setting is necessary?

    I tried only

    'project structure -> module -> import module -> module dependency'

    ReplyDelete
    Replies
    1. This tutorial is specifically for Android Studio 0.1.9. So if you have a different version, it might not work correctly.

      The good news is, if you want to support ActionBar for API 7 and above, You don't have to use the ActionBarSherlock library anymore. Google has update the Android Support Library (rev. 18) which includes the ActionBar!

      I will publish another blog on how to implement this. But for now you can head to http://developer.android.com/guide/topics/ui/actionbar.html to read more about the support library (android.support.v7.app.ActionBar).

      Wish you good luck!

      Delete
    2. Sorry I did not answer your question directly.

      YES it is required because the current preview release of Android Studio doesn't change the settings at

      'project structure -> module -> import module -> module dependency'

      You have to manually modify the gradle files.

      Delete
    3. Thank you! Then perhaps, android version 2.3 is also works using api7?

      Anyway thanks for your advise. I'll try again!

      Delete
    4. That's correct! API 7 is for Android 2.1

      Delete
  6. Thanks very much ! that really help me to configure android studio

    ReplyDelete