Android

Integrating Eartho into Your Android App

This section describes how to integrate Eartho into an Android app using Kotlin.

1. Install the SDK

Add the Eartho Android SDK to your project's dependencies.

Add the dependency

Add the following line to the dependencies section of your build.gradle file:

// app/build.gradle
dependencies {
    // Add the Eartho Android SDK
    implementation 'io.github.eartho-group:one:1.0.7'
}

2. Retrieve Eartho Client ID, Client Secret, and Access ID

Go to Eartho Creator and copy your Eartho client ID, client secret, and access ID from the "Developers Integration" section.

3. Initialize and Configure the SDK

Integrate Eartho into your Android app by configuring the SDK and handling authentication.

Example Code:

Create and Configure the EarthoOne Instance

// MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.github.eartho.group.one.EarthoOne
import io.github.eartho.group.one.EarthoOneConfig

class MainActivity : AppCompatActivity() {

    private lateinit var earthoOne: EarthoOne

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize the EarthoOne instance
        val config = EarthoOneConfig(
            clientId = "YOUR_EARTHO_CLIENT_ID",
            clientSecret = "YOUR_EARTHO_CLIENT_SECRET"
        )
        earthoOne = EarthoOne(this, config)
    }
}

Implement Login and Logout

// MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.github.eartho.group.one.EarthoOne
import io.github.eartho.group.one.EarthoOneConfig
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var earthoOne: EarthoOne

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize the EarthoOne instance
        val config = EarthoOneConfig(
            clientId = "YOUR_EARTHO_CLIENT_ID",
            clientSecret = "YOUR_EARTHO_CLIENT_SECRET"
        )
        earthoOne = EarthoOne(this, config)

        loginButton.setOnClickListener {
            connectToEartho()
        }

        logoutButton.setOnClickListener {
            logout()
        }
    }

    private fun connectToEartho() {
        val accessId = "YOUR_EARTHO_ACCESS_ID"
        earthoOne.connectWithRedirect(accessId, onSuccess = { result ->
            val idToken = result.idToken
            val profile = earthoOne.getUser()
            // Handle the authenticated user's profile
            println("User ID: ${profile?.uid}")
            println("User Name: ${profile?.displayName}")
        }, onFailure = { error ->
            // Handle the error
            println("Error: ${error.message}")
        })
    }

    private fun logout() {
        earthoOne.logout()
    }
}

Example Layout XML

<!-- res/layout/activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <Button
        android:id="@+id/loginButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Log in" />

    <Button
        android:id="@+id/logoutButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Log out"
        android:layout_marginTop="16dp" />
</LinearLayout>

Additional Resources

For more detailed examples and usage, visit the Eartho Android Example Repository.

Last updated