Protobuf: google/protobuf/timestamp not found in java

Created on 3 Nov 2016  路  14Comments  路  Source: protocolbuffers/protobuf

I am trying to compile the following snippet in android app

import "base.proto";
import "google/protobuf/timestamp.proto";
package test;

service EventService {
  rpc SetEvent (SetEventRequest) returns (BaseReply);
}

I am getting error import google protobuf timestamp proto was not found or had errors. Is there anything wrong in the import statement or how can I import timestamp.proto.

Most helpful comment

You can actually just add a "protobuf" dependency to your build. The normal protobuf jar includes the .protos, so this will extract those to let import work and will generate the code for them.

dependencies {
  protobuf 'com.google.protobuf:protobuf-java:3.0.2'
}

All 14 comments

Shouldn't it just be "google/protobuf/timestamp.proto" instead of "google/protobuf/timestamp/timestamp.proto"? If that alone doesn't fix it then you might also need to make sure you add the right path to protoc with the -I option.

Am sorry that was a typo. It was "google/protobuf/timestamp.proto"
And how is the path to be provided when i am building with gradle

Oh ok, what does your build.gradle file look like? I'm not very familiar with Gradle but it sounds like it should be straightforward to add the path. @zhangkun83, have you seen this issue before?

This is my gradle file.

`apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.grpcsample"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions{
        disable 'InvalidPackage', 'HardcodedText'
        textReport true
        textOutput "stdout"
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.2'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.0.1' 
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite {}
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'io.grpc:grpc-okhttp:1.0.0'
    compile 'io.grpc:grpc-protobuf-lite:1.0.0'
    compile 'io.grpc:grpc-stub:1.0.0'
    compile 'javax.annotation:javax.annotation-api:1.2'
}`

I think what is going on is that the well-known types are not currently distributed with the lite runtime (see issue https://github.com/google/protobuf/issues/1889). For now the workaround is to run protoc yourself to generate the code for the well-known types, but hopefully we will have a better solution for this soon.

You can actually just add a "protobuf" dependency to your build. The normal protobuf jar includes the .protos, so this will extract those to let import work and will generate the code for them.

dependencies {
  protobuf 'com.google.protobuf:protobuf-java:3.0.2'
}

okay. that fixes my issue. thank you :)

You can actually just add a "protobuf" dependency to your build. The normal protobuf jar includes the .protos, so this will extract those to let import work and will generate the code for them.

dependencies {
  protobuf 'com.google.protobuf:protobuf-java:3.0.2'
}

After using this protobuf in my file i am getting error " Program type already present: com.google.protobuf.AbstractMessageLite$Builder$LimitedInputS"
This is due the same com.google.protobuf package is generated in both protobuflite and also protobuf dependency ,can you please tell how to solve this issue.

@harini2191, make sure you specified protobuf-java as a protobuf dependency. The Java classes are not included in the class path, so should not cause that error. You may also check your (transitive) dependencies; you may be pulling in protobuf-java from somewhere else.

@harini2191, make sure you specified protobuf-java as a protobuf dependency. The Java classes are not included in the class path, so should not cause that error. You may also check your (transitive) dependencies; you may be pulling in protobuf-java from somewhere else.

Now i have fixed this issue by using compile.exclude group: 'com.google.protobuf', module: 'protobuf-lite'.thanks for reply any way i am facing issues on "import "google/protobuf/any.proto";"this type of file are already there in protobuf jar but it telling file not found.

@harini2191, the protobuf 'com.google.protobuf:protobuf-java:3.0.2' solution was for users of protobuf-lite, since it doesn't yet include the generated code for well-known types. If you are wanting to use protobuf instead of protobuf-lite, then you shouldn't be impacted by this issue.

In any case, it seems you should open a new issue as you have Necro'd this issue and for something that seems a different issue.

@harini2191, the protobuf 'com.google.protobuf:protobuf-java:3.0.2' solution was for users of protobuf-lite, since it doesn't yet include the generated code for well-known types. If you are wanting to use protobuf instead of protobuf-lite, then you shouldn't be impacted by this issue.

In any case, it seems you should open a new issue as you have Necro'd this issue and for something that seems a different issue.

And i am facing new use that i am able to generate the java classes for Kotlin project.

My working build.gradle:

dependencies {

   implementation 'com.squareup.okhttp:okhttp:2.7.5'
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    implementation 'io.grpc:grpc-core:1.27.1'
    implementation 'io.grpc:grpc-stub:1.27.1'
    implementation 'io.grpc:grpc-okhttp:1.27.1'
    implementation('io.grpc:grpc-protobuf-lite:1.27.1') {
        exclude module: "protobuf-lite"
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.11.4'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.27.1'
        }
        javalite {
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option 'lite'
                }
            }

            task.plugins {
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

@harini2191 how did you fix your problem. I came across same steps and eventually ended at the point where I receive

AGPBI: {"kind":"error","text":"Program type already present: com.google.protobuf.Any$1","sources":[{}],"tool":"D8"}
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 

How did you fix your issue then?

Was this page helpful?
0 / 5 - 0 ratings