Scheduled to support Reiwa on Android 11 as standard

The Android version will go up. Android 11 is about to be released. It seems that the Japanese era "Reiwa" will finally be supported by the standard API on Android 11.

Conclusion

I will state the conclusion first.

--On Android 11 (R), it seems that the Japanese era "Reiwa" is finally supported by the standard API. --Since API level 24, Android standard API has already incorporated ICU. ――But API levels 24 to 29 do not support "Reiwa". It is only "Heisei".

[Quiet] Codename or API level

By the way, the code name for the name of the candy has disappeared from "Q".

KitKat, Lollipop, Marshmallow, Nougat, Oreo, Pie. Occasionally, with a mixture of registered trademarks. The Android kids were excited about what the sweets would be, starting with a "Q", but Google said, "I've stopped naming sweets."

Android 10 is "Q". The 11 that will be released soon is "R".

Supports "Reiwa" with "R" on Android ... No way! Is that "R" the R of "Reiwa"? !! What?

And apart from this version number such as 10 or 11, and the code name, the important number for the developer is "[API level](https://developer.android.com/guide/topics/manifest/uses-sdk-" element? hl = ja) ". Android 8.0 is Oreo and has an API level of 26. However, when it comes to Android 8.1, it's also Oreo, but the API level is 27. It's complicated!

And what is the API level number assigned to "R" ...? I wrote it towards the end of this article, so stay tuned. (Sumimasen)

IBM's ICU library

http://site.icu-project.org/home

It is an abbreviation for "International Components for Unicode". Anyway, various useful APIs are recorded. Open source software.

ICU's Japanese Calendar class

The com.ibm.icu.util.JapaneseCalendar class corresponds to the Japanese era. From the first era "Taika" to the present day. I haven't verified the maniac of how to handle the Northern and Southern Dynasties.

Honnoji Incident


import java.util.Locale;

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.DateFormatSymbols;
import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.GregorianCalendar;
import com.ibm.icu.util.JapaneseCalendar;

public class Honnoujinohen {
    public static void main(String[] args) {
        JapaneseCalendar jpCalendar = new JapaneseCalendar();
        DateFormatSymbols dfs = new DateFormatSymbols(jpCalendar, Locale.JAPANESE);
        DateFormat dateFormat = new SimpleDateFormat("Gyy year MM month dd day", dfs);
        dateFormat.setCalendar(jpCalendar);

        //"Honnoji Incident" June 2, 1582
        int year = 1582;
        int month = 6;
        int day = 2;
        String s = dateFormat.format(new GregorianCalendar(year, month - 1, day).getTime());
        System.out.println(s);
    }
}

landmark_honnouji_fire.png

Execution result


June 02, 10th year of Tensho

It is unbearable for taiga drama fans and reki-jo. Kirin is coming. Was it the era that wrote "Heaven is right"? Oh, Onin War. Tenpo Reforms. Ansei Purge. You can run around the times in your brain just by thinking about the era name.

This ICU is compatible with "Reiwa" from version 64.2 released on April 17, 2019.

Android standard API

In the Android standard API, ICU was imported from API level 24 (Android 7.0 Nougat). The package has changed just because it was imported. For example, the JapaneseCalendar class above is ʻandroid.icu.util.JapaneseCalendar`.

And. finally. At the API level of Android 11 (R),

python


public static final int REIWA

Will be added! Rainy day int enum pattern! !! !! No! !! !! I don't know, IBM's ICU is an int enum pattern in the first place! !! !!

Sample app

I made an app like this.

Screenshot_1585201324.png

"Kenmu Restoration" is a new policy that Emperor Go-Daigo started on July 17, 1333 after defeating the Kamakura Shogunate, but on that day the era name is still "Genko", isn't it? I think I'm going to take the next second semester midterm exam.

nigaoe_godaigo_tennou.png

Screenshot_1585200766.png

From Heisei to Reiwa.

Screenshot_1585200774.png

The development environment is as follows.

Android Studio 3.6.1 Build #AI-192.7142.36.36.6241897, built on February 27, 2020 Runtime version: 1.8.0_212-release-1586-b04 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0 GC: ParNew, ConcurrentMarkSweep Memory: 1237M Cores: 8 Registry: ide.new.welcome.screen.force=true Non-Bundled Plugins:

Gradle settings

Must be created at the API level of Android 11 (R). Make the following in the Gradle configuration file (partially omitted).

build.gradle


android {
    compileSdkVersion 'android-R'
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.example.gengou"
        minSdkVersion 24
    }

    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

At the time of this writing, no numbers have been assigned to the Android 11 (R) API level. It seems that it is still a preview version. It is estimated that it will probably be 30 when the official version is released.

minSdkVersion must be at least 24. Because, as mentioned above, ICU was incorporated into the Android standard API from API level 24.

Activity

MainActivity.java


package com.example.gengou;

import android.icu.text.DateFormat;
import android.icu.text.DateFormatSymbols;
import android.icu.text.SimpleDateFormat;
import android.icu.util.GregorianCalendar;
import android.icu.util.JapaneseCalendar;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText year = findViewById(R.id.year);
        EditText month = findViewById(R.id.month);
        EditText day = findViewById(R.id.day);
        TextView gengo = findViewById(R.id.gengou);

        findViewById(R.id.button).setOnClickListener(v -> {
            int y = Integer.parseInt(year.getText().toString());
            int m = Integer.parseInt(month.getText().toString());
            int d = Integer.parseInt(day.getText().toString());
            JapaneseCalendar jpCalendar = new JapaneseCalendar();
            DateFormatSymbols dfs = new DateFormatSymbols(jpCalendar, Locale.JAPANESE);
            DateFormat dateFormat = new SimpleDateFormat("Gyy year MM month dd day", dfs);
            dateFormat.setCalendar(jpCalendar);
            String s = dateFormat.format(new GregorianCalendar(y, m - 1, d).getTime());
            gengo.setText(s);
        });
    }
}

Supports Reiwa with IBM's ICU instead of Android standard API

Even though the Android standard API supports Reiwa! Instead of using it, IBM's ICU responds to Reiwa, and it's a mess! You may think that, but I would like to do such a thing for the following reasons.

--The Android standard API supports Reiwa from "R". That's good. --ICU was incorporated into the Android standard API at API level 24 (Android 7.0 "N"). --If so, the minSdkVersion must be at least 24 in the Gradle settings, or the ʻandroid.icu` package cannot be used before Reiwa Unun. ――But I want to make my own app compatible with Android versions 6, 5, and 4!

So that's how it's done.

Gradle settings

build.gradle


dependencies {
    implementation 'com.ibm.icu:icu4j:64.2'
}

Add it as a dependent library and it's SyncNow. Specify 64.2 or higher that supports Reiwa.

program

The ʻimport` statement looks like this to avoid name collisions.

Pay attention to the package


import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.DateFormatSymbols;
import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.GregorianCalendar;
import com.ibm.icu.util.JapaneseCalendar;

import java.util.Locale;

Only Locale is a stable java.util package. With this, even Android 6, 5 and 4 can support Reiwa.

[Bonus] Obviously.

Rest assured that ICU does not have the era name "Kobun".

Screenshot_1585202068.png

that's all.

Recommended Posts

Scheduled to support Reiwa on Android 11 as standard
How to "hollow" View on Android
Sample to display (head-up) notifications on Android
How to detect microphone conflicts on Android
[Java] 4 steps to implement splash screen on Android
What to do if TextToSpeech doesn't work on Android 11
As of April 2018 How to get Java 8 on Mac
[Android Studio] I want to use Maven library on Android
I want to simplify the log output on Android