This is a sample app to show you how to integrate an Android app with the Dropbox Java SDK. It demonstrates how to do the following:
- Authenticates with the Dropbox SDK
- Upload a photo to Dropbox
- Fetch the contents of a folder
- Fetch a user's profile information
- Use Kotlin Coroutines to make calls to the Dropbox API off the main thread using the Java SDK
- Hide the app key using
local.properties(as described below)
To run the app locally you'll need to create a Dropbox app key in the developer console and put this line in the local.properties file: DROPBOX_KEY=mydropboxkeygoeshere
- Acquire an app key for your project, instructions here
- Add the dependency to your build.gradle with the latest version. You can find our versions here.
implementation 'com.dropbox.core:dropbox-core-sdk:X.Y.Z'
- Add the following to your
AndroidManifest.xmland replaceYOUR_APP_KEYwith the key from the console
<activity
android:name="com.dropbox.core.android.AuthActivity"
android:configChanges="orientation|keyboard"
android:launchMode="singleTask">
<intent-filter>
<!-- Change this to be db- followed by your app key -->
<data android:scheme="db-YOUR_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>
- To initiate the Dropbox authentication flow call
startOAuth2PKCEwith the appropiate scopes your app will need. Read more about scopes here. This will launch the Dropbox app if installed or Dropbox.com in the browser.
val scopes = listOf("account_info.read", "files.content.write")
Auth.startOAuth2PKCE(this, APP_KEY, requestConfig, scopes)
- When the user has completed the authentication flow they will be taken back to your app. In
onResumeyou can callAuth.getDbxCredential()to get the resultingDbxCredential. - To make requests using the Dropbox Java SDK, create a
DbxClientV2like so. ClientIdentifiers are usually of the form "SoftwareName/SoftwareVersion".DbxClientV2will have the request objects needed to make a call to the Dropbox API.
val clientIdentifier = "DropboxSampleAndroid/1.0.0"
val requestConfig = DbxRequestConfig(clientIdentifier)
val dropboxClient = DbxClientV2(requestConfig, credential)
The Dropbox Java SDK will use the long lived refresh token found in the DbxCredential to automatically refresh your short lived access token when it makes requests.
If you want to hide your Dropbox app secret, you can follow these steps:
- Edit your
local.propertiesfile to contain the following line with your Dropbox key from the developer console
DROPBOX_KEY=mydropboxkeygoeshere
- Edit your
build.gradleto include the following:
android {
defaultConfig {
//more config
Properties localProperties = getLocalProperties()
String dropboxKey = localProperties['DROPBOX_KEY']
buildConfigField "String", "DROPBOX_KEY", "\"${dropboxKey}\""
manifestPlaceholders = [dropboxKey: dropboxKey]
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
def getLocalProperties() {
Properties props = new Properties()
if (file('../local.properties').exists()) {
props.load(new FileInputStream(file('../local.properties')))
}
return props
}
- Edit your
AndroidManifest.xmlto use the manifest placeholder defined in step 2.
<activity
android:name="com.dropbox.core.android.AuthActivity"
android:configChanges="orientation|keyboard"
android:launchMode="singleTask">
<intent-filter>
<!-- Using a Manifest Placeholder to hide our app key -->
<data android:scheme="db-${dropboxKey}" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
- Use your app key with:
val APP_KEY = BuildConfig.DROPBOX_KEY
Starting September 30, 2021 the Dropbox authentication flow will only return short lived tokens. The Java SDK has been updated to support this using DbxCredentials.
This credential consists of a short lived accessToken and a long lived refreshToken. The SDK will refresh this short lived access token by calling the server and exchanging the refresh token for an access token when the access token has expired (See refreshAccessTokenIfNeeded() in DbxRawClientV2).
This allows clients to continue to have a valid access token without further interaction with the user. This is useful for mobile clients that need to do periodic background work without having to force the user to manually re-authenticate. You can read more about this on the Dropbox Tech blog
If you have users with existing long lived access tokens, they will not be impacted. Only tokens issued after September 30th will be short lived.


