Reputation: 2174
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.
Upvotes: 2
Views: 1831
Reputation: 471
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 ?). Usually to avoid issues I'm using the distributed version of the compiler by configuring the protobuf configuration in the app gradle build file. Also i'm using the lite runtime as it is the recommended way for 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
If so your class should be available in the MainActivity.
Last point, your proto file is not syntactically correct but i assume that it is was just for the example.
Upvotes: 2