From Android I want to download messages stored in the module PubSub
of Google Cloud , at the moment of making pull
the following error is received in time of execution:
Android GoogleCloud PubSub NoSuchMethodError: No virtual method shouldDiscardUnkownFields) Z in class Lcom / google / protobuf / CodedInputStream;
The program uses protobuf
, within the code of CodeInputStream
this function has the form:
final boolean shouldDiscardUnknownFields()
Because internally the dependencies and the code look for a "virtual" function when it is defined as "final"? What is happening?
The function where the error occurs is this, using AsyncTask
:
@Override
protected Integer doInBackground(String...params){
try{
Log.d(TAG, "Leyendo archivo de credenciales: "+MainActivity.CREDENTIALS_FILE);
AssetManager am = context.getAssets();
InputStream isCredentials = am.open(MainActivity.CREDENTIALS_FILE);
ManagedChannel channelImpl = OkHttpChannelBuilder.forAddress("pubsub.googleapis.com",443).negotiationType(NegotiationType.TLS).build();
Log.d(TAG, "Canal Terminado?:"+ channelImpl.isTerminated());
GoogleCredentials credential = GoogleCredentials.fromStream(isCredentials);
credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
ExecutorService executor = Executors.newSingleThreadExecutor();
ClientAuthInterceptor interceptor = new ClientAuthInterceptor(credential, executor);
Log.d(TAG, "Interceptor: " + interceptor.toString());
Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
Log.d(TAG, "Channel authority: " + channel.authority());
SubscriberGrpc.SubscriberBlockingStub subscriberStub = SubscriberGrpc.newBlockingStub(channel);
PullRequest pullRequest = PullRequest.newBuilder()
.setSubscription( MainActivity.SUBSCRIPTION )
.setReturnImmediately( true )
.setMaxMessages(10)
.build();
PullResponse pullResponse = subscriberStub.pull(pullRequest);
numMessages = pullResponse.getReceivedMessagesCount();
Log.d(TAG, "Number of messages received: " + String.valueOf(numMessages));
for (ReceivedMessage message1 : pullResponse.getReceivedMessagesList()) {
PubsubMessage pubsubMessage1 = message1.getMessage();
arrayList.add(pubsubMessage1.toString());
Log.d(TAG, "Message received (ID="+message1.getAckId()+"): " + pubsubMessage1.toString());
}
}catch (IOException e){
Log.d(TAG, "Excepcion: "+e.toString());
}
return 0;
}
The error occurs on the line:
PullResponse pullResponse = subscriberStub.pull(pullRequest);
This is the gradle
:
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.arkmind_2.googlecloud"
minSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'InvalidPackage', 'HardcodedText'
}
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
sourceSets {
main.java.srcDirs += "${protobuf.generatedFilesBaseDir}/main/javalite"
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:support-annotations:27.1.1'
compile 'com.google.protobuf:protobuf-java:3.5.1'
compile 'io.grpc:grpc-okhttp:1.12.0' // CURRENT_GRPC_VERSION
compile 'io.grpc:grpc-protobuf-lite:1.12.0' // CURRENT_GRPC_VERSION
compile 'io.grpc:grpc-stub:1.12.0' // CURRENT_GRPC_VERSION
compile 'io.grpc:grpc-testing:1.12.0' // CURRENT_GRPC_VERSION
compile 'io.grpc:grpc-auth:1.12.0'
compile 'com.google.api.grpc:grpc-google-cloud-pubsub-v1:0.1.22'
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'junit:junit:4.12'
compile 'com.google.auth:google-auth-library-oauth2-http:0.9.1'
androidTestCompile 'com.android.support.test:rules:1.0.1'
androidTestCompile 'com.android.support.test:runner:1.0.1'
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
javalite {
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.12.0'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
remove java
}
task.plugins {
javalite {}
grpc {
option 'lite'
}
}
}
}
}