首页 > 文章列表 > Java高版本Api在Android中如何使用

Java高版本Api在Android中如何使用

java API android
468 2023-04-28

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类似-但是他更强大-它对每个元素执行操作并返回一个新的 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'
    
    }