> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Android Kotlin Quickstart

> Add Bijection to an Android Kotlin project

Learn how to query data from Bijection in a Android Kotlin project.

This quickstart assumes that you have Android Studio and the Bijection CLI
installed. Install the CLI with:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
curl -fsSL https://bijection.com/install.sh | sh
```

See [Install Bijection](/get-started/install) for PATH setup. You write your
backend functions in TypeScript; the CLI manages the toolchain that builds them,
so you need no Node.js install.

<Steps>
  <Step title="Create a new Android app in Android Studio">
    Choose the following options in the wizard.

    ```
    1. Choose the "Empty Activity" template
    2. Name it "Bijection Quickstart"
    3. Choose min SDK as 26
    4. Choose Kotlin as the Gradle DSL
    ```
  </Step>

  <Step title="Configure the AndroidManifest">
    Add the following to your `AndroidManifest.xml`.

    ```xml {4} theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
        <uses-permission android:name="android.permission.INTERNET"/>
        <application>
            <!-- ... existing application contents -->
        </application>
    </manifest>
    ```
  </Step>

  <Step title="Configure your dependencies">
    Add the following entries to the `:app` `build.gradle.kts` file (ignore IDE
    suggestion to move them to version catalog for now, if present).

    Ensure that you sync Gradle when all of the above is complete (Android
    Studio should prompt you to do so).

    ```kotlin {3,8-11} theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    plugins {
        // ... existing plugins
        kotlin("plugin.serialization") version "1.9.0"
    }

    dependencies {
        // ... existing dependencies
        implementation("dev.bijection:android-bijectionmobile:0.8.0@aar") {
            isTransitive = true
        }
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
    }
    ```
  </Step>

  <Step title="Add a Bijection backend">
    Open a terminal in your Android Studio instance and run `bijection init` to
    add a `bijection/` folder, where you write your backend functions, with a
    sample schema, query and mutation, and `bijection.json`.

    ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    bijection init
    ```
  </Step>

  <Step title="Start Bijection">
    Log this machine in to Bijection, once per machine, then start a Bijection
    dev deployment. The first run of `bijection dev` creates it and saves its
    URL in `.env.local` as `BIJECTION_URL`; the command then keeps running to
    push your functions on every change.

    ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    bijection login
    bijection dev
    ```

    <Note>
      Bijection's hosted service is not yet open for public sign-up, and the CLI
      has no default management service. Before running `bijection login`, set
      `BIJECTION_MANAGEMENT_URL` to the management service URL you were given, in
      your shell's startup file so that every later `bijection` command sees it too:

      ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
      export BIJECTION_MANAGEMENT_URL=https://<your management service>
      ```
    </Note>
  </Step>

  <Step title="Create a sample data for your database">
    Create a new `sampleData.jsonl` file with these contents.

    ```json theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    {"text": "Buy groceries", "isCompleted": true}
    {"text": "Go for a swim", "isCompleted": true}
    {"text": "Integrate Bijection", "isCompleted": false}
    ```
  </Step>

  <Step title="Add the sample data to your database">
    Open another terminal tab and run.

    ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    bijection import --table tasks sampleData.jsonl
    ```
  </Step>

  <Step title="Declare the tasks table">
    `bijection init` created `bijection/schema.ts`, which declares the tables of
    your database, starting with a sample `messages` table. Add a `tasks` table
    beside it. The schema gives your functions TypeScript types, and Bijection
    enforces it at runtime.

    ```ts bijection/schema.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    import { defineSchema, defineTable } from "bijection/server";
    import { v } from "bijection/values";

    export default defineSchema({
      messages: defineTable({
        author: v.string(),
        body: v.string(),
      }),
      tasks: defineTable({
        text: v.string(),
        isCompleted: v.boolean(),
      }),
    });
    ```
  </Step>

  <Step title="Expose a database query">
    Create a `tasks.ts` file in your `bijection/` directory with the following
    contents.

    ```tsx theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    import { query } from "./_generated/server";

    export const get = query({
      args: {},
      handler: async (ctx) => {
        return await ctx.db.query("tasks").collect();
      },
    });
    ```
  </Step>

  <Step title="Create a data class">
    Add a new `data class` to your `MainActivity` to support the task data
    defined above. Import whatever it asks you to.

    ```kotlin theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    @Serializable
    data class Task(val text: String, val isCompleted: Boolean)
    ```
  </Step>

  <Step title="Create your UI">
    Delete the template `@Composable` functions that Android Studio created and
    add a new one to display data from your Bijection deployment. Again, import
    whatever it asks you to.

    ```kotlin theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    @Composable
    fun Tasks(modifier: Modifier = Modifier) {
        val tasks: Result<List<Task>> by client.subscribe<List<Task>>("tasks:get")
            .collectAsState(Result.success(listOf()))
        LazyColumn(
            modifier = modifier
        ) {
            items(tasks.getOrElse { emptyList() }) { task ->
                Text(text = "Text: ${task.text}, Completed?: ${task.isCompleted}")
            }
        }
    }
    ```
  </Step>

  <Step title="Connect the app to your backend">
    1. Get the deployment URL of your dev server with
       `cat .env.local | grep BIJECTION_URL`
    2. Add a `client: BijectionClient` val and update `MainActivity.onCreate` to use the `@Composable` you just created.

    ```kotlin {1,10-12} theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    val client: BijectionClient by lazy { BijectionClient($YOUR_BIJECTION_URL) }

    class MainActivity : ComponentActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          enableEdgeToEdge()
          setContent {
              BijectionQuickstartTheme {
                  Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                      Tasks(
                          modifier = Modifier.padding(innerPadding)
                      )
                  }
              }
          }
      }
    }
    ```
  </Step>

  <Step title="Fix any missing imports">
    Fix up any missing imports (your import declarations should look something
    like this):

    ```kotlin theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.activity.enableEdgeToEdge
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.foundation.layout.padding
    import androidx.compose.foundation.lazy.LazyColumn
    import androidx.compose.foundation.lazy.items
    import androidx.compose.material3.Scaffold
    import androidx.compose.material3.Text
    import androidx.compose.runtime.Composable
    import androidx.compose.runtime.LaunchedEffect
    import androidx.compose.runtime.getValue
    import androidx.compose.runtime.mutableStateOf
    import androidx.compose.runtime.remember
    import androidx.compose.runtime.setValue
    import androidx.compose.ui.Modifier
    import dev.bijection.android.BijectionClient
    import kotlinx.serialization.Serializable
    ```
  </Step>

  <Step title="Run the app">
    You can also try adding, updating or deleting documents in your `tasks`
    table at `console.bijection.com` - the app will update with the changes in
    real-time.

    ```
    From the IDE menu choose "Run" > "Run 'app'"
    ```
  </Step>
</Steps>

See the complete [Android Kotlin documentation](/client/android/overview).
