Faiz
Faiz

Reputation: 6980

How to resolve "source value 8 is obsolete" warning in Android Studio?

I'm working on an Android project in Android Studio and I'm getting the following warnings:

warning: [options] source value 8 is obsolete and will be removed in a future release
warning: [options] target value 8 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.

I understand that these warnings are related to the Java version being used, but I'm not sure how to update the source and target values to fix this issue.

How to update the Java source and target compatibility in my project? Or is there another way to suppress these warnings?

Upvotes: 2

Views: 4539

Answers (3)

Hasan Gunduz
Hasan Gunduz

Reputation: 21

Update build.gradle File: Add or update the compileOptions and kotlinOptions sections like :android { compileOptions { sourceCompatibility JavaVersion.VERSION_xx targetCompatibility JavaVersion.VERSION_xx } kotlinOptions { jvmTarget = "xx" } }

Finally; Clean and Rebuild the Project,

File > Invalidate Caches / Restart > Invalidate and Restart. Build > Clean Project > Rebuild Project.

Upvotes: 1

Sayed Mohammad Sadat
Sayed Mohammad Sadat

Reputation: 314

I recently faced the exact issue while working on my Flutter project, and after two days of troubleshooting, I found a solution that worked for me. The problem stems from an incompatibility between the Java Development Kit (JDK) version and the Gradle configuration. Here’s how I resolved it:

  1. Downgrade the JDK to Version 17

Flutter projects currently work best with JDK 17 for the latest Gradle and Android plugin versions. You can download JDK 17 from the official OpenJDK website. JDK 17 - Setup file

  1. Update app/build.gradle

Ensure that the compileOptions and kotlinOptions target JDK 17:

android {
    ndkVersion "25.1.8937393"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
}
  1. Update gradle/wrapper/gradle-wrapper.properties

Set the distributionUrl to a compatible Gradle version, such as 8.10.2:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-all.zip
  1. Update settings.gradle

Ensure the plugin versions are compatible with JDK 17 and Gradle 8.3.2:

id "com.android.application" version "8.3.2" apply false
id "org.jetbrains.kotlin.android" version "2.0.20" apply false

I struggled with these warnings for two days before discovering the solution. It wasn’t immediately clear that the issue lay in the mismatch between Java, Gradle, and plugin versions. By systematically adjusting configurations and testing, I was able to eliminate the warnings and build my project successfully.

If you’re facing similar issues, I hope this solution saves you time and frustration! Let me know if you encounter any challenges implementing these changes.

Upvotes: 9

Basil Bourque
Basil Bourque

Reputation: 338136

Read The Fine Manual.

In build.gradle put:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

If using build.gradle.kts, put:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

Doing either supplies defaults for sourceCompatibility and targetCompatibility.

Upvotes: 1

Related Questions