使用Glide加载Drawable资源遭遇NoClassDefFoundError问题求助
解决Glide 4.4.0在Fragment中触发java.lang.NoClassDefFoundError的问题
从你提供的代码、Gradle配置和报错日志来看,这个错误是因为Glide的Gif相关核心类被混淆或者未正确加载导致的。结合你的Fragment场景,给你几个针对性的解决方案:
1. 修复ProGuard混淆规则
你在release构建中开启了minifyEnabled true和shrinkResources true,这大概率会把Glide的Gif解析相关类误删或混淆。请在proguard-rules.pro中添加Glide官方推荐的混淆规则,尤其是Gif相关类的保护:
# Glide基础混淆规则 -keep public class * implements com.bumptech.glide.module.GlideModule -keep public class * extends com.bumptech.glide.module.AppGlideModule -keep public enum com.bumptech.glide.load.ImageHeaderParser$** { **[] $VALUES; public *; } # 专门保护Gif解析相关类 -keep class com.bumptech.glide.load.resource.gif.** { *; }
2. 升级Glide到稳定新版本
Glide 4.4.0是比较旧的版本,可能存在与Android 26版本SDK或Kotlin的兼容性问题。建议升级到较新的稳定版本,同时注意将过时的compile替换为implementation:
dependencies { // 替换原Glide依赖 implementation 'com.github.bumptech.glide:glide:4.15.1' kapt 'com.github.bumptech.glide:compiler:4.15.1' // 其他依赖保持不变 }
3. 确保MultiDex配置完全生效
虽然你开启了multiDexEnabled true,但如果你的Application没有正确初始化MultiDex,也可能导致类加载失败:
- 如果你的自定义Application继承了
MultiDexApplication,那没问题; - 如果没有,在你的Application类中添加:
override fun attachBaseContext(base: Context?) { super.attachBaseContext(base) MultiDex.install(this) }
4. 优化Fragment中Glide的使用方式
你当前使用Glide.with(context),在Fragment中更推荐直接绑定Fragment的生命周期,避免context生命周期不一致的问题:
Glide .with(this) // 使用Fragment实例代替context .load(R.drawable.a12) .into(imageView)
5. 清理项目缓存并重建
有时候Android Studio的缓存会导致类加载异常,执行以下操作:
- 点击菜单栏
Build->Clean Project - 然后点击
Build->Rebuild Project - 必要时可以手动删除项目根目录下的
.gradle和app/build文件夹,再重新同步Gradle
你的问题相关信息:
Fragment代码:
class F12 : Fragment() { override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val myImageViewLayout : View = inflater!!.inflate(R.layout.f12, container, false) val imageView = myImageViewLayout.findViewById<ImageView>(R.id.preview12) Glide .with(context) .load(R.drawable.a12) .into(imageView) return myImageViewLayout } override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { val shareButton =view!!.findViewById<Button>(R.id.b12) val wallButton = view.findViewById<Button>(R.id.wb12) val adRequest = AdRequest.Builder().build() shareButton.setOnClickListener { val shareIntent = Intent() shareIntent.action = Intent.ACTION_SEND shareIntent.putExtra(Intent.EXTRA_TEXT, "ከመሸ ፍቅራችን ደራ…ወደኋላ አንመልሰዉ ነገር ሆነብን….ቀልዳችን የምር ሲሆን፣ቅዠት ያልነዉ እዉን ሲሆን ሲጨላልም አየነዉ") shareIntent.type = "text/plain" startActivity(Intent.createChooser(shareIntent, "ሼር ያድርጉ...")) } } }
App模块Gradle配置:
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.3" defaultConfig { applicationId "com.afc.amhariclovequotes" minSdkVersion 14 targetSdkVersion 26 versionCode 1 versionName "1.0" multiDexEnabled true } buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } dexOptions { javaMaxHeapSize "4g" } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" compile 'com.android.support:appcompat-v7:26.0.0' compile 'com.android.support:mediarouter-v7:26.0.0' compile 'com.android.support:design:26.0.0' compile 'com.google.firebase:firebase-ads:11.6.0' compile 'com.google.firebase:firebase-crash:11.6.0' compile 'com.google.firebase:firebase-core:11.6.0' compile 'com.google.firebase:firebase-messaging:11.6.0' compile 'com.android.support:multidex:1.0.1' compile 'com.android.support.constraint:constraint-layout:1.0.1' compile 'com.github.bumptech.glide:glide:4.4.0@aar' kapt 'com.github.bumptech.glide:compiler:4.4.0' testCompile 'junit:junit:4.12' } repositories { mavenCentral() } apply plugin: 'com.google.gms.google-services'
报错日志:
FATAL EXCEPTION: main Process: com.afc_quotes.first_amharic_love_quotes, PID: 24273 java.lang.NoClassDefFoundError: com.bumptech.glide.load.resource.gif.GifBitmapProvider at com.bumptech.glide.load.resource.gif.ByteBufferGifDecoder.<init>(ByteBufferGifDecoder.java:68) at com.bumptech.glide.load.resource.gif.ByteBufferGifDecoder.<init>(ByteBufferGifDecoder.java:54) at com.bumptech.glide.Glide.<init>(Glide.java:327) at com.bumptech.glide.GlideBuilder.build(GlideBuilder.java:445) at com.bumptech.glide.Glide.initializeGlide(Glide.java:257) at com.bumptech.glide.Glide.initializeGlide(Glide.java:212) at com.bumptech.glide.Glide.checkAndInitializeGlide(Glide.java:176) at com.bumptech.glide.Glide.get(Glide.java:160) at com.bumptech.glide.Glide.getRetriever(Glide.java:612) at com.bumptech.glide.Glide.with(Glide.java:638) **at com.afc.amhariclovequotes.F12.onCreateView(F12.kt:50)** at android.support.v4.app.Fragment.performCreateView(Fragment.java:2337) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199) at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145) at android.support.v4.view.ViewPager.populate(ViewPager.java:1236) at android.support.v4.view.ViewPager.populate(ViewPager.java:1084) at android.support.v4.view.ViewPager$3.run(ViewPager.java:267) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:603) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
内容的提问来源于stack exchange,提问作者Haileapp
相关产品推荐
相关产品推荐

