【问题标题】:YouTube API Exception In Initializer Error初始化程序错误中的 YouTube API 异常
【发布时间】:2019-12-09 06:49:31
【问题描述】:

所以过去几个月我一直在 Android Studio 中成功使用 YouTube API。我去更新我的应用程序,截至今天,该应用程序在尝试初始化 YouTube 构建器时不断崩溃。有没有其他人遇到过这个问题?

mYoutubeDataApi = new YouTube.Builder(mTransport, mJsonFactory, null)
                                .setApplicationName(getResources().getString(R.string.app_name))
                                .build();

应用程序崩溃并显示以下输出:

2019-12-09 01:38:06.443 17937-17937/ E/AndroidRuntime: FATAL EXCEPTION: main

    java.lang.ExceptionInInitializerError
        at com.google.api.services.youtube.YouTube.<clinit>(YouTube.java:44)
        at com.google.api.services.youtube.YouTube$Builder.build(YouTube.java:16644)

YouTube.java 文件中的第 44 行是:

public class YouTube extends com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient {

  // Note: Leave this static initializer at the top of the file.
  static {
    com.google.api.client.util.Preconditions.checkState(
        com.google.api.client.googleapis.GoogleUtils.MAJOR_VERSION == 1 &&
        com.google.api.client.googleapis.GoogleUtils.MINOR_VERSION >= 15,
        "You are currently running with version %s of google-api-client. " +
        "You need at least version 1.15 of google-api-client to run version " +
        "1.30.1 of the YouTube Data API library.", com.google.api.client.googleapis.GoogleUtils.VERSION);
  }

【问题讨论】:

标签: android youtube-api


【解决方案1】:

在 1.30.6 中,他们添加了这个:https://github.com/googleapis/google-api-java-client/pull/1419

要修复,请将您的 build.gradle 编辑回 1.30.5

dependencies {
  implementation ('com.google.api-client:google-api-client:1.30.5')
  implementation ('com.google.api-client:google-api-client-android:1.30.5')
}

如果有更好的解决方案,我想听听!


为了进一步解释 1.30.6 中的更改导致崩溃的原因,这里有更多信息。

具体来说,问题来自这个文件:https://github.com/googleapis/google-api-java-client/blob/master/google-api-client/src/main/java/com/google/api/client/googleapis/GoogleUtils.java

     Caused by: java.lang.IllegalStateException: No successful match so far
        at java.util.regex.Matcher.ensureMatch(Matcher.java:1116)
        at java.util.regex.Matcher.group(Matcher.java:382)
        at com.google.api.client.googleapis.GoogleUtils.<clinit>(Unknown Source:26)

这里是相关代码

  public static final String VERSION = getVersion();

  static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(-SNAPSHOT)?");

  static {
    Matcher versionMatcher = VERSION_PATTERN.matcher(VERSION);
    versionMatcher.find();
    MAJOR_VERSION = Integer.parseInt(versionMatcher.group(1));
    MINOR_VERSION = Integer.parseInt(versionMatcher.group(2));
    BUGFIX_VERSION = Integer.parseInt(versionMatcher.group(3));
  }

private static String getVersion() {
    String version = GoogleUtils.class.getPackage().getImplementationVersion();
    // in a non-packaged environment (local), there's no implementation version to read
    if (version == null) {
      // fall back to reading from a properties file - note this value is expected to be cached
      try (InputStream inputStream =
          GoogleUtils.class.getResourceAsStream("google-api-client.properties")) {
        if (inputStream != null) {
          Properties properties = new Properties();
          properties.load(inputStream);
          version = properties.getProperty("google-api-client.version");
        }
      } catch (IOException e) {
        // ignore
      }
    }
    return version;
  }

据推测,getVersion 正在返回 null,尽管我不能说明原因。鉴于最近 2-3 天前我也发生了这种情况,我们更新的内容一定是相互矛盾的。

【讨论】:

  • 这修复了它!非常感谢!
  • 别担心,我也很困惑。希望有比降级更好的解决方案,因为现在我们基本上停留在这个版本上。但与此同时......猜猜它有效。我认为您必须将我的答案标记为“已接受”
  • 谢谢 - 刚刚在这个愚蠢的问题上浪费了 2 个小时,然后才终于遇到这个问题。此处报告的错误:github.com/googleapis/google-api-java-client/issues/1451
【解决方案2】:

此错误已在 com.google.api-client:google-api-client:1.30.7 中修复。升级到该版本或更高版本将修复它。

【讨论】:

  • 不,这仍然发生在 'com.google.api-client:google-api-client-android:1.31.4'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-23
  • 2010-11-16
  • 1970-01-01
相关资源
最近更新 更多