protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Java Android does not have "pack" defined in Any class

Open AhmadChaker opened this issue 9 years ago • 7 comments

[EDIT: I've tried to get a minimally reproducible sample by removing all grpc code and simply using protobuf code] I am using Grpc for Android, I have "Any" objects on my .proto file. On the C# server side I have pack and unpack methods, on the Android Java side my Any class does not have pack or unpack, it seems to have all the methods expected of it as listed in the following class reference except pack and unpack: https://developers.google.com/protocol-buffers/docs/reference/java/

I am using proto3, my proto files begin with: syntax = "proto3"; import "google/protobuf/any.proto";

My build.gradle files for the application has the following lines:

protobuf {
    protoc {
        // Download from repositories
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }

    plugins {
        javalite {
            // The codegen for lite comes as a separate artifact
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite { }
            }
        }
    }
}

My dependencies are:

dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' })

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'

compile 'com.google.protobuf:protobuf-lite:3.0.0'

compile 'javax.annotation:jsr250-api:1.0'
testCompile 'junit:junit:4.12'

}

Does anyone have any ideas on why this might be the case?

AhmadChaker avatar Jun 01 '17 01:06 AhmadChaker

I am facing the same issue . Any one here can help us ?

kraghu avatar Jul 12 '17 19:07 kraghu

Experiencing this as well, seems related to https://github.com/google/protobuf/issues/1974

willtrking avatar Jul 12 '17 19:07 willtrking

@gerben-s is there any progress on this? We are running into the same issue. Are there any workarounds?

codesuki avatar Mar 06 '18 03:03 codesuki

Pinging on this. The pack/unpack libraries have been available for a long time but there is still no support on Android for them. Could we get this prioritized please?

robcos avatar Feb 09 '22 09:02 robcos

It seems that the current work-out for using Any is use protobuf instead of protobuf-lite in Android, which at least will double the size of compiled code of protos.

Shawlaw avatar Aug 22 '22 16:08 Shawlaw

Unfortunately, the Lite java system does not have proto reflection. That makes it impossible to provide the same API as the non-Lite version.

googleberg avatar Sep 01 '22 17:09 googleberg

This is what I did in Android Java on my project as a pack function:

String name = message.getClass().getSimpleName(); String packageName = message.getClass().getPackage().getName(); //needs to be the same as your proto package String fullName = packageName + '.'+ name; String typeURL = GetTypeUrl(fullName);

Any any = Any.newBuilder() .setValue(message.toByteString()) .setTypeUrl(typeURL) .build();

private String GetTypeUrl(String fullName) { String prefix = "type.googleapis.com"; return prefix + "/" + fullName; }

fernandocz2 avatar Feb 24 '23 21:02 fernandocz2