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

如何解决Android Studio中‘Java source root points to a non-existent location’错误

Fixing "Java source root points to a non-existent location" Error in Jetpack Compose Codelab Project

I cloned the Jetpack Compose navigation Codelab from Android Developers and encountered the Java source root points to a non-existent location error immediately after running the project, without making any code changes. Below are the build.gradle files for my project and app modules. I'm looking for help identifying the cause and solution.

Build.Gradle (Project Module)

buildscript { // Define versions in a single place
    ext {
        // Sdk and tools
        compileSdkVersion = 32
        minSdkVersion = 21
        targetSdkVersion = 32

        // App dependencies
        appCompatVersion = '1.4.2'
        activityComposeVersion = '1.6.0-alpha03'
        composeVersion = '1.2.0'
        // composeCompilerVersion = "1.2.0"
        composeCompilerVersion = "1.3.0-rc01"
        coreTestingVersion = '2.1.0'
        espressoVersion = '3.4.0'
        gradleVersion = '7.2.1'
        kotlinVersion = '1.7.10'
        // kotlinVersion = '1.7.0'
        ktlintVersion = '0.45.2'
        ktxVersion = '1.8.0'
        materialVersion = '1.6.1'
        navigationComposeVersion = '2.5.0'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradleVersion"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

plugins {
    id 'com.diffplug.spotless' version '6.7.0'
}

subprojects {
    repositories {
        google()
        mavenCentral()
    }
    apply plugin: 'com.diffplug.spotless'
    spotless {
        kotlin {
            target '**/*.kt'
            targetExclude("$buildDir/**/*.kt")
            targetExclude('bin/**/*.kt')
            ktlint(rootProject.ktlintVersion)
            licenseHeaderFile rootProject.file('spotless/copyright.kt')
        }
    }
}

Build.gradle (App module)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 32

    defaultConfig {
        applicationId "com.example.compose.rally"
        minSdkVersion 21
        targetSdkVersion 32
        versionCode 1
        versionName '1.0'

        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
        vectorDrawables.useSupportLibrary = true
    }

    signingConfigs {
        // We use a bundled debug keystore, to allow debug builds from CI to be upgradable
        debug {
            storeFile rootProject.file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            minifyEnabled true
            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 {
        compose true
    }

    composeOptions {
        kotlinCompilerExtensionVersion rootProject.composeCompilerVersion
    }

    packagingOptions {
        exclude "META-INF/licenses/**"
        exclude "META-INF/AL2.0"
        exclude "META-INF/LGPL2.1"
    }
}

dependencies {
    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "androidx.core:core-ktx:$rootProject.ktxVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"

    // Compose
    implementation "androidx.compose.runtime:runtime:$rootProject.composeVersion"
    implementation "androidx.compose.ui:ui:$rootProject.composeVersion"
    implementation "androidx.compose.foundation:foundation:$rootProject.composeVersion"
    implementation "androidx.compose.material:material:$rootProject.composeVersion"
    implementation "androidx.compose.material:material-icons-extended:$rootProject.composeVersion"
    implementation "androidx.activity:activity-compose:$rootProject.activityComposeVersion"
    implementation "androidx.navigation:navigation-compose:$rootProject.navigationComposeVersion"
    debugImplementation "androidx.compose.ui:ui-tooling:$rootProject.composeVersion"

    // Testing dependencies
    androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$rootProject.espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoVersion"

    // Compose testing dependencies
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$rootProject.composeVersion"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$rootProject.composeVersion"
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
    kotlinOptions {
        // Treat all Kotlin warnings as errors
        allWarningsAsErrors = true
        freeCompilerArgs += '-opt-in=kotlin.RequiresOptIn'
        // Set JVM target to 1.8
        jvmTarget = "1.8"
    }
}

Possible Causes & Solutions

This error typically pops up when Android Studio can't properly recognize your project's source directories, usually due to sync glitches, cached data, or version mismatches. Let's go through fixes in order of simplicity:

1. Resync Gradle & Clear IDE Cache

Start with the most straightforward fix—this resolves most post-clone recognition issues:

  • Click the Sync Project with Gradle Files button (elephant icon) in the top-right corner of Android Studio.
  • If that doesn't work, head to File > Invalidate Caches... > Invalidate and Restart to wipe IDE caches and reboot. This often fixes weird directory mapping bugs.

2. Verify Source Directory Structure

Double-check your project's folder structure:

  • Since this is a Compose project, it should use src/main/kotlin as the primary source root (not Java). Confirm this directory exists in your app module.
  • If the directory is missing, try re-cloning the Codelab project—your initial clone might have been incomplete.

3. Fix Kotlin & Compose Compiler Version Compatibility

Looking at your Gradle config, there's a likely version mismatch here:

  • You're using Kotlin 1.7.10 with Compose Compiler 1.3.0-rc01. These versions aren't fully compatible—Compose Compiler 1.3.x requires Kotlin 1.7.20 or newer.
  • To fix this, update your project-level build.gradle:
    • Change kotlinVersion to 1.7.20
    • Either keep composeCompilerVersion as 1.3.0-rc01, or downgrade it to 1.2.0 to match your Compose runtime version 1.2.0
  • Resync the project after making these changes.

4. Ensure Android Studio Version Supports Your Dependencies

Make sure your IDE is up to date for the tools you're using:

  • Compose 1.2.0 and Kotlin 1.7.x require Android Studio Arctic Fox (2020.3.1) or newer. For the best experience, use Android Studio Electric Eel (2022.1.1) or later.
  • Update your IDE via Help > Check for Updates if needed.

5. Check for Custom Source Set Configurations

Your current app-level build.gradle doesn't have custom sourceSets defined, which is good. If you had added any that override default source directories, that could cause this error—but your config looks clean in this regard.

Try these steps in order, starting with the simplest first. Let me know if you need more details!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 17:37:34