search
HomeJavajavaTutorialHow to use Java high version API in Android

    Android plug-in opens support for new APIs

    On this day, Xiao Wang imported a library, and a large piece of it crashed immediately after it went online? Find the problem:

    How to use Java high version API in Android

    #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.

    Commonly used classes that require compatible processing:

    1. LocalDate date 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));

    2. Stream collection flow operations

      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.

    AGP7 compilation problem

    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 &#39;https://maven.aliyun.com/nexus/content/groups/public/&#39; }
            google()
            maven { url &#39;https://jitpack.io&#39; }
            mavenCentral()
            jcenter()
        }
    
        dependencies {
            classpath &#39;com.android.tools.build:gradle:7.0.3&#39;
    
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    
            classpath &#39;com.google.gms:google-services:4.3.8&#39;
        }
       ...

    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 &#39;com.android.tools:desugar_jdk_libs:1.1.5&#39;
    }

    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!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

    Start Spring using IntelliJIDEAUltimate version...

    How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

    When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

    How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

    How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

    How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

    Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

    How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

    Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

    E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

    Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

    How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

    How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools