Home >Java >javaTutorial >How to use Java high version API in Android
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.
// 日期 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));
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.
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!