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類似-但是他更強大-它對每個元素執行操作并返回一個新的 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的一些操作符有點類型,現在項目都是Kotlin了,一般也用不到這玩意了,如果大家是Java的老項目,希望filter map集合的可以使用stream的api很方便的轉換數據。
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'
}
總結
到此這篇關于Java高版本Api在Android中使用的文章就介紹到這了,更多相關Java高版本Api使用內容請搜索html5模板網以前的文章希望大家以后多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!