【问题标题】:java.lang.VerifyError: Verifier rejected class StreamingRecognizeResponse due to bad methodjava.lang.VerifyError:由于方法错误,验证程序拒绝了类 StreamingRecognizeResponse
【发布时间】:2017-10-02 06:49:18
【问题描述】:

嘿,我正在尝试在 Android 中使用 Google Cloud Speech API,但我收到此错误并且无法解决。

java.lang.VerifyError: Verifier rejected class com.google.cloud.speech.v1
  .StreamingRecognizeResponse due to bad method java.lang.Object com.google
  .cloud.speech.v1.StreamingRecognizeResponse.dynamicMethod(com.google.protobuf
  .GeneratedMessageLite$MethodToInvoke, java.lang.Object, java.lang.Object) 
  (declaration of 'com.google.cloud.speech.v1.StreamingRecognizeResponse' 
  appears in /data/app/com.curieo.podcast-1/base.apk:classes65.dex)

  at com.google.cloud.speech.v1.StreamingRecognizeResponse.getDefaultInstance(StreamingRecognizeResponse.java:1095)
  at com.google.cloud.speech.v1.SpeechGrpc.<clinit>(SpeechGrpc.java:67)
  at com.curieo.podcast.ui.fragment.dates.googlecloud.StreamingRecognizeClient.<init>(StreamingRecognizeClient.java:57)
  at com.curieo.podcast.ui.fragment.dates.googlecloud.MicrophoneStreamRecognizeClient.<init>(MicrophoneStreamRecognizeClient.java:54)
  at com.curieo.podcast.ui.fragment.dates.RecordFragment$1.run(RecordFragment.java:112)

我对此进行了搜索,但找不到解决方案。这是发生错误的代码

private Thread runner = new Thread() {

    public void run() {

        try {
            MicrophoneStreamRecognizeClient client;
            synchronized (this) {
                try {
                    client = new MicrophoneStreamRecognizeClient(getResources().openRawResource(R.raw.credential), Self); //crashes here 
                    client.start();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
};

这里是MicrophoneStreamRecognizeClient类的代码sn-p:

public class MicrophoneStreamRecognizeClient {

private String host = "speech.googleapis.com";
private Integer port = 443;
private ManagedChannel channel;
private StreamingRecognizeClient client;

private final List<String> OAUTH2_SCOPES = Arrays.asList("https://www.googleapis.com/auth/cloud-platform");

/**
 *
 * @param authorizationFile
 * @param host
 * @param port
 * @return
 * @throws IOException
 */
 private ManagedChannel createChannel(InputStream authorizationFile, String host, int port) throws IOException {

    GoogleCredentials creds = GoogleCredentials.fromStream(authorizationFile);
    creds = creds.createScoped(OAUTH2_SCOPES);
    return ManagedChannelBuilder.forAddress(host, port)
        .intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor()))
        .build();
}

/**
 *
 * @param autorizationFile
 * @throws IOException
 */
 public MicrophoneStreamRecognizeClient(InputStream autorizationFile, IResults screen) throws IOException {

    channel = createChannel(autorizationFile, host, port);
    client = new StreamingRecognizeClient(channel, screen);
}

/**
 *
 * @throws IOException
 * @throws InterruptedException
 */
 public void start() throws IOException, InterruptedException {

    client.recognize();
}

/**
 *
 * @throws InterruptedException
 */
 public void stop() throws InterruptedException {

    client.shutdown();
}

}

【问题讨论】:

  • 可以分享一下 MicrophoneStreamRecognizeClient 类声明吗?
  • 你扩展了任何类吗?不清楚!你在哪里得到shutdown()和recognize()?
  • 您可以查看此link。它显示了完整的示例。 @EnamulHaque

标签: java android google-cloud-speech


【解决方案1】:

清理build 文件夹解决了这个问题。不知道为什么 ART 有问题,但 Dalvik 没有。

运行gradle clean 任务并没有一直清除我的构建文件夹。我必须手动完成,但 clean 可能对某些人有用。

Reference

java.lang.VerifyError 可能出于某种原因发生:

  1. 一个类试图扩展一个声明为 final 的类

  2. 方法试图覆盖声明为 final 的超级方法

  3. 一个错误的参数被传递给一个方法

     clint = new MicrophoneStreamRecognizeClient(getResources()
             .openRawResource(R.raw.credential), Self); //crashes here 
    

我不知道Self 是什么。可以吗?

试试这个。

  synchronized (this) {
     MicrophoneStreamRecognizeClient client;
     try {
        client = new MicrophoneStreamRecognizeClient(getResources().openRawResource(R.raw.credential), Self); //crashes here 
         client.start();
     } catch (InterruptedException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }
 }

因为另一个问题可能是! try / catch 块内的同步块! java.lang.VerifyErrorreference的原因

【讨论】:

  • 我已经尝试过,仍然是同样的错误。 self 变量是一个接口变量,用于打印结果@EnamulHaque
  • 我明白了.... 将 try catch 放入同步块后是否有相同的异常?
  • 是的,是一样的。 @EnamulHaque
【解决方案2】:

如果java.lang.VerifyError 被触发是因为library differences between runtime and compiling,正如@Enamul 所建议的那样,那么您可能想检查com.google.cloud.speech.v1.StreamingRecognizeResponsecom.google.cloud.speech.v1beta1.StreamingRecognizeResponse 之间的差异。

即便如此,请尝试使用该库的 beta 更新版本。启发您的示例使用的是测试版。

【讨论】:

猜你喜欢
  • 2017-06-11
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多