Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java High Edition Api in Android

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use the high version of Java Api in Android". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope that this article "how to use the high version of Java Api in Android" can help you solve the problem.

The Android plug-in enables support for the new Api

On this day, Xiao Wang imported a library and directly burst a large area after it was launched. Find the problem:

What the heck? Android 8.0 cannot be used for a while? In this way, all mobile phones with less than 8.0 online have flashed. Check to know that you need to open the plug-in to start the support for Java Api.

Android {defaultConfig {multiDexEnabled true} compileOptions {/ / Flag to enable support for the new language APIs coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8}} dependencies {coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'}

Be sure to turn on multiDexEnabled, the principle is to compile a separate dex package, do some compatible processing.

Commonly used classes that require compatible processing: 1. LocalDate date processing / / date LocalDate today = LocalDate.now (); / what number int dayofMonth = today.getDayOfMonth (); / / day of the week int dayofWeek = today.getDayOfWeek (). GetValue () / / int dayofYear = today.getDayOfYear () this year; LocalDate endOfFeb = LocalDate.parse ("2018-02-28"); / / take the first day of this month: LocalDate firstDayOfThisMonth = today.with (TemporalAdjusters.firstDayOfMonth ()) / / take the second day of the month: LocalDate secondDayOfThisMonth = today.withDayOfMonth (2); / / take the last day of the month, and no longer need to calculate whether it is 28, 29, 30 or 31: LocalDate lastDayOfThisMonth = today.with (TemporalAdjusters.lastDayOfMonth ()); / / take the next day: LocalDate firstDayOfNextMonth = lastDayOfThisMonth.plusDays (1) / / take the first Monday of January 2017: LocalDate firstMondayOf2017 = LocalDate.parse ("2017-01-01") .with (TemporalAdjusters.firstInMonth (DayOfWeek.MONDAY)); 2. Stream aggregate flow operation List widgets = new ArrayList (); widgets.add (new widget (Color.RED, "Name", 1)) Int sum = widgets.stream () .filter (w-> w.getColor () = = Color.RED) .mapToInt (w-> w.getWeight ()) .sum (); List userList = Stream. Of (arrayList). Map (person-> new User (person.getName (). Collect (Collectors.toList ()) / / peek is similar to map-but it is more powerful-it performs operations on each element and returns a new Stream Stream.of ("one", "two", "three") "four") .filter (e-> e.length () > 3) .peek (e-> System.out.println ("Filtered value:" + e)) .map (String::toUpperCase) .peek (e-> System.out.println ("Mapped value:" + e)) .filter (Collectors.toList ()) / / limit returns the first n elements of Stream / / skip is to throw away the first n elements List personList2 = persons.stream () .map (Person::getName) .limit (10) .skip (3) .throw (Collectors.toList ()); System.out.println (personList2)

And Kotlin some operators have a type, now the project is Kotlin, generally do not need this thing, if you are an old Java project, I hope that the filter map collection can use stream's api to easily convert data.

Problems with AGP7 compilation

When the previous project was compiled, because our compatible code was written in the build.gradle of the sub-module, the app module of the merge was compiled successfully, and there was no problem in running it. However, some time ago, after the project was upgraded to AGP, the specified api cannot be run. You need to add a compatible code block in the build.gradle of the running module app to run it. Here is a record.

... Repositories {maven {url 'https://maven.aliyun.com/nexus/content/groups/public/'} google () maven {url' https://jitpack.io'} mavenCentral () jcenter ()} dependencies {classpath 'com.android.tools.build:gradle:7.0.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version "classpath 'com.google.gms:google-services:4.3.8'}...

App build.gradle needs to be added

Android {defaultConfig {multiDexEnabled true} compileOptions {/ / Flag to enable support for the new language APIs coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8}} dependencies {coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'} about "how the high version of Java Api is used in Android", thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report