---
source_url: https://www.pubnub.com/docs/chat/community-supported/android
title: Get started with PubNub Chat Components for Android
updated_at: 2026-05-19T12:09:52.785Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Get started with PubNub Chat Components for Android

:::warning Unsupported library
PubNub no longer supports this software library, but you are [welcome to contribute](https://github.com/pubnub/chat-components-android).
:::

See how you can get a 1:1 chat app quickly up and running.

You will download a sample Android app that uses two PubNub Chat Components for Android: [MessageInput](https://www.pubnub.com/docs/chat/community-supported/android/ui-components#messageinput) and [MessageList](https://www.pubnub.com/docs/chat/community-supported/android/ui-components#messagelist). Then, you'll run the app and send your first message by typing it in the message input field. The messages will pile up on the screen as you send them.

## Prerequisites

* [Android Studio](https://developer.android.com/studio) (>= Dolphin 2021.3.1)
* [git](https://www.atlassian.com/git/tutorials/install-git)

:::note Tools used
This guide uses [PubNub Kotlin SDK](https://github.com/pubnub/kotlin) (>= 7.3.2) for chat components and [Jetpack Compose](https://developer.android.com/jetpack/compose) as the UI Toolkit.
:::

## Steps

Follow the steps to get your PubNub keys, clone the sample Android project files with chat components, and run the app to send your first message.

### Create a PubNub account

Before you start, you need to obtain [Publish and Subscribe Keys](https://www.pubnub.com/docs/general/basics/initialize-pubnub) for your chat app. You need them to initialize the PubNub object in your app to send and receive messages through the PubNub Network. To get both keys, sign in or create an [account](https://www.pubnub.com/docs/general/setup/account-setup) on the [Admin Portal](https://admin.pubnub.com/). The autogenerated Demo Keyset in My First App already contains the configuration required to complete this guide.

### Clone a sample chat app with components

Clone a sample app from the [chat-components-android-examples](https://github.com/pubnub/chat-components-android-examples) repository.

```ssh
git clone git@github.com:pubnub/chat-components-android-examples.git
```

### Configure PubNub keys

1. Once you've cloned the repository, open it in Android Studio and wait until it loads.
2. In the gradle.properties file, replace both PUBNUB_PUBLISH_KEY and PUBNUB_SUBSCRIBE_KEY with your Publish and Subscribe Keys from your PubNub account in the Admin Portal: 1PUBNUB_PUBLISH_KEY="myPublishKey"2PUBNUB_SUBSCRIBE_KEY="mySubscribeKey"
3. Click Sync Now or the Sync Project with Gradle Files icon in the top notification bar to update your Android project.

![Sync Project with Gradle Files](https://www.pubnub.com/assets/images/gs-android-sync-gradle-b09e6cd8acf6bbe26d4e488449aed689.png)

The rest of the configuration is already provided:

* Dependencies to the PubNub Chat Components for Android and the Kotlin SDK are defined in the Dependencies.kt file: 1object PubNub {2 private const val version = "X.X.X"3 const val bom = "com.pubnub:pubnub-kotlin-bom:$version"4 const val kotlin = "com.pubnub:pubnub-kotlin"5 const val memberships = "com.pubnub:pubnub-memberships"6 7 object Components {8 const val chat = "com.pubnub.components:chat-android:X.X.X"9 }10}
* The Settings file stores the base application settings: 1object Settings {2 const val channelId: ChannelId = "Default"3 const val userId: UserId = "myFirstUser"4 val members = arrayOf("myFirstUser", "mySecondUser")5} userIdFor simplicity, the getting started app creates both the myFirstUser and mySecondUser members. To switch between these two users, change the userId value.
* In the ChatActivity file, the app initializes the PubNub instance in the onCreate() method: 1override fun onCreate(savedInstanceState: Bundle?) {2 ...3 initializePubNub()4 ...5} Similarly, the app will destroy the PubNub instance and clean up all related resources once the application is terminated: 1override fun onDestroy() {2 destroyPubNub()3 super.onDestroy()4}
* To associate a sender/current user with the PubNub messages, it's required to configure the userId parameter (also known as userId) to define the default value for the chat user. The app already sets userId in the ChatActivity file, under the initializePubNub() method. Both publishKey and subscribeKey are copied from the gradle.properties file to the BuildConfig file when the project is built. 1private fun initializePubNub(){2 pubNub = PubNub(3 PNConfiguration(UserId(Settings.userId)).apply {4 publishKey = BuildConfig.PUBLISH_KEY5 subscribeKey = BuildConfig.SUBSCRIBE_KEY6 ...7 }8 )9}

:::tip userId
For simplicity, the getting started app sets a static `userId`. However, if you implement chat in your app, you should generate a `userId` per user, device, and server and reuse it for their lifetime. Using separate User IDs in real-life apps is particularly important as it impacts your overall [billing](https://www.pubnub.com/docs/general/setup/users-and-devices#billing) and the way your app [works](https://www.pubnub.com/docs/general/setup/users-and-devices#presence).
:::

* The app calls ChatProvider which initializes all the data components. These components are responsible for providing data to UI, setting the default app theme, and communicating with the PubNub service. ChatProvider is initialized by modifying the application theme functionality, in the Theme.kt file. This file is used to facilitate the majority of the functionality provided by PubNub Chat Components for Android. 1@Composable2fun AppTheme(3 pubNub: PubNub,4 database: DefaultDatabase = Database.initialize(LocalContext.current),5 darkTheme: Boolean = isSystemInDarkTheme(),6 content: @Composable() () -> Unit,7) {8 val colors = if (darkTheme) DarkColorPalette9 else LightColorPalette10 11 MaterialTheme(12 colors = colors,13 typography = Typography,14 shapes = Shapes,15 ) {16 17 ChatProvider(pubNub, database.asPubNub()) {18 content()19 }20 }21}
* The setContent() method in the ChatActivity file calls the AppTheme() composable function responsible for setting the visual content of chat components' content on the screen: 1 setContent {2 AppTheme(pubNub = pubNub, database = ChatApplication.database) {3 Box(modifier = Modifier.fillMaxSize()) {4 Chat.View(Settings.channelId)5 }6 }7 }
* In the ChatApplication file, the app initializes the default Android Room persistence library to save data in a local database. This database is used to fill the data for all the chat components. 1class ChatApplication : Application() {2 3 companion object {4 lateinit var database: DefaultDatabase5 }6 7 override fun onCreate() {8 super.onCreate()9 database = Database.initialize(applicationContext) { it.prepopulate() }10 }
* The Room database needs data to display in the sample app. This data is provided in the RoomDatabase method in the ChatApplication file. This method is called only once, when the database is created. 1@OptIn(DelicateCoroutinesApi::class)2private fun prepopulateData(){3 4 GlobalScope.launch(Dispatchers.IO) {5 with(database) {6 val channelArray = arrayOf(Settings.channelId)7 8 // Creates user objects with userId9 val members = Settings.members.map { userId ->10 DBMember(11 id = userId,12 name = userId,13 profileUrl = "https://picsum.photos/seed/${userId}/200"14 )15 }.toTypedArray()16 17 // Creates a membership so that the user could subscribe to channels18 val memberships: Array<DBMembership> =19 channelArray.flatMap { channel ->20 members.map { member ->21 DBMembership(22 channelId = channel,23 memberId = member.id24 )25 }26 }.toTypedArray()27 28 // Fills the database with member and memberships data29 val channels: Array<DBChannel> =30 channelArray.map { id -> DBChannel(id, "Channel #$id") }31 .toTypedArray()32 33 34 memberDao().insertOrUpdate(*members)35 membershipDao().insertOrUpdate(*memberships)36 channelDao().insertOrUpdate(*channels)37 }38 }39}
* In the same Chat.kt file, the app invokes chat components by passing a channel-related ChannelId parameter to the composable function. 1@Composable2fun View(3 channelId: ChannelId,4) {5 // region Content data6 val messageViewModel: MessageViewModel = MessageViewModel.defaultWithMediator()7 val messages = remember(channelId) { messageViewModel.getAll(channelId) }8 // endregion9 10 CompositionLocalProvider(11 LocalChannel provides channelId12 ) {13 Content(14 messages = messages,15 )16 }17}
* The Content function in the Chat.kt file draws the MessageList and MessageInput components in a column. 1@Composable2internal fun Content(3 messages: Flow<PagingData<MessageUi>>,4 presence: Presence? = null,5 onMessageSelected: (MessageUi.Data) -> Unit = {},6) {7 val localFocusManager = LocalFocusManager.current8 Column(9 modifier = Modifier10 .fillMaxSize()11 .background(MaterialTheme.colors.background)12 .pointerInput(Unit) {13 detectTapGestures(onTap = {14 localFocusManager.clearFocus()15 })16 }17 ) {18 MessageList(19 messages = messages,20 presence = presence,21 onMessageSelected = onMessageSelected,22 modifier = Modifier23 .fillMaxSize()24 .weight(1f, true),25 )26 27 MessageInput(28 typingIndicatorEnabled = true29 )30 }31}
* To make sure the app works upon running, it needs internet permissions to connect to the PubNub Network. This is specified in the AndroidManifest.xml file.

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

:::tip OkHttp version
The Kotlin SDK that PubNub Chat Components for Android are based on uses OkHttp client in version `4.9.3` to handle all HTTP requests. If your chat app uses a different version of the OkHttp client, make sure to override it in the `NetworkImage.kt` file as follows: `val url = data.toHttpUrl()`.
:::

### Run the chat app

[Choose an emulator](https://developer.android.com/studio/run/emulator?hl=en#runningapp) and run the `getting-started` app.

### Send your first message

When your application opens up, type and send your first message. The messages will pile up on the screen as you send them.

![Getting Started app for Android](https://www.pubnub.com/assets/images/getting-started-android-app-1a9d17ed0d64f925270c96e5b786357f.png)

To verify that the app is connected to the PubNub Network, mock a real-live chat conversation with `myFirstUser`. Open the app in a new emulator, send a message from one emulator and reply to it from another.