## Getting started with Sinch In‑app Calling for Android SDK This guide shows you how to get started integrating your Android application with the In‑app Calling SDK. Because this is a getting started guide, we only cover the basic steps to sign in to the In‑app Calling SDK and to make and receive audio calls. Notes This guide is meant to be used alongside our samples available in the SDK. Code snippets shown in the guide are part of the `sinch-rtc-sample-push` sample available on our [download page](https://developers.sinch.com/docs/in-app-calling/sdk-downloads/) inside the SDK archive. Please follow along with the samples open in your IDE. For more complex examples and documentation, check inside the SDK package file or look at our [tutorials](https://developers.sinch.com/docs/in-app-calling/). ## Prerequisites - Android Studio and Android SDK tools available at the [Android Studio website](https://developer.android.com/studio). - In‑app Calling [SDK for Android](https://developers.sinch.com/docs/in-app-calling/sdk-downloads/). - For convenience, two physical Android devices (however, one physical device and one emulator is enough for testing purposes). ## Create an Android Studio project Create a new Android Studio project using the "Empty Activity" template. ![as1](/assets/as1.7e79306efe2e767e0be3200c136d0e642c0f19363aee396610d1448f5e2aa849.4503b5b1.png) Notes - You can use Kotlin as your development language in both Java and Kotlin versions of the SDK. - The name of your application does not need to correspond to the one provided in the Sinch Dashboard. ## Add the Sinch Voice and Video SDK to your Android application 1. In your web browser, go to the [Sinch SDKs download page](https://developers.sinch.com/docs/in-app-calling/sdk-downloads/). 2. Find the Android SDK for Java, download the ZIP file, and extract it. 3. Along with the library itself, the package also contains documentation and sample applications. For the purpose of this tutorial, only the AAR file is needed. Locate it inside the `libs` folder and copy it. 4. Paste it into the `/app/libs` directory. ![as2](/assets/as2.8b6ad95b988bd84dfb2729944c71915462339dff8a9de8792f20ca72e9aefc0d.4503b5b1.png) 5. Edit your app module `build.gradle` file located under `app/build.gradle` to include the following: `app/build.gradle` ```groovy repositories { flatDir { dirs 'libs' } } dependencies { implementation(name:'sinch-android-rtc', version:'+', ext:'aar') // other dependencies } ``` You should see a message that Gradle files have changed. Click **Sync Now** and wait for Android Studio to adopt your changes. Note Alternatively, the SDK is available on Maven Central. See [Minimum requirements](/docs/in-app-calling/sdk-downloads#android-sdk-on-maven-central) for more information. ## Interacting with the Voice and Video SDK The `SinchClient` object is the main SDK entry point. Once created, it provides various SDK features such as video, audio, or PSTN calls. We recommend initiating `SinchClient` once and retaining its instance for the entire lifetime of the running application. Instead of placing it inside Activity or Fragment classes, put it inside a [Service](https://developer.android.com/guide/components/services) component. 1. Create a new Kotlin class that extends the Service component (don't forget to define the service in your `AndroidManifest.xml` file). 2. Add a `SinchClient` instance as a member variable. 3. Create a function that builds the client. The result of these steps is demonstrated below: ```kotlin class SinchService : Service() { companion object { private const val APP_KEY = "enter-application-key" private const val APP_SECRET = "enter-application-secret" private const val ENVIRONMENT = "ocra.api.sinch.com" } private var sinchClient: SinchClient? = null private fun createClient(username: String) { sinchClient = SinchClient.builder().context(applicationContext) .userId(username) .applicationKey(APP_KEY) .environmentHost(ENVIRONMENT) .pushConfiguration( PushConfiguration.fcmPushConfigurationBuilder() .senderID(APP_FCM_SENDER_ID) .registrationToken(getFcmRegistrationToken(this).orEmpty()).build() ) .pushNotificationDisplayName("User $username") .build() sinchClient?.addSinchClientListener(MySinchClientListener()) sinchClient?.callController?.addCallControllerListener(SinchCallControllerListener()) } // ... } ``` To make this example work for you, you need to update some values: | Parameter | Your value | | --- | --- | | `APP_KEY` | You can find your key on your [Build dashboard](https://dashboard.sinch.com/voice/apps). | | `APP_SECRET` | You can find your secret on your [Build dashboard](https://dashboard.sinch.com/voice/apps). | | `pushConfiguration` | FCM or HMS push configuration needed by the SDK to send push messages notifying about incoming calls. See the [Push notifications](https://developers.sinch.com/docs/in-app-calling/android/push-notifications/) section for more information. | There are a few other elements to notice: - The `environmentHost` parameter is the endpoint of the REST API the SDK targets. - The `userId` parameter is the user identifier to register within your application (the specific value will be provided after clicking the **Login** button in the Android application). When starting the Sinch client, the `SinchClientListener.onCredentialsRequired` method executes. Inside this callback you must provide a JWT token signed with your application secret. In a production application the token should be generated on your backend infrastructure. Most importantly, do not put your app secret value into your Android application source code. More information can be found in the [Authentication & Authorization](https://developers.sinch.com/docs/in-app-calling/android/application-authentication/) and [Authorizing the client](https://developers.sinch.com/docs/in-app-calling/android/sinch-client/#authorizing-the-client--user) sections. Just for this step‑by‑step guide, we will mimic a backend authentication server behavior with a helper `JWT` class that creates the token locally based on `userId` and your application credentials, and then passes it back to the Sinch SDK: ```kotlin override fun onCredentialsRequired(clientRegistration: ClientRegistration) { clientRegistration.register(create(APP_KEY, APP_SECRET, settings.username)) } ``` An implementation of the `JWT` class can be found in the samples provided with the SDK package. For example: `samples/sinch-rtc-sample-push/src/com/sinch/android/rtc/sample/push/JWT.kt` ## Communication between views and the service To communicate between your application view layer (activities and fragments) and the Sinch client that lives inside the Service instance, implement a bound service pattern. Information about bound services can be found in the official Android documentation: [Bound services](https://developer.android.com/guide/components/bound-services). 1. Inside `SinchService`, create an inner class that extends `Binder`. 2. Add basic functionality that allows the application to start the Sinch client for a given username and provide a way to be notified about the initialization result: ```kotlin private var listener: StartFailedListener? = null interface StartFailedListener { fun onFailed(error: SinchError) fun onStarted() } inner class SinchServiceInterface : Binder() { fun startClient() { // The username is fetched from settings. start() } fun setStartListener(listener: StartFailedListener?) { this@SinchService.listener = listener } } ``` 3. Override the `onBind` method to return `SinchServiceInterface`: ```kotlin private val sinchServiceInterface: SinchServiceInterface = SinchServiceInterface() override fun onBind(intent: Intent): IBinder { // ... return sinchServiceInterface } ``` ## Log in to the application When the user starts the application, they typically enter a username that will be passed as `userId` and used to create a Sinch client. The username they choose will be used as a callee identifier for making the audio call. 1. Rename the `MainActivity` and `activity_main.xml` files in Android Studio (created when you set up the project) to `LoginActivity` and `login.xml`. Right‑click the filename and choose `Refactor -> Rename`. 2. Create a simple layout containing an `EditText` (for entering the username) and a login button. ```xml ``` 3. Inside the `LoginActivity` file, first bind to the Sinch client service by calling: ```kotlin private fun bindService() { val serviceIntent = Intent(this, SinchService::class.java) applicationContext.bindService(serviceIntent, this, BIND_AUTO_CREATE) } ``` Note that `LoginActivity` is passed as the second argument, meaning it must implement the [ServiceConnection](https://developer.android.com/reference/android/content/ServiceConnection) interface. 4. Inside `onServiceConnected` in `BaseActivity`, assign the provided binder to a local variable for later communication with the service, and assign `LoginActivity` as a listener to get notifications about success or failure when starting the Sinch client. `BaseActivity` ```kotlin protected var sinchServiceInterface: SinchService.SinchServiceInterface? = null private set override fun onServiceConnected(componentName: ComponentName, iBinder: IBinder) { if (SinchService::class.java.name == componentName.className) { sinchServiceInterface = iBinder as SinchService.SinchServiceInterface onServiceConnected() } } protected open fun onServiceConnected() { // for subclasses } ``` `LoginActivity` ```kotlin override fun onServiceConnected() { if (sinchServiceInterface?.isStarted == true) { openPlaceCallActivity() } else { sinchServiceInterface?.setStartListener(this) } } ``` 5. Finally, assign the login button a click listener that initiates and starts the Sinch client: ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... loginButton.apply { setOnClickListener { loginClicked() } } } private fun loginClicked() { val username = loginNameEditText.text.toString() sinchServiceInterface?.username = username startClientAndOpenPlaceCallActivity() } ``` 6. Return to the `SinchService` implementation. Inside `onClientStarted` and `onClientFailed` callbacks of `SinchClientListener`, pass the result to the `sinchClientInitializationListener`. ```kotlin override fun onClientFailed(client: SinchClient, error: SinchError) { listener?.onFailed(error) // ... } override fun onClientStarted(client: SinchClient) { listener?.onStarted() } ``` 7. Before launching the application, declare two permissions inside your `AndroidManifest.xml` file that are required to start the `SinchClient`: ```xml ``` 8. Run the application, enter a username of your choice, and verify your Logcat output. You should see that the client started successfully. ![as3](/assets/as3.ff34585691851fef1d79925c2782a5f40c70ae14cd1e309549a1a74667b005d1.4503b5b1.png) ## Next steps Now that your application is created, you can configure it to make a call. Make a call