How to build Google protocol buffers and Kotlin using Gradle?

前端 未结 3 1388
灰色年华
灰色年华 2021-02-13 05:07

I\'m trying to build a project that uses both Google protocol buffers and Kotlin using Gradle. I want the proto files to compile into Java source, which is then called from my K

3条回答
  •  一向
    一向 (楼主)
    2021-02-13 05:21

    if you are working with multiple build types and flavors in android and with protobuf-lite use below with kotlin.

    for example I have debug and release builds with demo and prod flavors it will create demoDebug, demoRelease and prodDebug and prodRelease variants.

    then use

    `

    android{
        sourceSets {
            debug.java.srcDirs += 'build/generated/source/proto/demoDebug/javalite'
            debug.java.srcDirs += 'build/generated/source/proto/prodDebug/javalite'
            release.java.srcDirs += 'build/generated/source/proto/demoRelease/javalite'
            release.java.srcDirs += 'build/generated/source/proto/prodRelease/javalite'
          }
        }
    

    `

    tie the different compileKotlin with generateProto

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
      if (getName() == 'compileDemoDebugKotlin')
        dependsOn(':app:generateDemoDebugProto')
      if (getName() == 'compileDemoReleaseKotlin')
        dependsOn(':app:generateDemoReleaseProto')
      if (getName() == 'compileProdDebugKotlin')
        dependsOn(':app:generateProdDebugProto')
      if (getName() == 'compileProdReleaseKotlin')
        dependsOn(':app:generateProdReleaseProto')
    }
    

提交回复
热议问题