How to build Google protocol buffers and Kotlin using Gradle?

前端 未结 3 1392
灰色年华
灰色年华 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:08

    I was able to get this to work by adding two lines to my build.gradle.

    The first line adds the directory in which the proto compiler generates Java code to the directories the :compileKotlin looks for Java source:

    sourceSets.main.java.srcDirs += 'build/generated/source/proto/main/java'
    

    The second ensures that the Java code is (re)generated before invoking :compileKotlin:

    compileKotlin.dependsOn ':generateProto'
    
    0 讨论(0)
  • 2021-02-13 05:08

    For Kotlin and Android:

    android {
    
        sourceSets {
            debug.java.srcDirs += 'build/generated/source/proto/debug/java'
            release.java.srcDirs += 'build/generated/source/proto/release/java'
        }
    }
    

    An additional source directory has to be added for every build type. In this sample there are two build types: debug and release.

    If you're using grpc, another line has to be added per build type:

    android {
    
        sourceSets {
            debug.java.srcDirs += 'build/generated/source/proto/debug/java'
            debug.java.srcDirs += 'build/generated/source/proto/debug/grpc'
            release.java.srcDirs += 'build/generated/source/proto/release/java'
            release.java.srcDirs += 'build/generated/source/proto/release/grpc'
        }
    }
    

    At least with Kotlin 1.0.6, protobuf-gradle-plugin 0.8.0, protobuf 3.2.x and grpc 1.x it's not required to fiddle with the task order.

    0 讨论(0)
  • 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')
    }
    
    0 讨论(0)
提交回复
热议问题