kotlinandroid-studiogradleprotocol-buffersdatastore

Setting up Protobuf Android Studio Giraffe


I am encountering build errors trying to setup protobuf in my Android Studio project.

I’ve been trying to learn more about Datastores in Android development and am currently in the process of trying to setup a Proto Datastore in Android Studio (Giraffe 2022.3.1). Despite my different approaches, my project won’t build and I end up with varying error messages. I haven’t been able to find many recent tutorials, so I am following this at the moment: Setting up Protobuf + Kotlin in Android Studio 2023

My module build.gradle looks like this:

import com.google.protobuf.gradle.*


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


android {
   namespace = "com.name.myapplication"
   compileSdk = 34


   defaultConfig {
       applicationId = "com.name.myapplication"
       minSdk = 28
       targetSdk = 34
       versionCode = 1
       versionName = "1.0"


       testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
       vectorDrawables {
           useSupportLibrary = true
       }
   }


   buildTypes {
       release {
           isMinifyEnabled = false
           proguardFiles(
               getDefaultProguardFile("proguard-android-optimize.txt"),
               "proguard-rules.pro"
           )
       }
   }
   compileOptions {
       sourceCompatibility = JavaVersion.VERSION_1_8
       targetCompatibility = JavaVersion.VERSION_1_8
   }
   kotlinOptions {
       jvmTarget = "1.8"
   }
   buildFeatures {
       compose = true
   }
   composeOptions {
       kotlinCompilerExtensionVersion = "1.4.3"
   }
   packaging {
       resources {
           excludes += "/META-INF/{AL2.0,LGPL2.1}"
       }
   }
}


dependencies {


   implementation("androidx.core:core-ktx:1.12.0")
   implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
   implementation("androidx.activity:activity-compose:1.8.1")
   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")
   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")


   implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
   implementation("com.google.protobuf:protobuf-kotlin:3.21.2")
   implementation("io.grpc:grpc-stub:1.52.0")
   implementation("io.grpc:grpc-protobuf:1.52.0")
   implementation("io.grpc:grpc-okhttp:1.52.0")


   implementation("com.google.protobuf:protobuf-java-util:3.21.7")
   implementation("com.google.protobuf:protobuf-kotlin:3.21.2")
   implementation("io.grpc:grpc-kotlin-stub:1.3.0")
}


protobuf {
   protoc {
       artifact = "com.google.protobuf:protoc:${rootProject.ext["3.21.2"]}"
   }
   plugins {
       id("java") {
           artifact = "io.grpc:protoc-gen-grpc-java:${rootProject.ext["1.47.0"]}"
       }
       id("grpc") {
           artifact = "io.grpc:protoc-gen-grpc-java:${rootProject.ext["1.47.0"]}"
       }
       id("grpckt") {
           artifact = "io.grpc:protoc-gen-grpc-kotlin:${rootProject.ext["1.3.0"]}:jdk8@jar"
       }
   }
   generateProtoTasks {
       all().forEach {
           it.plugins {
               id("java") {
                   option("lite")
               }
               id("grpc") {
                   option("lite")
               }
               id("grpckt") {
                   option("lite")
               }
           }
           it.builtins {
               id("kotlin") {
                   option("lite")
               }
           }
       }
   }
}

And my project build.gradle looks like this:

plugins {
   id("com.android.application") version "8.1.4" apply false
   id("org.jetbrains.kotlin.android") version "1.8.10" apply false
   id("com.google.protobuf") version "0.8.15" apply false
}

And this is the error I get after I sync:

Exception is: org.gradle.api.plugins.ExtraPropertiesExtension$UnknownPropertyException: Cannot get property '3.21.2' on extra properties extension as it does not exist.

I’ve tried using different versions but I still get that Exception. Also, trying to setup sourceSets usually ends with ‘main’ turning red and the message: Expression 'main' cannot be invoked as a function. The function 'invoke()' is not found

I don’t understand where to look to resolve this. I am using Kotlin DSL if that makes a difference. Thanks


Solution

  • I fixed it. Turned out, all I needed to was change:

    id("com.google.protobuf") version "0.8.15" apply false
    

    to:

    id("com.google.protobuf") version "0.9.4" apply false
    

    in my project-level build.gradle.kts and then it was all downhill from there.