How to add protobuf into flutter/kotlin android project
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.
Gatonito
Updated on December 26, 2022Comments
-
Gatonito over 1 year
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 theprotobuf
folderI 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
onMainActivity.kt
neither typingMessageResponseCode
suggests anything. -
Gatonito over 3 yearswhere do I put the protobuf sources? Remember that they are in the root dir, not android dir
-
bcordier over 3 yearsYou have configured the proto source directory to be 'protobuf/proto_java' in the root directory of your project (flutter_project/), so it should be where you put them.
-
Gatonito over 3 yearshow much of my gradle files and yours should I preserve? I'm lost
-
Gatonito over 3 yearsI pasted your code to mine and I got
protoc: stdout: . stderr: datasets/google_message3/benchmark_message3_1.proto: File not found.
after trying to compile my app. Please look, I opened a bounty -
bcordier over 3 yearsFor the configuration you just needed to add the block in your android/app/build.gradle file without changing your configuration. As for your error, it seems that you are trying to include
datasets/google_message3/benchmark_message3_1.proto
in a proto file and it is not found in the configured proto source directory or your are trying to use some file that includesdatasets/google_message3/benchmark_message3_1.proto
which again do not exist. Without the complete proto file it would be difficult to debug.