Trần Kim Dự
Trần Kim Dự

Reputation: 6102

Protobuf on Android: Error Duplicate classes defined in protoc file when compile

I want to write a Grpc client on Android. I follow this tutorial

Here is my outer build.gradle file:

apply plugin: 'java'
apply plugin: 'com.google.protobuf'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0"

    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.2.0"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.3.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

allprojects {
    repositories {
        jcenter()
        mavenLocal()
    }
}

Here is my inner file build.gradle file:

dependencies {
    compile 'com.android.support:appcompat-v7:23.+'

    // You need to build grpc-java to obtain these libraries below.
    compile 'io.grpc:grpc-okhttp:1.3.0' // CURRENT_GRPC_VERSION
    compile 'io.grpc:grpc-protobuf-lite:1.3.0' // CURRENT_GRPC_VERSION
    compile 'io.grpc:grpc-stub:1.3.0' // CURRENT_GRPC_VERSION
    compile 'javax.annotation:javax.annotation-api:1.2'
}

When I tried to build project, I always meet errors:

Error:(14, 15) error: duplicate class: io.grpc.routeguideexample.Feature
Error:(13, 15) error: duplicate class: io.grpc.routeguideexample.FeatureDatabase
// ...

With Feature ... is class that I have defined in protoc file. Please help me figure out what wrong in my code.

Thanks

Upvotes: 2

Views: 2650

Answers (1)

Eric G
Eric G

Reputation: 4058

If you have defined a Java class io.grpc.routeguideexample.Feature in your code, this will conflict with the generated class name from the route_guide.proto file, causing the error you described. You can either change the name or package of your own Feature and FeatureDatabase class, or change the route_guide.proto to use a different Java package name. In general, you can look for a directory like build/generated/source/proto/.../javalite/io/grpc/routeguideexample/ to see the actual generated code from your proto files.

Upvotes: 1

Related Questions