You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kotlin Gradle中正确集成PhotoView?(存在重复类问题)

问题:集成PhotoView时出现AndroidX与Support库重复类错误

我使用Android Studio Hedgehog(2023.1.1 Patch 1)和Kotlin Gradle脚本集成PhotoView,现有教程大多基于旧版Groovy脚本,自身技术基础薄弱,遇到了构建报错。移除PhotoView依赖后应用可正常构建,确定是该依赖导致的重复类问题。

我的配置文件如下:

build.gradle.kts(Project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id("com.android.application") version "8.2.1" apply false
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false
}

build.gradle.kts(Module :app)

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    namespace = "com.ipn.dataadventures"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.ipn.dataadventures"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    
    buildFeatures{
        viewBinding = true
    }

}

dependencies {
    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.10.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    implementation("androidx.navigation:navigation-fragment-ktx:2.7.0")
    implementation("androidx.navigation:navigation-ui-ktx:2.7.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    implementation ("com.github.chrisbanes:PhotoView:2.0.0")
}

settings.gradle.kts

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url = uri("https://jitpack.io") }
    }
}

构建时出现的错误:

> Task :app:checkDebugDuplicateClasses FAILED
Execution failed for task ':app:checkDebugDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
   > Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)
     Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)
     Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)
     Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)
     Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)
     Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)
     Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)
     Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)
     Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)
     Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.0-runtime (com.android.support:support-compat:25.3.0)

解决方案

问题根源是PhotoView 2.0.0依赖了旧版Android Support库,而项目使用的是AndroidX库,两者类名重复导致冲突。解决方法如下:

1. 升级到PhotoView的AndroidX兼容版本

PhotoView从2.1.0开始已迁移到AndroidX,替换依赖为:

implementation("com.github.chrisbanes:PhotoView:2.3.0") // 或最新稳定版

2. 排除PhotoView中的Support库依赖(若需保留2.0.0版本)

如果必须使用2.0.0版本,在依赖中排除Support相关模块:

implementation("com.github.chrisbanes:PhotoView:2.0.0") {
    exclude(group = "com.android.support")
}

3. 启用Jetifier(可选,确保所有依赖自动迁移到AndroidX)

在项目根目录的gradle.properties中添加以下配置,让Gradle自动将Support库依赖转换为AndroidX:

android.useAndroidX=true
android.enableJetifier=true

执行以上操作后,重新同步Gradle并构建项目即可解决重复类错误。

内容的提问来源于stack exchange,提问作者Jesús Santoyo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 16:57:10