首页 > 解决方案 > 使用 Glide 时类与同名包冲突

问题描述

我正在尝试Glide在我的应用程序上使用。Glide需要我创建一个扩展的类,Application然后在里面我需要创建一个扩展的方法AppGlideModule。然后我需要做一个Make Project我做过的事情。出于某种原因,创建了导致错误Glide的类的多个实例。Application

我所做的是Application多次重命名类,将类移到不同的文件夹中,但错误仍然相同。将Application类移动到不同的文件夹后,Android Studio检测到该类的 2 个实例。一个在文件夹外的实例和另一个在文件夹内的实例。请注意,Application该类已经在文件夹中,并且没有其他类与该类具有相同的名称。

这是我的Application课。

import android.app.Application;

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

public class Globals extends Application {

    @GlideModule
    public final class mAppGlideModule extends AppGlideModule {

    }
}

这是我的 build.gradle(app)。

android {
    compileSdkVersion 27
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
}

这是不断出现的错误。

error: class Globals clashes with package of same name

标签: javaandroid

解决方案


在 gradle 中添加这一行

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

并尝试将类的名称从更改Globals为并在清单文件Application的标记中添加类名application

<application
        android:name=".Application"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

推荐阅读