androidkotlingradleandroid-photoview

How to properly implement PhotoView in a Kotlin Gradle? (Duplicate Class found)


I've been having a hard time trying to implement PhotoView in my android app. My knowledge is very basic, although I google a lot, but I can't fix this since every tutorial out there is using the old Groovy Gradle scripts, while Im using Kotlin Gradle and the most recent Android Studio Hedgehog (2023.1.1 Patch 1)

This is my 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
}

And this is my 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")
}

And finally my settings.gradle:

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

First I solved an error with the way the "maven url" part was written, then i fixed the way to add dependencies by changing the settings.gradle.kts

And after all that, when I try to build, i get this error:

> 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)

If i remove the implementation and dependency of PhotoView from the files, the app builds without errors, so it has to be related to this, but im completely lost here


Solution

  • The reason for this error is that several years ago Google migrated its Android support libraries from android.support.* packages to androidx.*, and when they both appear in the same build, it leads to conflicts as provided in the question.

    Generally, it gets fixed by adding the android.enableJetifier=true line to the gradle.properties file.

    But in this specific case, it's not necessary, since upgrading to the latest PhotoView version, 2.3.0, is sufficient as it's already migrated to the androidx.*:

    implementation("com.github.chrisbanes:PhotoView:2.3.0")