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

解决Android应用中Gemini API与Supabase的Ktor版本依赖冲突

问题:Android应用中Supabase(Ktor 3.x)与Gemini API(Ktor 2.x)的Ktor版本冲突

开发环境

  • Android Studio Meerkat | 2024.3.1
  • Kotlin 1.9.x
  • Jetpack Compose
  • Hilt(依赖注入)
  • Target SDK 35

错误详情

应用启动时会交替出现两类崩溃错误:

Supabase相关错误

java.lang.NoClassDefFoundError: Failed resolution of: Lio/ktor/client/HttpClientJvmKt;  
at io.github.jan.supabase.network.KtorSupabaseHttpClient.<init>(KtorSupabaseHttpClient.kt:55)

Gemini相关错误

java.lang.NoClassDefFoundError: Failed resolution of: Lio/ktor/client/plugins/HttpTimeout;
at io.ktor.client.engine.okhttp.OkHttpEngine.<init>(OkHttpEngine.kt:40)
at com.google.ai.client.generativeai.common.APIController.<init>

依赖配置(App模块build.gradle.kts)

// Supabase SDK(依赖Ktor 3.x)
implementation(libs.supabase.auth) 
implementation(libs.supabase.postgrest) 
implementation(libs.supabase.realtime) 
implementation(libs.supabase.storage) 
implementation(libs.supabase.functions)

// Gemini API(依赖Ktor 2.x)
implementation(libs.generativeai)

// 显式Ktor依赖
implementation(libs.ktor.client.android)

已尝试但无效的方案

  1. 排除Gemini的Ktor依赖:
implementation(libs.generativeai) {
    exclude(group = "io.ktor")
}
  1. 强制指定Ktor版本(包括混合版本尝试):
implementation("io.ktor:ktor-client-core:3.1.0")  
implementation("io.ktor:ktor-client-okhttp:3.1.0")  
// 混合版本尝试失败
implementation("io.ktor:ktor-client-plugins:2.3.7")  
  1. 调整packagingOptions:
packaging {  
    resources {  
        pickFirsts.add("META-INF/INDEX.LIST")  
        pickFirsts.add("META-INF/io.netty.versions.properties")  
        excludes.add("META-INF/*.kotlin_module")  
    }  
}  

解决方案

方案1:使用Shadow插件隔离Ktor版本(推荐)

Ktor 2.x和3.x属于大版本不兼容,无法通过强制统一版本解决问题,因此需要通过包重命名实现两个版本共存:

  1. 引入Shadow插件
    在项目根目录的build.gradle.kts中添加插件:
plugins {
    // 其他项目级插件
    id("com.github.johnrengelman.shadow") version "8.1.1" apply false
}
  1. 配置App模块的Shadow规则
    在App模块的build.gradle.kts中应用插件并设置包重命名,针对Gemini依赖的Ktor 2.x进行隔离:
plugins {
    // 现有插件(如com.android.application、kotlin-android等)
    id("com.github.johnrengelman.shadow")
}

shadowJar {
    // 将Ktor 2.x的包重命名为io.ktor2,避免与3.x冲突
    relocate("io.ktor", "io.ktor2")
    // 仅对Gemini相关依赖执行重命名操作
    dependencies {
        include(dependency(libs.generativeai))
    }
}

// 确保Debug构建依赖Shadow任务
tasks.named("assembleDebug") {
    dependsOn(tasks.named("shadowDebug"))
}

// Release构建同理
tasks.named("assembleRelease") {
    dependsOn(tasks.named("shadowRelease"))
}
  1. 调整依赖配置
  • 排除Gemini自带的Ktor依赖,显式添加Ktor 2.x完整依赖(用于重命名)
  • 保留Supabase的依赖,显式指定Ktor 3.x版本
// Gemini API配置
implementation(libs.generativeai) {
    exclude(group = "io.ktor")
}
// 添加Gemini所需的Ktor 2.x依赖
implementation("io.ktor:ktor-client-core:2.3.7")
implementation("io.ktor:ktor-client-okhttp:2.3.7")
implementation("io.ktor:ktor-client-plugins:2.3.7")
// 根据Gemini SDK需求添加其他Ktor插件,如content-negotiation
implementation("io.ktor:ktor-client-content-negotiation:2.3.7")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.7")

// Supabase配置
implementation(libs.supabase.auth) 
implementation(libs.supabase.postgrest) 
implementation(libs.supabase.realtime) 
implementation(libs.supabase.storage) 
implementation(libs.supabase.functions)
// 显式添加Ktor 3.x依赖
implementation("io.ktor:ktor-client-core:3.1.0")
implementation("io.ktor:ktor-client-android:3.1.0")

方案2:等待Gemini SDK版本更新

关注Google Generative AI Android SDK的官方更新,若后续发布支持Ktor 3.x的版本,可直接升级Gemini依赖解决冲突。

方案3:封装独立模块(复杂)

将Gemini相关功能封装到独立的Android Library模块中,该模块单独使用Ktor 2.x,主模块使用Ktor 3.x。需确保模块间的HttpClient实例完全隔离,避免跨模块的类加载冲突,实现成本较高。


内容的提问来源于stack exchange,提问作者MicioBau

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 14:39:54