Android plug-in opens support for new APIs
On this day, Xiao Wang imported a library, and a large piece of it crashed immediately after it went online? Find the problem:
#What the hell? Unable to use Android 8.0? In this way, all mobile phones launched below 8.0 crashed. After checking, I found out that I need to enable the plug-in to enable 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 that a separate dex package will be created during compilation to do some compatible processing.
Commonly used classes that require compatible processing:
1. LocalDate date processing
// 日期 LocalDate today = LocalDate.now(); // 几号 int dayofMonth = today.getDayOfMonth(); // 星期几 int dayofWeek = today.getDayOfWeek().getValue(); // 今年 int dayofYear = today.getDayOfYear(); LocalDate endOfFeb = LocalDate.parse("2018-02-28"); // 取本月第1天: LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 取本月第2天: LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 取本月最后一天,再也不用计算是28,29,30还是31: LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 取下一天: LocalDate firstDayOfNextMonth = lastDayOfThisMonth.plusDays(1); // 取2017年1月第一个周一: LocalDate firstMondayOf2017 = LocalDate.parse("2017-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
2. Stream collection flow operations
List<widget> 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<User> userList = Stream. of(arrayList). map(person -> new User(person.getName())). collect(Collectors.toList()); //peek 和map类似-但是他更强大-它对每个元素执行操作并返回一个新的 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)) .collect(Collectors.toList()); //limit 返回 Stream 的前面 n 个元素; //skip 则是扔掉前 n 个元素 List<String> personList2 = persons.stream() .map(Person::getName) .limit(10) .skip(3) .collect(Collectors.toList()); System.out.println(personList2);
And some operations of Kotlin There are some types of symbols. Now that projects are all Kotlin, generally there is no need for this stuff. If you are an old Java project, I hope that the filter map collection can use the stream API to easily convert data.
AGP7 compilation problem
When the previous project was compiled, since our compatibility code was written in the build.gradle of the submodule, the app module would be merged successfully after compilation, and there would be no problem running. . However, after the project was upgraded to AGP some time ago, 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. This is recorded here.
... 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' }
The above is the detailed content of How to use Java high version API in Android. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools