简体   繁体   中英

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

在此处输入图像描述

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?

在此处输入图像描述 (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 . 在此处输入图像描述

But when I create one and build the project, this error pops up: 在此处输入图像描述

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?

For Part One: The Warning means that you have one or more classes within your project which are not supported for Java Versions higher than 11.

There are some dependencies which your project which are not compiled for JDK 17. That's because Android currently has no support for Java 17. This means you are mixing up different Versions of Java Bitcode which produces this warning.

Java Version Numbers

For Part Two: You are trying to use a record, which is currently not supported in Android.

Summary: You have set the Java Version for your Project to Java 17, which is currently not supported:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}

The highest you can currently go is Java 11 which means you have to change the compileOptions to:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
}

The latest version of java bytecode supported by Android is Java 7

Android SDK supports desugaring for some of the later java features - https://developer.android.com/studio/write/java8-support .
These features are "syntactic sugar" - when light and short code constructions replace complex constructions to improve readability. Such features do not require significant changes in the JVM, and can be recompiled into previous versions of java bytecode

"Desugaring" means that source code from java 8 (or later) is compiled into bytecode of java 7 when it is possible

Most of Java8+ features are unsupported

If you want to write code for Android in a modern manner you have to switch to Kotlin

It is possible to use Java Record in Android. See https://issuetracker.google.com/issues/197081367

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM