我正在使用在我的 android 客户端protobuf
中以技术方式自动生成我的 API 服务。gRPC
自从我一直在没有问题地工作以来已经有几个月了。现在突然之间,AndroidRuntime 验证器拒绝了我的 API 的自动生成方法之一。
这是堆栈跟踪:
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帖子上建议的解决方案)不起作用:
- 清理并重建(甚至尝试手动清理)
- 使缓存无效并重新启动 IDE
- 从 proto 重新生成 API 定义
此外,这是堆栈跟踪中指出的自动生成的方法:
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
. 这是重现此问题的 MWE 的链接。