Using the Dropbox SDK with Android Studio and Gradle

Dropbox DeveloperDropbox provides a set of interesting online services. Last year, they released a nice feature, the Datastore API. The documentation is good, but meant for using with Eclipse. If like me, you are using Android Studio, you will have to look for some more steps to make this working.

I gathered all of them into this single post.

First, you have to download the SDK. It is a zip file. The archive contains different files. The content of the libs folder should be copied into the libs folder of your app. The Dropbox SDK uses a native library, those are contained into the subfolders, so you have to copy the subfolders as well.

Following the Dropbox documentation

Now you have to configure Gradle, and this is the tricky part which unfortunately is not documented in the main documentation. The useful documentation is in a post on their blog. So, you have to add the following instructions at the end of your 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)
}

But there is a little mistake there as Compile is deprecated. Since Gradle 2.0, it does not work anymore as you’ll get the following error:

Error:(69) A problem occurred evaluating project ':app'.
> Could not find property 'Compile' on project ':app'.

So you should better use JavaCompile and set the following:

tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

Then, in the dependencies section, just add the libraries

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

A more clever setting

Since the version 0.7.2 of the Gradle plugin, you do not need custom task to include native library. Just create a jniLibs folder in the app/src/main path. Copy all the native libs there. You should have the following:

app/src/main/jniLibs/armeabi/libDropboxSync.so
app/src/main/jniLibs/armeabi-v7a/libDropboxSync.so
app/src/main/jniLibs/mips/libDropboxSync.so
app/src/main/jniLibs/x86/libDropboxSync.so

Those libraries will be included automatically. In the dependencies section, you will need to add:

dependencies {
compile files('libs/dropbox-sync-sdk-android.jar')
}

This is better setting and works fine.

Configuring your project

From there, you just have to go back to the SDK documentation. In short, add in the Manifest the Internet permissions if you haven’t declared them already.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Then, add the activities and service. You have to replace the APP-KEY with your app key, but check twice that the android:scheme is expecting a value with the pattern db-APP_KEY.

<activity android:name="com.dropbox.sync.android.DbxAuthActivity" />
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:launchMode="singleTask" >
<intent-filter>
<data android:scheme="db-APP_KEY" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name="com.dropbox.sync.android.DbxSyncService"
android:enabled="true"
android:exported="false"
android:label="Dropbox Sync" />

Now, you are good to go.

About Darko Stankovski

Darko Stankovski is the founder and editor of Dad 3.0. You can find more about him trough the following links.

Darko Stankovski

Darko Stankovski is the founder and editor of Dad 3.0. You can find more about him trough the following links.

You may also like...

Leave a Reply