我一直在努力在我的 Android 应用程序中实现 PhotoView。我的知识非常基础,虽然我经常谷歌搜索,但我无法解决这个问题,因为每个教程都使用旧的 Groovy Gradle 脚本,而我使用 Kotlin Gradle 和最新的 Android Studio Hedgehog (2023.1.1 Patch 1) )
这是我的build.gradle.kts(项目)
// 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:
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
首先我解决了“maven url”部分的编写方式的错误,然后我通过更改settings.gradle.kts修复了添加依赖项的方式
毕竟,当我尝试构建时,我收到此错误:
> 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 的实现和依赖项,则应用程序构建时不会出现错误,因此它必须与此相关,但我在这里完全迷失了
出现此错误的原因是几年前 Google 将其 Android 支持库从
android.support.*
包迁移到 androidx.*
,当它们都出现在同一版本中时,会导致问题中提供的冲突。
通常,通过将
android.enableJetifier=true
行添加到 gradle.properties
文件中即可修复。
但在这种特定情况下,没有必要,因为升级到最新的 PhotoView 版本 2.3.0 就足够了,因为它已经迁移到了
androidx.*
:
implementation("com.github.chrisbanes:PhotoView:2.3.0")