11月1日に迫った「targetSdkVersion は 26以上」に向けての対応の目処

メール来てますよね。

Hello Google Play Developer,

This is a reminder that starting November 1, 2018, updates to apps and games on Google Play will be required to target Android Oreo (API level 26) or higher. After this date, the Play Console will prevent you from submitting new APKs with a targetSdkVersion less than 26.

Configuring your app to target a recent API level ensures that users benefit from significant security and performance improvements, while still allowing your app to run on older Android versions (down to the minSdkVersion).

Action required

Please ensure that your apps are configured to target at least Android 8.0 (API level 26) by November 1, 2018. For technical advice on how to change your app's target API level to meet these requirements, refer to the migration guide.

Affected apps

The apps included below have one or more APKs—in production or testing tracks—that aren't currently targeting API level 26 or higher. Apps are listed with the maximum version code and corresponding targetSdkVersion. If you have more than 20 apps that could be affected in your account, please check the Play Console for a full list.

動くとはいうものの、GoogleサービスAPIの仕様や各サービス/プラットフォームのポリシーの変更が頻繁なことを考えると、放置しているアプリは一掃されていくと思われます。

メールには、以下が「マイグレーションガイド」として案内されています。

Meet Google Play's target API level requirement  |  Android Developers

おおまかに、影響しそうなキーワードを拾っておきます。

API 23 (6.0) 未満

「Runtime Permission」
実行時のパーミッション リクエスト  |  Android Developers

API 24 (7.0) 未満

「Doze」
Doze と App Standby 用に最適化する  |  Android Developers

「Firebase Cloud Messaging (FCM)」
Firebase Cloud Messaging  |  Firebase

「file://」
Setting up file sharing  |  Android Developers

API 26 (8.0) 未満

「startService()」
「startForeground()」
「startForegroundService()」
「Firebase Cloud Messaging (FCM)」
「Google Play services SDK」
「JobScheduler」

バックグラウンド実行制限  |  Android Developers

「Notification」
Create and Manage Notification Channels  |  Android Developers

「ANDROID_ID」
Settings.Secure  |  Android Developers

「multiple window/display」
マルチ ウィンドウのサポート  |  Android Developers
Android 8.0 の機能と API  |  Android Developers

「Camera API」
android.hardware.camera2  |  Android Developers

まとめ

Gradle周りやライブラリ同士の依存関係などもあって簡単には終われないですよね。

保守的に古いSDKにスティックしていた開発陣はモヤモヤ一掃のチャンスと思うべし。

あと、オプトインしといたほうがいいよ、と以下リンクがありました。

http://g.co/play/monthlynews


Androidアプリのアニメーションは必要?

なんとなくスマホイライラの原因の一つではないか、と思う。

More users like to turn off animations than you think.
予想以上にアニメーションOFFは好かれている。

Is it reasonable to assume your app's users have their developer options turned off? : androiddev

そんなあなたはOFFにしましょう。

スッキリしてサクサクします。

「開発者向けオプション(デベロッパーモード)」の有効化が必要ですが、開発者でない人も便利に利用できます。

android デベロッパーモード - Google 検索

有効化したら、その中で3つのアニメーションをOFFにするだけです。

ね、スッキリするでしょう?

どうせ待つなら滑らかアニメーションがあったほうがいいか。


最小限 の Kotlin を最短で学ぶ

最小限 の Kotlin を最短で学ぶ

シンプルな記事あったので。

Learn Kotlin for Android in One Day – Mayur Rokade – Medium

1. 変数

変数は「var」。


var username: String = "some user name"

定数は「val」。Java の final と等価。


val API_DELAY_CONSTANT: Int = 100

null で初期化するには「?」。 null を利用可能にします。


var myString: String? = null

static な定数には「companion object」。


class Animal {

    companion object {
        const val DEFAULT_NUM_OF_EYES = 2
    }
}

animal.numOfEyes = Animal.DEFAULT_NUM_OF_EYES

遅れて初期化するには「lateinit」。


lateinit var user: User
lateinit var items: List<String>

2. 関数

これは、public。Int を返します。


fun getNumber(): Int {
    return 1
}

private で引数を渡す場合。


private fun getStringLength(str1: String, str2: String): Int {
    return str1.length + str2.length
}

static 関数。


class MyStringUtil {

    companion object {
        fun getOddLengthString(str: String): String? {
            if (str.length % 2 == 0) return null

            return str
        }
    }
}
var str: String = MyStringUtil.getOddLengthString("hey")

「?」を使って、null を返すことをコンパイラに伝えています。

3. ループ と when

コレクションである items に「in」で。


var items: List<String> = ArrayList<String>()

for (item in items) {
    // do something with item
}

インデックスにアクセスすることもできる。


for (index in items.indices) {
    // access items using index
    // items.get(index)
}

Java の switch は、Kotlin では「when」。


// A cool example of when statement
fun describe(obj: Any): String? {
    var res: String? = null

    when (obj) {
        1 -> { res = "One" }         // if obj == 1
        "Hello" -> res ="Greeting"  // if obj == "Hello"
        is Long -> "Long"            // if obj is of type Long
        !is String -> "Not string"   // if obj is not of type String
        else -> {
            // execute this block of code
        }
    }

    return res
}

4. Null 安全

Java で NPE を避ける場合。if で。


if (person != null && person.department != null) {
    person.department.head = managersPool.getManager()
}

Kotlin では「?」を使って、if を省くことができる。chain可能。


person?.department?.head = managersPool.getManager()

null の場合を処理したいときは「?:」を使う。


var name = person?.name ?: throw InvalidDataException("Person cannot be null.")

5. クラス

「open」で継承可能。なければ, Java の final class と同じ。


open class Animal {
    // This class is open and
    // can be inherited
}

class Dog : Animal() {  // Notice the paranthesis
    // Class dog is final and
    // can't be inherited
}

// Compiler throws error
class Labrador : Dog {

}

6. シングルトン

「object」を使う。


object Singleton {
    var name: String = "singleton object"

    fun printName() {
        println(name)
    }
}

// later in the code
Singleton.name
Singleton.printName()

7. インターフェース

インターフェースの継承は直感的に。


interface Named {
    fun bar()
}

interface Person : Named { // Person inherits Named
    fun foo()
}

class Employee() : Person {
    override fun bar() {
        // do something
    }

    override fun foo() {
        // do something
    }
}

関数に渡す場合は少し違う。


// A function that accepts and interface as a parameter
private fun setOnClickListener(listener: View.OnClickListener) {
    // do something
}

// Passing an interface to the function
setOnClickListener(object : View.OnClickListener{
    override fun onClick(view: View?) {
        // do something
    }
})

8. 型チェックとキャスト

「is」や「!is」を使う。


if (obj is String) {
    print(obj.length)
}

if (obj !is String) {
    print("Not a String")
}

「as?」を使って例外を避けてキャスト可能。失敗時には null を返す。


val x: String? = y as? String

9. 例外

Java とほぼ同じ。


throw Exception("Hi There!")

try {
    // some code
}
catch (e: SomeException) {
    // handler
}
finally {
    // optional finally block
}

まとめ

重要な最小限の部分からきちんと学んでいくこと大事。

- Kotlin で FizzBuzz
- 「Fragment を利用するか、しないか」という話