Skip to content

Chat SDK Android: Adding Chat SDK to your existing project

bensmiley edited this page Mar 24, 2017 · 4 revisions

The Chat SDK is even easier to set up on Android than it is on iOS as we don't have Cocoapods to deal with and due to less steps in the set up process. In this tutorial we will be looking at how to add the Chat SDK to your project.

The Android Chat SDK comes ready to go straight from the box. Download a clone from the repository and then run in Android Studio and you can fully test all the functionality on our test Chat SDK accounts. This tutorial will be looking at how you can add the Chat SDK to a custom project.

Before you start we recommend watching the following video which runs through the entire process.

Youtube video tutorial - Click here

The first stage of adding the Chat SDK to your project involves importing the project modules into your project.

  • Open your existing project and save the unzipped Chat SDK in a known location
  • Click file -> new -> import module
  • Click on the far right button to browse your directories and navigate to where you have saved the Chat SDK project.
  • Select the following modules to import: country_picker, facebook, firebase_plugin, generator and sdk.
  • You should be able to import them all at once, if you can't then repeat the process to import all the needed modules.

Import Modules

If you try to sync Gradle you will see that it can't due to some missing parameters. We need to add the SDK Versions to your project.

  • Go to gradle.properties in the root of your project.
  • Add the following code: (This allows the build versions to be set for the Chat SDK all at once)
MIN_SDK = 15
ANDROID_BUILD_SDK_VERSION = 23
ANDROID_BUILD_TOOLS_VERSION = 21.1.0
ANDROID_BUILD_TARGET_SDK_VERSION = 23
ANDROID_COMPILE_SDK_VERSION = 23

Note: You should make sure that the correct SDK versions and build tools are installed in Android Studio. To do this open the Preferences panel and navigate to Appearance & Behavour -> System Settings -> Android SDK or click on the SDK Manager icon in the tool bar. Android SDK versions 4.4 and onwards should be installed. Android SDK Build-Tools version 21.1.0 should be installed.

Update Gradle

Now you need to ensure that the following repositories have been added to your project's build.gradle (Project: Your-project-name):

Update this file by adding the following:

buildscript {
    repositories {
        //*** Make sure these lines are present ***
        jcenter()
        mavenCentral()
    }
    dependencies {
        //*** Make sure these lines are present ***
        classpath 'com.android.tools.build:gradle:2.2.0'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Note: It is worth checking the latest version for these. The ones displayed are the correct ones on time of writing and will depreciate when new versions are released.

Now run Gradle to get the project ready to be compiled. The project should now compile without error.

At this point the Chat SDK has been added correctly but we still can't access the Chat SDK code from our app. To access the code, you need to open build.gradle (Module: app) and add the following line to the dependencies.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'

    //*** Add this line ***
    compile project(':firebase_plugin')

    testCompile 'junit:junit:4.12'
}

Synch Gradle again and we are ready to launch the Chat SDK activity.

Launching the Chat SDK login Activity

Open up your app's main activity. It should be in App -> java -> Your app namespace -> Main Activity

Add the following imports:

import com.braunster.androidchatsdk.firebaseplugin.firebase.BChatcatNetworkAdapter;
import com.braunster.chatsdk.Utils.helper.ChatSDKUiHelper;
import com.braunster.chatsdk.network.BDefines;
import com.braunster.chatsdk.network.BNetworkManager;
import com.braunster.chatsdk.activities.ChatSDKLoginActivity;
import android.content.Intent;

Next add the Chat SDK setup code to your onCreate method:

// This is used for the app custom toast and activity transition 
ChatSDKUiHelper.initDefault(); 

// Init the network manager
BNetworkManager.init(getApplicationContext()); 

// Create a new adapter
BChatcatNetworkAdapter adapter = new BChatcatNetworkAdapter(getApplicationContext());

// Set the adapter
BNetworkManager.sharedManager().setNetworkAdapter(adapter); 

This code gets the Chat SDK ready to be launched.

To load the Chat SDK we have two different options:

  • Load immediately when the app starts
  • Load from a button click

The way we code this differs slightly:

Load Chat SDK when app loads

Intent myIntent = new Intent(this, ChatSDKLoginActivity.class);
startActivity(myIntent);

Load Chat SDK on a button click

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent myIntent = new Intent(MainActivity.this, ChatSDKLoginActivity.class);
        MainActivity.this.startActivity(myIntent);
    }
});

Adding the activity to the manifest

The final step is to make the login activity available by adding it to our app manifest. Open the file App -> manifests -> Android Manifest.xml and add the following code below any existing activities that may be setup.

<activity
    android:name="com.braunster.chatsdk.activities.ChatSDKLoginActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@style/ChatSDKTheme"
    android:windowSoftInputMode="stateHidden|adjustPan"
    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Now run the app and you will see the Chat SDK login screen open when the app launches.

Read through the next tutorial to configure your custom Firebase account with the Chat SDK you have just added to your project.