我正在使用protobuf
在我的Android客户端中以gRPC
技术自动生成我的API服务。我已经好几个月没有问题了。现在突然间,AndroidRuntime Verifier拒绝了我的API的一个自动生成的方法。
这是stacktrace:
com.company.companyplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.companyplayer, PID: 18181
java.lang.VerifyError: Verifier rejected class company.v1.PlayerEarningsPageDataResponse: void company.v1.PlayerEarningsPageDataResponse.mergeNextDepositDate(com.google.protobuf.Timestamp) failed to verify: void company.v1.PlayerEarningsPageDataResponse.mergeNextDepositDate(com.google.protobuf.Timestamp): [0x26] register v5 has type Precise Reference: com.google.protobuf.Timestamp but expected Reference: com.google.protobuf.GeneratedMessageLiteVerifier rejected class company.v1.PlayerEarningsPageDataResponse: java.lang.Object company.v1.PlayerEarningsPageDataResponse.dynamicMethod(com.google.protobuf.GeneratedMessageLite$MethodToInvoke, java.lang.Object, java.lang.Object) failed to verify: java.lang.Object company.v1.PlayerEarningsPageDataResponse.dynamicMethod(com.google.protobuf.GeneratedMessageLite$MethodToInvoke, java.lang.Object, java.lang.Object): [0x16E] register v8 has type Precise Reference: com.google.protobuf.Timestamp but expected Reference: com.google.protobuf.GeneratedMessageLite (declaration of 'company.v1.PlayerEarningsPageDataResponse' appears in /data/app/com.company.companyplayer-1/split_lib_slice_4_apk.apk)
at company.v1.PlayerEarningsPageDataResponse.getDefaultInstance(PlayerEarningsPageDataResponse.java:0)
at company.v1.APIGrpc.<clinit>(APIGrpc.java:47)
at company.v1.APIGrpc.newStub(APIGrpc.java:0)
at com.company.companyplayer.backend.Client.getAuthAsyncStub(Client.java:103)
at com.company.companyplayer.backend.Client.playerLogin(Client.java:119)
at com.company.companyplayer.ui.activity.LoginActivity.loginAndGoToMainActivity(LoginActivity.java:202)
at com.company.companyplayer.ui.activity.LoginActivity.onActivityResult(LoginActivity.java:127)
at android.app.Activity.dispatchActivityResult(Activity.java:6935)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4086)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4133)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1534)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
以下(类似SO帖子建议的解决方案)不起作用:
此外,这里是stacktrace中指出的自动生成方法:
private void mergeNextDepositDate(com.google.protobuf.Timestamp value) {
if (nextDepositDate_ != null && nextDepositDate_ != com.google.protobuf.Timestamp.getDefaultInstance()) {
nextDepositDate_ = com.google.protobuf.Timestamp.newBuilder(nextDepositDate_).mergeFrom(value).buildPartial();
} else {
nextDepositDate_ = value;
}
}
可以看出,该方法既不冗长,也不需要太多参数;有人说这些错误的常见原因。
以下是我的应用的build.gradle:(自上次所有这些工作正常以来,gradle中没有更改任何内容)
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
defaultConfig {
applicationId "com.company.companyplayer"
minSdkVersion 15
targetSdkVersion 25
versionCode 5
versionName "0.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
}
lintOptions {
disable 'InvalidPackage'
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.2.0'
}
plugins {
javalite {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
}
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.3.0'
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc {
// Options added to --grpc_out
option 'lite'
}
}
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
/* android support libraries */
compile 'com.google.android:flexbox:0.2.7'
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.firebase:firebase-config:11.0.1'
compile 'com.google.firebase:firebase-core:11.0.1'
compile 'com.google.firebase:firebase-messaging:11.0.1'
compile 'com.google.android.gms:play-services-maps:11.0.1'
compile 'com.google.android.gms:play-services-location:11.0.1'
compile 'com.google.maps.android:android-maps-utils:0.4.4'
compile 'com.google.code.findbugs:jsr305:3.0.1'
compile 'io.grpc:grpc-okhttp:1.3.0'
compile 'io.grpc:grpc-protobuf-lite:1.3.0'
compile 'io.grpc:grpc-stub:1.3.0'
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.facebook.android:account-kit-sdk:4.22.0'
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'com.google.api.grpc:googleapis-common-protos:0.0.3' // This is for importing "google/api/annotations.proto" in our proto file
testCompile 'junit:junit:4.12'
androidTestCompile 'com.google.code.findbugs:jsr305:3.0.1'
protobuf 'com.google.protobuf:protobuf-java:3.1.0'
}
apply plugin: 'com.google.gms.google-services'
更新:该错误肯定与google.protobuf.Timestamp
有关。以下是再现此问题的link to an MWE。
答案 0 :(得分:3)
你正在混合精简和完整的protobuf。
您不能对googleapis-common-protos
具有编译或运行时依赖性,因为它是为完整protobuf生成的代码,它不适用于lite protobuf。它还会导致protobuf-java
的依赖,当与protobuf-lite
混合时会导致重复的类。使用protobuf依赖项而不是编译依赖项,让protobuf gradle插件生成JAR中包含的.proto
的代码。
您当前对protobuf-lite
也没有任何依赖关系,因此如果没有常见的protos依赖关系,您将遇到编译失败。常见的protos也带来了所需的grpc-protobuf-lite
依赖。我建议在io.grpc:grpc-protobuf-lite:1.3.0
上添加编译时依赖项,这也将引入protobuf-lite
。
简而言之,对项目进行这些更改并进行编译:
diff --git a/app/build.gradle b/app/build.gradle
index 8f606bb..2fb997b 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -62,7 +62,8 @@ dependencies {
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'io.grpc:grpc-stub:1.3.0'
compile 'io.grpc:grpc-okhttp:1.3.0'
+ compile 'io.grpc:grpc-protobuf-lite:1.3.0'
- compile 'com.google.api.grpc:googleapis-common-protos:0.0.3'
+ protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'
protobuf 'com.google.protobuf:protobuf-java:3.1.0'
}