Android/Module

[Android Multi module] DataStore protobuf 라이브러리 추가 방법

whjungdev 2024. 9. 13. 20:48

1. libs.versions.toml 파일에 protobuf 라이브러리 추가

protobuf = "3.21.12"
protobufPlugin = "0.9.0"

[libraries]
protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }
protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" }

[plugins]
protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" }

https://github.com/woohyun-jeong/AndroidSampleApp/blob/datastore_test1/gradle/libs.versions.toml

 

2. core:datastore 모듈의 build.gradle.kts 세팅 추가

@Suppress("DSL_SCOPE_VIOLATION")
plugins {
    id("example.android.library")
    alias(libs.plugins.protobuf)

}

android {
    namespace = "com.example.sampleapp.core.datastore"
}

// Setup protobuf configuration, generating lite Java and Kotlin classes
protobuf {
    protoc {
        artifact = libs.protobuf.protoc.get().toString()
    }
    generateProtoTasks {
        all().forEach { task ->
            task.builtins {
                val java by registering {
                    option("lite")
                }
                val kotlin by registering {
                    option("lite")
                }
            }
        }
    }
}

dependencies {
    testImplementation(libs.junit4)
    testImplementation(libs.kotlin.test)

    implementation(libs.androidx.datastore)
    implementation(libs.androidx.datastore.core)
    api(libs.androidx.datastore.core)
    api(libs.protobuf.kotlin.lite)

}

https://github.com/woohyun-jeong/AndroidSampleApp/blob/datastore_test1/core/datastore/build.gradle.kts

샘플 프로젝트 : https://github.com/woohyun-jeong/AndroidSampleApp/tree/datastore_test1