Friday, February 17, 2023
HomeMobileAPI desugaring supporting Android 13 and java.nio

API desugaring supporting Android 13 and java.nio


The brand new model 2.0 launch is available in 3 flavors:

  • com.android.instruments:desugar_jdk_libs_nio:2.0.2 – the nio model contains all of the desugaring obtainable together with the java.nio, java.time, stream, and capabilities APIs.
  • com.android.instruments:desugar_jdk_libs:2.0.2 – the default model contains desugaring for the java.time, stream, and capabilities APIs. It’s just like model 1.x API desugaring already obtainable, however up to date with APIs added as much as Android 13.
  • com.android.instruments:desugar_jdk_libs_minimal:2.0.2 – the minimal model contains solely the java.util.perform bundle and bug fixes on concurrent collections. It’s designed for minimal code measurement overhead.

Opting into extra desugaring options will result in a bigger affect in your app’s code measurement. The minimal specification has, as its identify signifies, a minimal affect on the code measurement of the app. The nio specification has essentially the most affect.

The brand new java.nio APIs

The brand new java.nio APIs supported in API desugaring embrace:

  • All of the courses and APIs in java.nio.file akin to BasicFileAttributes, file manipulation, or utilization of java.nio.file.Path.
  • Some extensions of java.nio.channels, such because the FileChannel#open strategies.
  • A couple of utility strategies akin to File#toPath.

The next code snippet illustrates how one can now use the brand new java.nio APIs on all gadgets, together with gadgets operating Android 7 and decrease, by the strategies of kotlin.io.path which rely on java.nio.file.Information. A temp file will be created, written into, learn, and its fundamental attributes and its existence will be queried utilizing the brand new java.nio APIs.

import android.util.Log
import java.nio.file.StandardOpenOption.APPEND
import kotlin.io.path.createTempDirectory
import kotlin.io.path.deleteIfExists
import kotlin.io.path.exists
import kotlin.io.path.fileSize
import kotlin.io.path.readLines
import kotlin.io.path.writeLines

...
val TAG = "java.nio Take a look at"
val tempDirectory = createTempDirectory("tempFile")
val tempFile = tempDirectory.resolve("tempFile")
tempFile.writeLines(listOf("first"))
tempFile.writeLines(listOf("second"), choices = arrayOf(APPEND))
Log.d(TAG,"Content material: ${tempFile.readLines()}")
Log.d(TAG,"Measurement: ${tempFile.fileSize()}")
Log.d(TAG,"Exists (earlier than deletion): ${tempFile.exists()}")
tempFile.deleteIfExists()
Log.d(TAG,"Exists (after deletion): ${tempFile.exists()}")

// Ensuing logcat output.
Content material: first second
Measurement: 13
Exists (earlier than deletion): true
Exists (after deletion): false

A couple of options nonetheless can’t be emulated for gadgets operating Android 7 and decrease and as an alternative throw an occasion of UnsupportedOperationException or return null. They nonetheless work on gadgets operating Android 8 or greater, so current code guarded by an API stage examine ought to work because it used to. See the full checklist of APIs obtainable and the identified limitations.

The code has been extensively examined, however we’re in search of extra inputs from app builders.

Please check out the brand new model of API desugaring, and tell us the way it labored for you!

For added background see the submit Help for newer Java language APIs from when API desugaring was launched.

Java and OpenJDK are emblems or registered emblems of Oracle and/or its associates.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments