🌊

How to use ProfoBuf in Android App

1 min read

Add below in project level build.gradle

classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.12'

Add protobuf dependency in app level build.gradle

implementation 'com.google.protobuf:protobuf-lite:3.0.1'

Add plugin in app level build.gradle

apply plugin: 'com.google.protobuf'

Add this in app level build.gradle

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        javalite {
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
    }
// this is a task which wil generate classes for our proto files
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite {}
            }
        }
    }
}

sourceSets {
    main.java.srcDirs += "${protobuf.generatedFilesBaseDir}/main/javalite"
}

processResources {
    exclude('**/*.proto')
}

Add proto files in app -> src -> main-> proto folder

.java files will be generated from proto file in the build -> generated folder when you build the project

Discussion

ログインするとコメントできます