Add permissions

Declare permissions in Android Manifest

Add the required permissions to the AndroidManifest.xml file (typically app/src/main/AndroidManifest.xml):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

Request permissions at runtime

To request permissions, add the following code to the onCreate method of the MainActivity class:

override fun onCreate(savedInstanceState: Bundle?) {

    // request permissions
    val callsPermissions = arrayOf(Manifest.permission.RECORD_AUDIO)
    ActivityCompat.requestPermissions(this, callsPermissions, 123)

After pasting the above snippet some of the class references (imports) can be missing. The missing class is marked using red color. You have to add the missing imports to fix this error. Rollover on the red text, wait for the window to appear, and press Import (this action will be required in the following steps as well).

NOTE You can also add missing import by placing caret at red text and pressing Option + Return on macOS or Alt+Enter on Windows.

Run the app

You now launch the app. Use the physical phone (with USB Debugging enabled) or create a new Android Virtual Device. When the virtual device is available press the Launch button:

Launch app
Launch app

Notice the prompt asking for permission to use the microphone:

Permissions dialog
Permissions dialog