LiveKit LogoDocs

Quickstart Guides / Clients / Android

Android Quickstart

On this page

1. Install LiveKit SDK2. Permissions3. Connect to a Room using a VideoView4. Create a backend server to generate tokens

1. Install LiveKit SDK

LiveKit for Android is available as a Maven package.

...
dependencies {
implementation "io.livekit:livekit-android:1.1.10"
}

You'll also need jitpack as one of your repositories.

subprojects {
repositories {
google()
mavenCentral()
// ...
maven { url 'https://jitpack.io' }
}
}

2. Permissions

LiveKit relies on the RECORD_AUDIO and CAMERA permissions to use the microphone and camera. These permission must be requested at runtime, like so:

private fun requestPermissions() {
val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { grants ->
for (grant in grants.entries) {
if (!grant.value) {
Toast.makeText(
this,
"Missing permission: ${grant.key}",
Toast.LENGTH_SHORT
)
.show()
}
}
}
val neededPermissions = listOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA)
.filter {
ContextCompat.checkSelfPermission(
this,
it
) == PackageManager.PERMISSION_DENIED
}
.toTypedArray()
if (neededPermissions.isNotEmpty()) {
requestPermissionLauncher.launch(neededPermissions)
}
}

3. Connect to a Room using a VideoView

LiveKit uses SurfaceViewRenderer to render video tracks. A TextureView implementation is also provided through TextureViewRenderer. Subscribed audio tracks are automatically played.

Note that this example hardcodes a token we generated for you that expires in 15 minutes. In a real app, you’ll need your server to generate a token for you.

// !! Note !!
// This sample hardcodes a token which expires in 15 minutes.
const val wsURL = "<wsURL>"
const val token = "<token>"
// In production you should generate tokens on your server, and your client
// should request a token from your server.
class MainActivity : AppCompatActivity() {
lateinit var room: Room
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Create Room object.
room = LiveKit.create(applicationContext)
// Setup the video renderer
room.initVideoRenderer(findViewById<SurfaceViewRenderer>(R.id.renderer))
connectToRoom()
}
private fun connectToRoom() {
lifecycleScope.launch {
// Setup event handling.
launch {
room.events.collect { event ->
when (event) {
is RoomEvent.TrackSubscribed -> onTrackSubscribed(event)
else -> {}
}
}
}
// Connect to server.
room.connect(
wsURL,
token,
)
// Publish audio/video to the room
val localParticipant = room.localParticipant
localParticipant.setMicrophoneEnabled(true)
localParticipant.setCameraEnabled(true)
}
}
private fun onTrackSubscribed(event: RoomEvent.TrackSubscribed) {
val track = event.track
if (track is VideoTrack) {
attachVideo(track)
}
}
private fun attachVideo(videoTrack: VideoTrack) {
videoTrack.addRenderer(findViewById<SurfaceViewRenderer>(R.id.renderer))
findViewById<View>(R.id.progress).visibility = View.GONE
}
}

(For more details, you can reference the complete sample app.)

4. Create a backend server to generate tokens

Use our Server SDKs to create a backend server than generates tokens for your app at runtime:

Previous

Chevron IconiOS/macOS
LiveKit logo

Product

SFU

SDKs

Performance

Deployment