Bitly Android SDK

Getting Started

If you do not have any mobile apps setup in your Bitly account you will need to refer to the user guide for setting up deeplinks in your account.

Once you have setup your mobile app in your account, go to Your Settings -> Advanced -> Deep Link Applications. There you will find the Bitly App ID which needs to be included in your SDK configuration.

Enable App Links

To create the Digital Asset Links JSON file, which would be located at https://yourdomain.com/.well-known/assetlinks.json, you must send the sha256_cert_fingerprints associated with the app to your Bitly Account Manager to enable. You can determine the value using this command

  keytool -list -v -keystore my-release-key.keystore

Install the SDK

  1. Add the following to your build.gradle file

    compile 'com.bitly:bitlysdk:+'
    
  2. You may need to also add JCenter as a repository

    repositories {
      jcenter()
    }
    

Configure the App Links

  1. Create an Activity to handle the App Links
  2. Configure the Activity in your AndroidManifest.xml to receive the App Links

    <activity android:name=”MainActivity”>
        <intent-filter android:autoVerify="true">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http" android:host="yourdomain.com" />
            <data android:scheme="https" android:host="yourdomain.com" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="yourscheme" android:host="*" />
        </intent-filter>
    </activity>

    You cannot combine your launch activity and your App Links activity. These must be two separate activities.

  3. Multiple <intent-filter> can be created to support multiple domains

Configure the SDK for App Links

  1. If your application doesn't already have a custom Application class, create it and configure it in the AndroidManifest.xml
  2. Import the SDK in your Application instance

    import com.bitly.Bitly;
  3. Initialize the SDK on Application creation

    @Override
    public void onCreate() {
        super.onCreate();
    
    Bitly.initialize(this, "YOUR_APP_ID", Arrays.asList("yourdomain.com","yourotherdomain.com"), Arrays.asList("yourscheme"), new Bitly.Callback() {
            @Override
            public void onResponse(Response response) {
                // response provides a Response object which contains the full URL information
                // response includes a status code
                // Your custom logic goes here...
            }
    
    @Override
            public void onError(Error error) {
                // error provides any errors in retrieving information about the URL
                // Your custom logic goes here...
            }
        });
    }

    You can also provide the access token for shortening in the same initialize method

  4. Import the SDK in your Activity for handling App Links

    import com.bitly.Bitly;
  5. Handle the first intent

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
    //Handle the intent creating the activity
        Bitly.handleIntent(getIntent());
    }
  6. Handle any additional intents after activity creation

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bitly.handleIntent(intent);
    }

    If the activity is specified as having an android:launchMode that only allows for the creation of a single instance ("singleTop", etc) then handling intents in protected void onNewIntent(Intent intent) is necessary

  7. Retry on error

    Bitly.retryError(error)

    If there are any issues in handling an App Link the operation can be retried using the following line of code in your response handler from step 2

Configure the SDK for Shortening

  1. Configure an OAUTH token for your app http://bitly.com/a/oauth_apps
  2. If your application doesn't already have a custom Application class, create it and configure it in the AndroidManifest.xml
  3. Import the SDK in your Application instance

    import com.bitly.Bitly;
  4. Initialize the SDK on Application creation

    @Override
    public void onCreate() {
        super.onCreate();
    
    Bitly.initialize(this, "YOUR_APP_ACCESS_TOKEN");
    }

    You can combine this initialization with the App Link initialization

  5. You can shorten from anywhere in your application

    Bitly.shorten("http://theurlyouwishtoshorten.com", new Bitly.Callback() {
        @Override
        public void onResponse(Response response) {
            // response provides a Response object which contains the shortened Bitlink
            // response includes a status code
            // Your custom logic goes here...
        }
    
    @Override
        public void onError(Error error) {
            // error provides any errors in shortening the link
            // Your custom logic goes here...
        }
    });