Appwrite Android SDK

repository·main·Indexed 41 days ago

https://github.com/appwrite/sdk-for-android

A type-safe Android SDK for integrating applications with Appwrite backend services, including Authentication, Databases, and Storage. Version 27.2.0 supports Kotlin and Java, featuring type-safe database models, OAuth callback configuration via CallbackActivity, and comprehensive account management including MFA, JWT generation, and anonymous sessions.

Tokens
127.2K
Snippets
345
Records
360
Agent score
97%

What's inside Appwrite

  1. Use Type Safety with Database Models

    main

    The SDK supports type-safe database operations. When calling methods like listDocuments or getDocument, pass your custom data class (Kotlin) or class (Java) to the nestedType parameter. This allows the SDK to automatically map database documents to your specific model.

    data class Book(
        val name: String,
        val author: String,
        val releaseYear: String? = null,
        val category: String? = null,
        val genre: List<String>? = null,
        val isCheckedOut: Boolean
    )
    
    val databases = Databases(client)
    
    try {
        val documents = databases.listDocuments(
            databaseId = "your-database-id",
            collectionId = "your-collection-id",
            nestedType = Book::class.java
        )
        
        for (book in documents.documents) {
            Log.d("Appwrite", "Book: ${book.name} by ${book.author}")
        }
    } catch (e: AppwriteException) {
        Log.e("Appwrite", e.message ?: "Unknown error")
    }
  2. Install the Appwrite Android SDK via Maven

    main

    Add the following dependency to your pom.xml file to include the Appwrite Android SDK.

    <dependencies>
        <dependency>
            <groupId>io.appwrite</groupId>
            <artifactId>sdk-for-android</artifactId>
            <version>27.2.0</version>
        </dependency>
    </dependencies>
  3. Install the Appwrite Android SDK via Gradle

    main

    To use the Appwrite Android SDK, add mavenCentral() to your root level build.gradle(.kts) file. Then, add the dependency to your app-level build.gradle(.kts) file.

    If you need to use SNAPSHOT releases, you must also add the Sonatype snapshot repository.

    // Root build.gradle(.kts)
    repositories {
        mavenCentral()
    }
    
    // App-level build.gradle(.kts)
    dependencies {
        implementation("io.appwrite:sdk-for-android:27.2.0")
    }
  4. Initialize the Appwrite Client and Teams service

    main

    To use the Teams service, you must first initialize the Client with your Appwrite endpoint and project ID, then pass that client instance to the Teams service constructor.

    Note: The endpoint should follow the format https://<REGION>.cloud.appwrite.io/v1.

    import io.appwrite.Client;
    import io.appwrite.services.Teams;
    
    // Initialize Client
    Client client = new Client(context)
        .setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
        .setProject("<YOUR_PROJECT_ID>");
    
    // Initialize Teams service
    Teams teams = new Teams(client);
  5. Register an Android Platform in Appwrite

    main

    Before using the SDK, you must register your Android app in the Appwrite Console:

    1. Go to your Appwrite project dashboard.
    2. Click 'Add Platform'.
    3. Select 'Android'.
    4. Enter your app's name and package name (the applicationId from your app-level build.gradle).
  6. Configure CallbackActivity for OAuth

    main

    To support Appwrite OAuth callbacks, you must register the io.appwrite.views.CallbackActivity in your AndroidManifest.xml. Replace [PROJECT_ID] with your actual Appwrite project ID.

    <manifest>
        <application>
            <activity android:name="io.appwrite.views.CallbackActivity" >
                <intent-filter android:label="android_web_auth">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data android:scheme="appwrite-callback-<PROJECT_ID>" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
  7. Initialize the Appwrite Client and Account service

    main

    To use Appwrite services in Android, first initialize the Client with your project's endpoint and project ID. You can then instantiate specific services, such as Account, by passing the Client instance to their constructor.

    Note: The Client constructor requires an Android Context.

    import io.appwrite.Client
    import io.appwrite.services.Account
    
    val client = Client(context)
        .setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
        .setProject("<YOUR_PROJECT_ID>") // Your project ID
    
    val account = Account(client)
  8. Initialize the Appwrite Client

    main

    Initialize the Client object with your Appwrite server endpoint and project ID.

    Note on Networking: If running on an emulator or physical device, do not use localhost. Use your machine's private IP address or a proxy like ngrok to ensure the device can reach the Appwrite server.

    import io.appwrite.Client
    import io.appwrite.services.Account
    
    val client = Client(context)
      .setEndpoint("https://<HOSTNAME_OR_IP>/v1") // Your API Endpoint
      .setProject("<PROJECT_ID>") // Your project ID
      .setSelfSigned(true) // Remove in production
  9. Initialize the Graphql service

    main

    Before making GraphQL requests, you must set up the Client with your Appwrite endpoint and project ID, then pass that client to the Graphql service constructor.

    Client client = new Client(context)
        .setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
        .setProject("<YOUR_PROJECT_ID>"); // Your project ID
    
    Graphql graphql = new Graphql(client);
  10. Initialize the Appwrite Android SDK

    main

    To use the Appwrite SDK, you must first initialize a Client instance with your API endpoint and project ID. This client is then passed to specific service classes (like Organization) to perform operations.

    1. Create a Client instance using your Android Context.
    2. Call .setEndpoint("https://<REGION>.cloud.appwrite.io/v1") with your Appwrite endpoint.
    3. Call .setProject("<YOUR_PROJECT_ID>") with your project ID.
    4. Instantiate the desired service (e.g., Organization) by passing the Client instance to its constructor.
    import io.appwrite.Client;
    import io.appwrite.services.Organization;
    
    // Initialize Client
    Client client = new Client(context)
        .setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
        .setProject("<YOUR_PROJECT_ID>");
    
    // Initialize Service
    Organization organization = new Organization(client);
  11. Build queries with the Query class

    main

    The Query class is a helper used to generate query strings for filtering and searching database documents. You typically use the static methods provided in the Query.Companion object to create these strings, which are then passed to Appwrite service methods (like listDocuments or getDocuments).

    Each method returns a JSON string representation of a query object. You can combine multiple queries using logical operators like and() and or().

    // Example of creating simple queries
    val query1 = Query.equal("status", "active")
    val query2 = Query.greaterThan("age", 18)
    
    // Combining queries with AND
    val combinedQuery = Query.and(listOf(query1, query2))