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

Gradle同步失败:无法找到com.amitshekhar.android:debug-db:1.0.6依赖

解决Gradle同步失败问题

问题根源分析

  1. buildscript块配置位置错误:你将buildscript块嵌套在android块内部,这违反了Gradle配置规范,会导致构建脚本依赖的仓库配置无法正常生效。
  2. 模块级依赖仓库缺失:应用依赖(如debug-db)需要的仓库未在模块层级配置——buildscript中的仓库仅作用于构建脚本的classpath依赖,不覆盖应用级依赖的查找范围。
  3. 未定义变量:testImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion"中的mockitoKotlinVersion未声明,会引发额外同步错误。

修复步骤

1. 调整buildscript块位置

将buildscript块移到模块级build.gradle最外层,与plugins、android块同级,并补充未定义的变量:

buildscript {
    ext {
        agp_version = '8.1.1'
        mockitoKotlinVersion = "4.1.0" // 补充未定义的Mockito Kotlin版本变量
    }
    repositories {
        maven { url 'https://jitpack.io' }
        google()
        mavenCentral()
    }
    dependencies {
        def nav_version = "2.7.1"

        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
        classpath "com.android.tools.build:gradle:$agp_version"
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10'
    }
}

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.plugin.parcelize'
    id 'com.google.devtools.ksp'
    id 'org.jetbrains.kotlin.kapt'
    id 'androidx.navigation.safeargs.kotlin'
}

2. 添加模块级依赖仓库

在plugins块之后、android块之前添加repositories块,配置应用依赖所需的仓库:

repositories {
    google()
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

3. 清理重复依赖(可选)

移除dependencies块中的重复依赖,避免冗余:

  • 重复的implementation 'com.google.android.material:material:1.9.0'
  • 重复的androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

完整修复后的配置示例

buildscript {
    ext {
        agp_version = '8.1.1'
        mockitoKotlinVersion = "4.1.0"
    }
    repositories {
        maven { url 'https://jitpack.io' }
        google()
        mavenCentral()
    }
    dependencies {
        def nav_version = "2.7.1"

        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
        classpath "com.android.tools.build:gradle:$agp_version"
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10'
    }
}

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.plugin.parcelize'
    id 'com.google.devtools.ksp'
    id 'org.jetbrains.kotlin.kapt'
    id 'androidx.navigation.safeargs.kotlin'
}

repositories {
    google()
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

android {
    namespace 'com.example.myittaroostockinventorymanger'
    compileSdk 34

    defaultConfig {
        applicationId 'com.example.myittaroostockinventorymanger'
        minSdkVersion 24
        targetSdkVersion 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    buildFeatures {
        viewBinding(true)
    }
}

dependencies {
    def room_version = "2.5.2"
    def nav_version = "2.7.1"
    def arch_core_version = "2.2.0"

    //room database
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-rxjava2:$room_version"

    //rxjava
    implementation "io.reactivex.rxjava2:rxjava:2.2.21"
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

    //navigation
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

    debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

    //testing
    testImplementation 'junit:junit:4.13.2'
    testImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion"
    testImplementation "com.google.truth:truth:1.0.1"
    testImplementation "androidx.arch.core:core-testing:$arch_core_version"

    androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"
    androidTestImplementation "com.google.truth:truth:1.0.1"
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation "androidx.arch.core:core-testing:$arch_core_version"

    implementation 'com.nambimobile.widgets:expandable-fab:1.1.1'

    implementation 'androidx.core:core-ktx:1.10.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.0"

    kapt("androidx.room:room-compiler:$room_version")
}

最终操作

完成配置修改后,点击Android Studio中的「Sync Project with Gradle Files」按钮重新同步,即可解决依赖找不到的问题。

内容的提问来源于stack exchange,提问作者Wai Yan Htun

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 02:37:03