byte-sized adventures.

Android Jetpack Compose Hilt Retrofit Firebase Setup

02 Oct 2023

.kts (Some Common Depenencies)

build.gradle.kts (Project)

buildscript {
    dependencies {
        classpath("com.google.gms:google-services:4.4.0")

        classpath ("com.google.dagger:hilt-android-gradle-plugin:2.48")

    }
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id("com.android.application") version "8.1.1" apply false
    id("org.jetbrains.kotlin.android") version "1.8.10" apply false

    id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false

}

build.gradle.kts (App)

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")

    id("com.google.gms.google-services")

    id("kotlin-kapt")
    id("com.google.dagger.hilt.android")

    id("com.google.devtools.ksp")
}

android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
    implementation("androidx.activity:activity-compose:1.7.2")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    implementation("com.google.firebase:firebase-auth-ktx:22.1.2")
    implementation("com.google.android.gms:play-services-auth:20.7.0")
    implementation("com.google.firebase:firebase-firestore-ktx:24.8.1")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")





    // Coroutines
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1")
    implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.1")

    // Coroutine Lifecycle Scopes
    implementation ("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2")
    implementation ("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")

    //Retrofit
    implementation("com.squareup.retrofit2:retrofit:2.9.0")
    //GSON converter
    implementation("com.squareup.retrofit2:converter-gson:2.9.0")

    //Dagger - Hilt
    val hiltVersion = "2.48"
    implementation ("com.google.dagger:hilt-android:$hiltVersion")  //May need okkhttp also
    kapt ("com.google.dagger:hilt-android-compiler:$hiltVersion")
    kapt ("androidx.hilt:hilt-compiler:1.0.0")
    implementation ("androidx.hilt:hilt-navigation-compose:1.1.0-alpha01")

    //material icons - use with caution!
    implementation ("androidx.compose.material:material-icons-extended:1.5.2")

    // Coroutine Lifecycle Scopes
    implementation ("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2")
    //lifecycle
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")

    // Coil
    implementation("io.coil-kt:coil-compose:2.4.0")

    // Retrofit
    implementation ("com.squareup.retrofit2:retrofit:2.9.0")

    // OkHttp
    implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.2")

    // JSON Converter
    implementation ("com.squareup.retrofit2:converter-gson:2.9.0")

    //Room
    val room_version = "2.5.2"
    implementation ("androidx.room:room-runtime:$room_version")
    annotationProcessor ("androidx.room:room-compiler:$room_version")

    // To use Kotlin annotation processing tool (kapt) or (ksp) MUST HAVE!
    ksp("androidx.room:room-compiler:2.5.2")
    implementation ("androidx.room:room-ktx:$room_version")

    //Lottie
    implementation("com.airbnb.android:lottie-compose:6.0.1")

    implementation ("androidx.datastore:datastore-preferences:1.0.0")
}

MyApplication.kt

@HiltAndroidApp
class MyApplication : Application() {}

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

<application
        android:name=".MyApplication"
        ...
/application>

package templates

  • components
  • data
  • di
  • model
  • navigation
  • network
  • repository
  • screens
  • utils

di/AppModule

//This module is gonna provide the instances

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    // @Provides
    // @Singleton

}

MainActivity.kt

@AndroidEntryPoint
class MainActivity : ComponentActivity() { //...

Firebase Test Function

###(Observe in Logcat) You can call it anywhere proper in MainActivity

private fun firebaseTest() {
    val db = Firebase.firestore

    val user: MutableMap<String, Any> = HashMap()
    val FN = "name"
    val LN = "surname"

    user[FN] = "Ruhi"
    user[LN] = "Su"

    db
        .collection("users")
        .add(user)
        .addOnSuccessListener {
            Log.d(TAG, "onCreate: ${it.id}")
        }
        .addOnFailureListener {
            Log.d(TAG, "onCreate: $it")
        }
}

Warning If you are getting error like: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.

Firebase Account > Database > Rules:

service cloud.firestore {
 match /databases/{database}/documents {
   match /{document=**} {
     allow read, write: if request.auth != null;
   }
 }
}

or simply change false to true for development purposes.