The Auth0 SDK automatically handles dependency resolution and includes secure token storage capabilities. Manifest placeholders will be configured with your Auth0 credentials in the next step.
3
Setup your Auth0 App
Next up, you need to create a new app on your Auth0 tenant and add the configuration to your Android project.First, prepare your app/src/main/res/values/strings.xml file with placeholder values:
app/src/main/res/values/strings.xml
Copy
Ask AI
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">Auth0 Android Sample</string> <string name="app_title">Auth0 Android Sample</string> <!-- Auth0 Domain and Client Id --> <string name="com_auth0_domain">YOUR_AUTH0_DOMAIN</string> <string name="com_auth0_client_id">YOUR_AUTH0_CLIENT_ID</string> <string name="com_auth0_scheme">https</string> <string name="welcome_unauthenticated">Get started by signing in to your account</string> <string name="welcome_authenticated">Welcome to Auth0 Android!</string> <string name="log_in">Log In</string> <string name="log_out">Log Out</string></resources>
Replace YOUR_AUTH0_DOMAIN with your actual Auth0 domain (e.g., dev-abc123.us.auth0.com).
Allowed Callback URLs are a critical security measure to ensure users are safely returned to your application after authentication. Without a matching URL, the login process will fail, and users will be blocked by an Auth0 error page instead of accessing your app.Allowed Logout URLs are essential for providing a seamless user experience upon signing out. Without a matching URL, users will not be redirected back to your application after logout and will instead be left on a generic Auth0 page.The URL scheme includes your package name (com.auth0.samples.android) to ensure the callback is routed to your specific app.
Important: Ensure the package name in your callback URLs matches your applicationId in build.gradle.kts. If authentication fails, verify these values are identical.
4
Create the Authentication Service
Create a centralized service to handle all authentication logic.Add a new Kotlin file:
Right-click your package → New → Kotlin Class/File → File
Name it AuthenticationManager
Replace its contents with:
AuthenticationManager.kt
Copy
Ask AI
package com.auth0.samples.androidimport android.content.Contextimport androidx.fragment.app.FragmentActivityimport com.auth0.android.Auth0import kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.StateFlowimport kotlinx.coroutines.flow.asStateFlowimport com.auth0.android.authentication.AuthenticationAPIClientimport com.auth0.android.authentication.AuthenticationExceptionimport com.auth0.android.authentication.storage.CredentialsManagerExceptionimport com.auth0.android.authentication.storage.SecureCredentialsManagerimport com.auth0.android.authentication.storage.SharedPreferencesStorageimport com.auth0.android.callback.Callbackimport com.auth0.android.provider.WebAuthProviderimport com.auth0.android.result.Credentialsimport com.auth0.android.result.UserProfileclass AuthenticationManager(private val context: Context) {private val auth0 = Auth0.getInstance(context)private val credentialsManager: SecureCredentialsManagerprivate val _isAuthenticated = MutableStateFlow(false)val isAuthenticated: StateFlow<Boolean> = _isAuthenticated.asStateFlow()init { val authentication = AuthenticationAPIClient(auth0) val storage = SharedPreferencesStorage(context) credentialsManager = SecureCredentialsManager(context, auth0, storage)}fun login() { WebAuthProvider.login(auth0) .withScheme("https") .withScope("openid profile email offline_access") .start(context as FragmentActivity, object : Callback<Credentials, AuthenticationException> { override fun onSuccess(credentials: Credentials) { //Save the credentials credentialsManager.saveCredentials(credentials) _isAuthenticated.value = true } override fun onFailure(exception: AuthenticationException) { //Handle error cases } })}fun logout() { WebAuthProvider.logout(auth0) .withScheme("https") .start(context as FragmentActivity, object : Callback<Void?, AuthenticationException> { override fun onSuccess(result: Void?) { credentialsManager.clearCredentials() _isAuthenticated.value = false } override fun onFailure(exception: AuthenticationException) { } })}}
6
Create the layout and styling
Create a modern Android layout with Material Design components.Update your activity_main.xml layout file:
Build and run your Android application.In Android Studio:
Copy
Ask AI
# Sync project with Gradle files (or use Android Studio's "Sync Now")./gradlew clean build# Build and install on connected device or emulator./gradlew installDebug# Or run directly from Android Studio# Click the "Run" button or press Shift+F10
Android will show a browser selection dialog if multiple browsers are installed. Chrome Custom Tabs provide the best user experience for Auth0 authentication.
CheckpointYou should now have a fully functional Auth0 login experience running on your Android device or emulator. The app uses Chrome Custom Tabs for secure authentication and automatically stores credentials.