使用Glide加载图片编译报错:Program type already present: com.bumptech.glide.BitmapOptions
解决Glide编译冲突:Program type already present: com.bumptech.glide.BitmapOptions
这个错误是典型的依赖冲突——你的项目里多次引入了包含com.bumptech.glide.BitmapOptions类的依赖包,导致D8编译器无法确定使用哪一版本的类,最终抛出冲突报错。
问题根源
你引入的jp.wasabeef:glide-transformations:2.0.1本身已经内置了Glide的依赖,而你又手动添加了com.github.bumptech.glide:glide:3.7.0,这就造成了同一个类被重复引入,触发编译冲突。
解决方案
1. 排除冲突的依赖
修改你项目build.gradle中的依赖配置,给glide-transformations添加排除规则,去掉它内置的Glide依赖,统一使用你指定的3.7.0版本:
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' // 保留你指定的Glide版本 compile 'com.github.bumptech.glide:glide:3.7.0' // 排除glide-transformations内置的Glide依赖 compile('jp.wasabeef:glide-transformations:2.0.1') { exclude group: 'com.github.bumptech.glide', module: 'glide' } compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' }
2. 验证依赖树(可选但推荐)
执行Gradle命令查看项目依赖树,确认没有重复的Glide依赖:
- macOS/Linux:终端运行
./gradlew app:dependencies - Windows:命令行运行
gradlew app:dependencies
在输出中搜索com.github.bumptech.glide:glide,确保只有3.7.0这一个版本被引入。
额外提醒
你的MainActivity.java中注释掉了setContentView(R.layout.activity_main)和控件初始化代码,这会导致后续调用Glide.with(...).into(blurImageView)时触发NullPointerException,记得取消这些注释,保证控件能正常初始化:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); // 取消注释 blurImageView = (ImageView) findViewById(R.id.iv_blur); // 取消注释 avatarImageView = (ImageView) findViewById(R.id.iv_avatar); // 取消注释 Glide.with(this).load(R.drawable.head) .bitmapTransform(new BlurTransformation(this, 25), new CenterCrop(this)) .into(blurImageView); Glide.with(this).load(R.drawable.head) .bitmapTransform(new CropCircleTransformation(this)) .into(avatarImageView); }
内容的提问来源于stack exchange,提问作者tts zhang
相关产品推荐
相关产品推荐

