Saturday 2 January 2021

How to add protobuf into flutter/kotlin android project

I'm trying to add protobuf to my flutter project. Here's the tree 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

I followed the instructions on https://github.com/google/protobuf-gradle-plugin. Here are my build.gradle files:

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 :

//...

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. For example, the protobuf file:

syntax = "proto3";
package proto_pack
enum MessageResponseCode {

}

I cannot do import proto_pack on MainActivity.kt neither typing MessageResponseCode suggests anything.



from How to add protobuf into flutter/kotlin android project

No comments:

Post a Comment