Java高版本Api在Android中的使用方法详解

网友投稿 530 2022-07-29


目录android插件开启对新Api的支持常用的需要兼容处理的类:1. LocalDate日期处理2. Stream集合流操作AGP7编译的问题总结

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.VEJhGPwcRQHPRSION_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 widgets = new ArrayList<>();

widgets.add(new widget(Color.JhGPwcRQHPRED, "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 和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 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编译的问题

之前的项目编译的时候,由于我们的兼容代码是写在子模块JhGPwcRQHP的build.gradle的app模块编译之后会merge成功,运行也没有问题。但是前段时间项目JhGPwcRQHP升级到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'

}

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot项目引入第三方sdk jar包的解决方案
下一篇:SpringBoot超详细讲解集成Flink的部署与打包方法(springboot整合flink)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~