Using the Sync API with Android Studio

// By Steve Marx • Sep 05, 2013

Update: The Sync and Datastore SDK has been deprecated. Learn more here.

EDIT [2014/12/04]: The steps here refer to an old version of Android Studio. Updated instructions can now be found as part of the standard Sync SDK setup.

Yesterday a developer asked on Stack Overflow how he could use the Dropbox Sync API with Android Studio. Although we mention that you can use other tools, we only provide instructions for using the Android Sync API with Eclipse. Adapting those instructions to Android Studio takes a little effort, particularly because the Sync API includes a native library. Note that the following instructions also apply to the Datastore API.

Add the libraries to your project

The first step is the same as when using Eclipse. After downloading the SDK, you need to copy the contents of the libs folder into the libs folder of your project. When you're done, your project should look something like this:
(Note that android-support-v4.jar was already there from when I created my app and is not part of the Android Sync API.)

Package the native libraries

This second step is different from what you do with Eclipse. Android Studio uses a tool called Gradle to manage the build process. There's a file called build.gradle in your project that controls things like what libraries are included. Handling native libraries with Gradle is a little tricky. If you search for it, you'll find a few methods of including native libraries using Gradle. I settled on the following, which you should add to the end of your build.gradle file:

task nativeLibsToJar(type: Zip) {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    extension 'jar'
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
}
 
tasks.withType(Compile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

This zips all the .so files (e.g. armeabi/libDropboxSync.so) into a JAR file under the path lib. That JAR file is placed in the build directory at the path native-libs/native-libs.jar.

Note that it's important to use double quotes around $buildDir/native-libs! In Groovy, the language used by Gradle, double quotes allow for string interpolation. Without them, $buildDir wouldn't be interpreted as a variable and you would instead end up with a directory called $buildDir.

Include the Dropbox libraries

Once the native libraries have been packaged up, all you need to do is tell Gradle that you want to actually use those libraries. Inside the dependencies section of build.gradle, add the following two dependencies:

compile files('libs/dropbox-sync-sdk-android.jar')
compile files("$buildDir/native-libs/native-libs.jar")

Enjoy

After rebuilding your project (twice, if necessary, to pick up the new JAR file), you should be able to follow along with the rest of the installation instructions for either the Sync API or the Datastore API.

If you run into any trouble, please let us know on the developer forum.


// Copy link