Search code examples
javaandroidandroid-studiojava-17java-record

Using JDK17 & Record in Android Studio (Java, Android)


enter image description here

Part 1 :-

After getting this version of Android Studio, I am able to use JDK17 in my project. Everything works great, no build fails, no runtime error, etc. (like enhanced-switch is working fine) ....but this warning keeps coming during each build. Is there any way to suppress this?

enter image description here (Warning Text -> "One or more classes has file version >= 56 which is not officially supported")

These are the build.gradle files:

(1):-

buildscript {

   repositories {
       google()
       mavenCentral()
   }
   dependencies {
       classpath 'com.android.tools.build:gradle:7.1.0-rc01'
       classpath 'com.google.android.gms:strict-version-matcher-plugin:1.2.2'

       // NOTE: Do not place your application dependencies here; they belong
       // in the individual module build.gradle files
   }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

(2):-

apply plugin: 'com.android.application'
apply plugin: 'com.google.android.gms.strict-version-matcher-plugin'

android {
    compileSdkVersion 32
    buildToolsVersion '32.0.0'
    defaultConfig {
        applicationId "com.Sujal_Industries.SelfEmot"
        minSdkVersion 21
        targetSdkVersion 32
        versionCode 11
        versionName "1.3.1"
    }
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'androidx.recyclerview:recyclerview:1.3.0-alpha01'
    implementation 'androidx.core:core-splashscreen:1.0.0-beta01'
    implementation 'com.google.mlkit:image-labeling:17.0.6'
    implementation 'com.google.android.material:material:1.6.0-alpha01'
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'io.reactivex.rxjava3:rxjava:3.1.3'
}

(I tried searching for similar warnings but no luck)

Part 2 :-

Recently, I found that Android Studio is now showing the option to create a new Java Record. enter image description here

But when I create one and build the project, this error pops up: enter image description here

So.. is there any way to forcefully add that Record class in java.lang package?

(If any more information/clarification is needed on this, please let me know)

Edit: I am aware of the fact that versions above 11 aren't officially supported but isn't there a workaround?


Solution

  • As of Android 14 SDK I can attest that all of the Java 17 language features below works flawlessly in a running app, neat! Code autocompletion and inspection also works for me.

    • JEP 361: Switch Expressions
    • JEP 378: Text Blocks
    • JEP 394: Pattern Matching for instanceof
    • JEP 395: Records
    • JEP 409: Sealed Classes

    Here they write:

    Android 14 includes features and improvements that further align with the OpenJDK 17 LTS release, including both library updates and Java 17 language support for app and platform developers.

    I only get a Build warning saying that my Android Gradle plugin (8.0.2) hasn't been "tested with compileSdk = 34". If you were to update Android Studio to the Beta or Canary channels with newer Android Gradle plugins, I'm sure the warning would disappear. I'm using the current official version:

    enter image description here

    To start using Java 17 language goodies, set up the Android 14 SDK:

    enter image description here

    enter image description here

    And in your app's build.gradle (using Groovy):

    android {
        compileSdk 34
        ...
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_17
            targetCompatibility JavaVersion.VERSION_17
        }
    }