报错信息:
W/Glide: 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
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.frame.mvp.main, PID: 16067
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.frame.mvp.main/com.frame.mvp.main.MainActivity}: java.lang.RuntimeException: Expected instanceof GlideModule, but found: com.frame.mvp.main.MainAppGlideModule@1f317876
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2454)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2516)
at android.app.ActivityThread.access$1200(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5601)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: java.lang.RuntimeException: Expected instanceof GlideModule, but found: com.frame.mvp.main.MainAppGlideModule@1f317876
at com.bumptech.glide.module.ManifestParser.parseModule(ManifestParser.java:87)
at com.bumptech.glide.module.ManifestParser.parse(ManifestParser.java:47)
at com.bumptech.glide.Glide.initializeGlide(Glide.java:232)
at com.bumptech.glide.Glide.initializeGlide(Glide.java:223)
at com.bumptech.glide.Glide.checkAndInitializeGlide(Glide.java:184)
at com.bumptech.glide.Glide.get(Glide.java:168)
at com.bumptech.glide.Glide.getRetriever(Glide.java:689)
at com.bumptech.glide.Glide.with(Glide.java:716)
at com.frame.mvp.main.MainActivity$onCreate$1.displayImage(MainActivity.kt:22)
at com.frame.mvp.main.MainActivity$onCreate$1.displayImage(MainActivity.kt:20)
at com.youth.banner.Banner.setImageList(Banner.java:361)
at com.youth.banner.Banner.start(Banner.java:269)
at com.frame.mvp.main.MainActivity.onCreate(MainActivity.kt:24)
at android.app.Activity.performCreate(Activity.java:6100)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2407)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2516)
at android.app.ActivityThread.access$1200(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5601)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
对应代码:
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;
}
public List<GlideModule> parse() {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Loading Glide modules");
}
List<GlideModule> modules = new ArrayList<>();
try {
ApplicationInfo appInfo = context.getPackageManager()
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
if (appInfo.metaData == null) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got null app info metadata");
}
return modules;
}
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Got app info metadata: " + appInfo.metaData);
}
for (String key : appInfo.metaData.keySet()) {
if (GLIDE_MODULE_VALUE.equals(appInfo.metaData.get(key))) {
modules.add(parseModule(key));
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Loaded Glide module: " + key);
}
}
}
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException("Unable to find metadata to parse GlideModules", e);
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Finished loading Glide modules");
}
return modules;
}
原因是 通过反射实例化 com.bumptech.glide.GeneratedAppGlideModuleImpl 对象时,没有找到该java文件。这个类是build时候生成的类。
if (annotationGeneratedModule == null || annotationGeneratedModule.isManifestParsingEnabled()) {
manifestModules = new ManifestParser(applicationContext).parse();
}
这段代码可以看出 还有 isManifestParsingEnabled判断。
解决:
build.gradle 中加入
apply plugin: 'kotlin-kapt' // 加在顶部 apply plugin: 'com.android.application' 后面
kapt 'com.github.bumptech.glide:compiler:4.9.0' /加在 dependencies 中
重写方法:
@GlideModule
class MainAppGlideModule : AppGlideModule(){
override fun isManifestParsingEnabled(): Boolean {
return false
}
}