Glide showing error: Failed to find GeneratedAppGlideModule

Glide showing error: Failed to find GeneratedAppGlideModule

我正在尝试使用 glide 加载图像,但不知何故我无法使用 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.

我也参考过This solution。但是,我已经有了glide的更新版本。

在我的 gradle 中,我添加了

implementation 'com.github.bumptech.glide:glide:4.7.1'

annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'

代码

XML

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".view.SettingActivity">

    <data>
        <variable
            name="settingsViewModel"
            type="com.sevenbits.android.mvvmsample.viewmodel.SettingsViewModel"/>

    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/splash_bg">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/profile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:background="@color/white"
                android:elevation="10dp"
                android:orientation="vertical"
                android:padding="5dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center"
                    android:layout_margin="10dp"
                    app:image_url="@{settingsViewModel.imageUrl}"
                    app:civ_border_width="2dp"
                    app:civ_border_color="@color/colorPrimary"/>

                  ...
            </LinearLayout>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

自定义绑定适配器

public class CustomBindingAdapter {

@BindingAdapter({"bind:image_url"})
public static void loadImage(ImageView imageView, String url) {

    RequestOptions requestOptions = new RequestOptions();
    requestOptions=requestOptions.placeholder(R.drawable.boy_32);

        Glide.with(imageView.getContext())
                .load(url)
                .apply(requestOptions)
                .into(imageView);
}

终于,我找到了答案here

我做了什么:

第一步

我创建了一个名为 GlideApp

的空 class
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

@GlideModule
public class GlideApp extends AppGlideModule {

}

注意:不要忘记添加注释@GlideModule

第 2 步 之后,我 build/rebuild 项目,然后,将 Glide 替换为 GlideApp。现在不需要使用 RequestOptions

public class CustomBindingAdapter {

    @BindingAdapter({"bind:image_url"})
    public static void loadImage(ImageView imageView, String url) {

//        RequestOptions requestOptions = new RequestOptions();
//        requestOptions=requestOptions.placeholder(R.drawable.boy_32);

        GlideApp.with(imageView.getContext())
                .load(url)
                .placeholder(R.drawable.boy_32)
                .into(imageView);

//            Glide.with(imageView.getContext())
//                    .load(url)
//                    .apply(requestOptions)
//                    .into(imageView);
    }
}

编辑: 对于 androidx 和 Glide 版本 4.9.0:

在我的应用程序中 gradle.build:

implementation ("com.github.bumptech.glide:glide:4.9.0") {
    exclude group: "com.android.support"
}
annotationProcessor 'androidx.annotation:annotation:1.0.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation ("com.github.bumptech.glide:glide:4.9.0@aar") {
    transitive = true
}

在我的gradle.properties:

android.enableJetifier=true
android.useAndroidX=true

就这些了。

Kotlin 解决方案:

确保在 gradle 文件中添加以下内容(将 annotationProcessor 替换为 kapt source):

repositories {
  mavenCentral()
  google()
}

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    kapt 'com.github.bumptech.glide:compiler:4.8.0'
}

在您的应用程序 [GlideSource][2] 中添加 **AppGlideModule** 实现(您可以覆盖默认方法 [overrideSource][3]):
import android.content.Context
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.module.AppGlideModule
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.signature.ObjectKey

@GlideModule
class AppNameGlideModule : AppGlideModule() {

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        super.applyOptions(context, builder)
        builder.apply { RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL) }
    }

}

在使用 glide 时,使用 **GlideApp** 而不是 **Glide**,例如:
GlideApp.with(context)
            .load(url)
            .into(imageView)

除了 Ridhi 的回答:

为了让它正常工作,我必须包括 isManifestParsingEnabled

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

@GlideModule
public class MyGlideApp extends AppGlideModule {

    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }

}

我在 Glide:4.9.0AndroidX
中遇到过这个问题 就这样解决了

在你的gradle.properties
android.useAndroidX = true
android.enableJetifier=true

在你的build.gradle
//Glide dependency implementation 'com.github.bumptech.glide:glide:4.9.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

然后添加你的CustomGlideModule
@GlideModule public class CustomeGlideModule extends AppGlideModule {}

最后一步生成GlideModule
Build >> Make Project 你应该会在构建文件夹中看到生成的模块

以上所有答案都是正确的并且工作正常

但我注意到当使用 #placeholder#error 方法时,glide 在没有构建 GlideModule[ 的情况下工作正常=24=] class 如上所述

示例: 当像这样使用 glide 时不起作用并且必须构建 GlideModule

Glide.with(this)
             .load(uri)
                .into(imageView);

这很好用

Glide.with(this).load(uri).placeholder(android.R.drawable.progress_indeterminate_horizontal).error(android.R.drawable.stat_notify_error).into(imageView);

在 build.gradle(应用)

def glide_version = "4.11.0"
implementation ("com.github.bumptech.glide:glide:$glide_version"){
    exclude group: "com.android.support"
}
kapt "com.github.bumptech.glide:compiler:$glide_version"

在 build.gradle(项目)

repositories {
    google()
    jcenter()
        ....
    maven { url 'https://maven.google.com' }
        ....
}

在你们各自的Class

import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.BitmapImageViewTarget

Glide.with(imageView.rootView.context)
.asBitmap()
.load("your url")
.into(object : BitmapImageViewTarget(imageView) {
     override fun setResource(resource: Bitmap?) {
         imageView.setImageBitmap(resource)
         super.setResource(resource)
     }
})

我也不得不面对这样的问题 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.

我花了很多时间解决了我的问题。

我在 XML 文件中出错 我设置了错误的 UIImageView 高度宽度 这就是为什么滑行抛出一个错误。

请正确检查UIImageView高宽。