Evil Mouth's Blog

Android Glide踩坑记 - AppGlideModule

November 06, 2018

在最新的Glide 4.x中,应该是4.x开始吧,官方改变了Glide的请求结构,许多 api 包括常用centerCrop()error()placeholder()等都需要通过RequestOptions去配置,从而导致从3.x迁移过来一路坎坷。所以官方为我们提供了AppGlideModule让我们保留以前的流式调用,然而…

0x01 You cannot call Glide.get() in registerComponents(), use the provided Glide instance instead

这是什么鬼,明明 debug 包玩的好好的,打个 release 包就崩溃了,赶紧点进去看看

public static Glide get(@NonNull Context context) {
  if (glide == null) {
    synchronized (Glide.class) {
      if (glide == null) {
        checkAndInitializeGlide(context);
      }
    }
  }
  return glide;
}

private static void checkAndInitializeGlide(@NonNull Context context) {
  // In the thread running initGlide(), one or more classes may call Glide.get(context).
  // Without this check, those calls could trigger infinite recursion.
  if (isInitializing) {
    throw new IllegalStateException("You cannot call Glide.get() in registerComponents(),"
        + " use the provided Glide instance instead");
  }
  isInitializing = true;
  initializeGlide(context);
  isInitializing = false;
}

调用Glide.with()会去拿单例,从而进入到checkAndInitializeGlide,报错的原因单例glide=null并且isInitializing,这东西怎么感觉不是我的问题。

这个是Glide的初始化问题,是什么导致的呢?协程?同步锁?混淆?

我一开始也是以为是混淆问题,所以赶紧去官网翻了下混淆文档 1

嗯?没什么问题,一切按照文档配置,针对自定义的AppGlideModule也有防混,那么只能上 issue 看看了

https://github.com/bumptech/glide/issues/2780

貌似有人说了初始化的问题,要放到Application.onCreate()去初始化???看了整页都没有解决方案,那只好试一下了

0x02 GeneratedAppGlideModuleImpl is implemented incorrectly. If you’ve manually implemented this class, remove your implementation. The Annotation processor will generate a correct implementation.

???虽然也是崩溃,不过这次报的错不一样,不过好像能看出什么

GeneratedAppGlideModuleImpl这个东西就是我们自定义AppGlideModule会帮我们生成的代理类,这里说它实现错误了

private static GeneratedAppGlideModule getAnnotationGeneratedGlideModules() {
  GeneratedAppGlideModule result = null;
  try {
    Class<GeneratedAppGlideModule> clazz =
        (Class<GeneratedAppGlideModule>)
            Class.forName("com.bumptech.glide.GeneratedAppGlideModuleImpl");
    result = clazz.getDeclaredConstructor().newInstance();
  } catch (ClassNotFoundException e) {
    if (Log.isLoggable(TAG, Log.WARN)) {
      Log.w(TAG, "Failed to find GeneratedAppGlideModule. You should include an"
          + " annotationProcessor compile dependency on com.github.bumptech.glide:compiler"
          + " in your application and a @GlideModule annotated AppGlideModule implementation or"
          + " LibraryGlideModules will be silently ignored");
    }
  // These exceptions can't be squashed across all versions of Android.
  } catch (InstantiationException e) {
    throwIncorrectGlideModule(e);
  } catch (IllegalAccessException e) {
    throwIncorrectGlideModule(e);
  } catch (NoSuchMethodException e) {
    throwIncorrectGlideModule(e);
  } catch (InvocationTargetException e) {
    throwIncorrectGlideModule(e);
  }
  return result;
}

private static void throwIncorrectGlideModule(Exception e) {
  throw new IllegalStateException("GeneratedAppGlideModuleImpl is implemented incorrectly."
      + " If you've manually implemented this class, remove your implementation. The Annotation"
      + " processor will generate a correct implementation.", e);
}

之所以报错就是找不到这个类,可以看到这里是通过Class.forName去查找的,既然找不到,说明很可能就是我一开始想的,被混淆了,赶紧打开mapping.txt看下

com.bumptech.glide.GeneratedAppGlideModuleImpl -> gi:

果然。。。那就加个混淆

-keep class com.bumptech.glide.GeneratedAppGlideModuleImpl { *; }

搞定,正常运行

— Evil Mouth