简体   繁体   English

如何将protobuf添加到flutter/kotlin android项目中

[英]How to add protobuf into flutter/kotlin android project

I'm trying to add protobuf to my flutter project.我正在尝试将 protobuf 添加到我的 flutter 项目中。 Here's the tree ls flutter_project :这是树ls flutter_project

android  docker  ios   protobuf      Readme.md         test
assets   docs    lib   pubspec.lock  
build    gen     oboe  pubspec.yaml  

.proto files are in the protobuf folder .proto文件在protobuf文件夹中

I followed the instructions on https://github.com/google/protobuf-gradle-plugin .我按照https://github.com/google/protobuf-gradle-plugin上的说明进行操作。 Here are my build.gradle files:这是我的 build.gradle 文件:

flutter_project/android/build.gradle : flutter_project/android/build.gradle

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.14'
    }
}

//...

flutter_project/android/app/build.gradle : flutter_project/android/app/build.gradle

//...

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
apply plugin: 'kotlin-android'

apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 29

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        main {
            proto {
                srcDir '../../protobuf/proto_java/'
            }
        }
    }

//..

The project syncs ok, but I cannot import any of the proto files in my MainActivity.kt .项目同步正常,但我无法在我的MainActivity.kt中导入任何原型文件。 For example, the protobuf file:例如,protobuf 文件:

syntax = "proto3";
package proto_pack
enum MessageResponseCode {

}

I cannot do import proto_pack on MainActivity.kt neither typing MessageResponseCode suggests anything.我无法在MainActivity.kt上执行import proto_pack ,键入MessageResponseCode也没有任何提示。

First of I don't see your configuration for the protobuf compiler, if it is not set it will search for it in the system path (Is it available?).首先,我没有看到您对 protobuf 编译器的配置,如果未设置,它将在系统路径中搜索它(是否可用?)。 Usually to avoid issues I'm using the distributed version of the compiler by configuring the protobuf configuration in the app gradle build file.通常为了避免出现问题,我通过在应用程序 gradle 构建文件中配置 protobuf 配置来使用编译器的分布式版本。 Also i'm using the lite runtime as it is the recommended way for Android.我还使用了精简版运行时,因为它是 Android 的推荐方式。

dependencies {
    implementation  "com.google.protobuf:protobuf-javalite:3.10.0"
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.10.0"
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option 'lite'
                }
            }
        }
    }
}

Try to compile your application, you should see the java generated file in your flutter_project/build/app/generated/source/proto/debug/java/proto_pack尝试编译您的应用程序,您应该在flutter_project/build/app/generated/source/proto/debug/java/proto_pack中看到 java 生成的文件

If so your class should be available in the MainActivity.如果是这样,您的 class 应该在 MainActivity 中可用。

Last point, your proto file is not syntactically correct but i assume that it is was just for the example.最后一点,您的原型文件在语法上不正确,但我认为它只是为了示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM