[Android] 003. 新規プロジェクト作成

New Project

Template選択

Empty Compose Activity (Material3) を選択
Minumum SDK はAPI26(Android 8.0)を指定
最低APIレベルは21(Android 5.0)
本当はサポートが辛いのでAPI30(Android 11.0)にしたい…

Project Structuer 設定

基本的には最新を指定
build.gradleに直接設定でも良いが、Project Structureで設定できる項目はなるべくそれで設定する方針

Gradle

Android Gradle Plugin Version を7.3.1、 Gradle Version を7.5.1に指定

SDK/Java バージョン

Compile – Terget SDK Version を33、Source – Terget Compatibility をJava11に指定

バージョン定義

指定すべきcompose_versionをSuggesionsで確認してVariablesで1.3.0に更新
kotlin_version 1.7.20 / compiler_version 1.3.2を追加
バージョンの組み合わせの確認は以下
https://developer.android.com/jetpack/androidx/releases/compose-kotlin
https://androidx.dev/storage/compose-compiler/repository
kotlinは1.7.20を使用するので対応したcompilerはandroidx.devでは1.4.0となっていたが
developer.android.comで1.3.2が追加された
developer.android.comが更新されたタイミングでAndroid Studioで指定ができるようなので
そちらを参照
kotlin_version / compiler_versionは後ほどbuild.gradleで使用

ライブラリの更新

okボタンを押して一度Project Structureを閉じるとSuggesionsにcompose_versionの設定が反映されます
SuggesionsでappのUpdateボタンが出ているものをすべて更新

build.gradle

build.gradle(Project)

kotlin_versionを指定
指定はシングルコーテだとエラーとなります

plugins {
    id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
}

build.gradle(Module)

compiler_versionを指定

android {
    composeOptions {
        kotlinCompilerExtensionVersion "$compiler_version"
    }
}

@OptInアノテーションを有効にするため以下を追加

android {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
    }
}

テンプレートのソース修正

Theme.kt

Template自動生成の実装でViewCompat.getWindowInsetsControllerが非推奨となってます
if (!view.isInEditMode)の内容を以下に置換える
import androidx.core.view.ViewCompatは不要なので削除

if (!view.isInEditMode) {
    /* getting the current window by tapping into the Activity */
    val currentWindow = (view.context as? Activity)?.window ?: throw Exception("Not in an activity - unable to get Window reference")

    SideEffect {
        /* the default code did the same cast here - might as well use our new variable! */
        currentWindow.statusBarColor = colorScheme.primary.toArgb()
        /* accessing the insets controller to change appearance of the status bar, with 100% less deprecation warnings */
        WindowCompat.getInsetsController(currentWindow, view).isAppearanceLightStatusBars = darkTheme
    }
}

初期動作確認

ビルドしてProblemsタブに何も出ないことを確認
エミュレータで起動することも確認


Android Studio Dolphin 2021.3.1 Patch 1 built on September 30, 2022