Java高版本Api在Android中的使用方法詳解
Android插件開啟對新Api的支持
這一天小王導入了一個庫,上線之后直接崩了一大片? 找到其中的問題:
什么鬼哦?安卓8.0一下無法使用? 這樣上線8.0以下的手機全部閃退了。 查一下才知道需要開啟插件啟動對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' }
一定要開啟multiDexEnabled,原理就是編譯時會單獨打一個dex包,做一些兼容的處理。
常用的需要兼容處理的類:
1. LocalDate日期處理
// 日期 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集合流操作
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類似-但是他更強大-它對每個元素執(zhí)行操作并返回一個新的 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);
和Kotlin的一些操作符有點類型,現(xiàn)在項目都是Kotlin了,一般也用不到這玩意了,如果大家是Java的老項目,希望filter map集合的可以使用stream的api很方便的轉(zhuǎn)換數(shù)據(jù)。
AGP7編譯的問題
之前的項目編譯的時候,由于我們的兼容代碼是寫在子模塊的build.gradle的app模塊編譯之后會merge成功,運行也沒有問題。但是前段時間項目升級到AGP之后,無法運行指定的api了,需要在運行模塊app的build.gradle中添加兼容代碼塊才能運行,這里特此記錄一下。
... 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需要添加
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' }
總結(jié)
到此這篇關(guān)于Java高版本Api在Android中使用的文章就介紹到這了,更多相關(guān)Java高版本Api使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis不同Mapper文件引用resultMap實例代碼
這篇文章主要介紹了mybatis 不同Mapper文件引用resultMap的實例代碼,非常不錯具有參考借鑒價值,需要的朋友可以參考下2017-07-07mybatis動態(tài)拼接實現(xiàn)有條件的插入
這篇文章主要介紹了mybatis動態(tài)拼接實現(xiàn)有條件的插入,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Java中@DS+@Transactional注解切換數(shù)據(jù)源失效解決方案
本文主要介紹了@DS+@Transactional注解切換數(shù)據(jù)源失效解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06Spring Boot實現(xiàn)通用的接口參數(shù)校驗
本文介紹基于 Spring Boot 和 JDK8 編寫一個 AOP ,結(jié)合自定義注解實現(xiàn)通用的接口參數(shù)校驗。具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05