Share
Embed
Now in Android アプリ解説
I/O Extended Japan 2022
https://gdgjapan.connpass.com/event/247417/
Yuki Anzai
June 05, 2022
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
55 slides
-
pluginManagement { includeBuild("build-logic") repositories { google() mavenCentral() gradlePluginPortal() } }
… QMVHJOCVJMEͷ SPPUQSPKFDUEJSFDUPSZ ಠࣗQMVHJOͷQSPKFDU -
plugins { `kotlin-dsl` } group = "com.google.samples.apps.nowinandroid.buildlogic" java { sourceCompatibility
= JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } dependencies { implementation(libs.android.gradlePlugin) implementation(libs.kotlin.gradlePlugin) implementation(libs.spotless.gradlePlugin) } gradlePlugin { plugins { … } } ʁ implementation("com.android.tools.build:gradle:7.2.1") ͷΑ͏ͳจࣈྻࢦఆͰͳ͍ MJCTBESPJEHSBEMF1MVHJOͲ͜Λࢦ͍ͯ͠Δʁ -
ϥΠϒϥϦཧ w HSBEMFͷ7FSTJPO$BUBMPH w IUUQTEPDTHSBEMFPSHDVSSFOUVTFSHVJEFQMBUGPSNTIUNM dependencyResolutionManagement { … versionCatalogs {
create("libs") { from(files("../gradle/libs.versions.toml")) } } } … -
[versions] accompanist = "0.24.8-beta" androidDesugarJdkLibs = "1.1.5" androidGradlePlugin = "7.2.1"
androidxActivity = "1.4.0" androidxAppCompat = "1.3.0" androidxCompose = "1.2.0-beta02" … [libraries] accompanist-flowlayout = { group = "com.google.accompanist", name = "accompanist-flowlayout", version.ref = "accompanist" } android-desugarJdkLibs = { group = "com.android.tools", name = "desugar_jdk_libs", version.ref = "androidDesugarJdkLibs" } android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" } androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidxAppCompat" } … [plugins] protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } -
[versions] accompanist = "0.24.8-beta" androidDesugarJdkLibs = "1.1.5" androidGradlePlugin = "7.2.1"
androidxActivity = "1.4.0" androidxAppCompat = "1.3.0" androidxCompose = "1.2.0-beta02" … [libraries] accompanist-flowlayout = { group = "com.google.accompanist", name = "accompanist-flowlayout", version.ref = "accompanist" } android-desugarJdkLibs = { group = "com.android.tools", name = "desugar_jdk_libs", version.ref = "androidDesugarJdkLibs" } android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" } androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidxAppCompat" } … [plugins] protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } -
… dependencies { implementation(libs.android.gradlePlugin) … } dependencyResolutionManagement { … versionCatalogs
{ create("libs") { from(files("...")) } } } … … [libraries] … android-gradlePlugin = { … } … -
CVJMEMPHJDͷDVTUPNHSBEMFQMVHJOԿΛ͍ͯ͠Δͷ͔ʁ plugins { id("nowinandroid.android.application") id("nowinandroid.android.application.compose") id("nowinandroid.android.application.jacoco") kotlin("kapt") id("jacoco") id("dagger.hilt.android.plugin") id("nowinandroid.spotless")
} android { … } dependencies { … } ʁ OPXJOBOESPJE99ͷJEΛࢦఆ͍ͯͯ͠ id("com.android.application") Ͱͳ͍ -
… gradlePlugin { plugins { register("androidApplicationCompose") { id = "nowinandroid.android.application.compose"
implementationClass = "AndroidApplicationComposeConventionPlugin" } register("androidApplication") { id = "nowinandroid.android.application" implementationClass = "AndroidApplicationConventionPlugin" } … } } -
class AndroidApplicationConventionPlugin : Plugin<Project> { override fun apply(target: Project) {
with(target) { with(pluginManager) { apply("com.android.application") apply("org.jetbrains.kotlin.android") } extensions.configure<BaseAppModuleExtension> { configureKotlinAndroid(this) defaultConfig.targetSdk = 32 } } } } ͜͜Ͱࢦఆ͞Ε͍ͯΔ BOESPJECMPDLͰͷઃఆͷڞ௨Խ -
internal fun Project.configureKotlinAndroid( commonExtension: CommonExtension<*, *, *, *>, ) {
commonExtension.apply { compileSdk = 32 defaultConfig { minSdk = 21 } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 isCoreLibraryDesugaringEnabled = true } kotlinOptions { … } } … } -
BQQOJBDBUBMPH app-nia-catalog core-ui core-model data class Author( val id: String,
val name: String, val imageUrl: String, val twitter: String, val mediumPage: String, val bio: String, ) -
BQQOJBDBUBMPH app-nia-catalog core-ui core-model @Composable fun NiaFilledButton( onClick: () ->
Unit, modifier: Modifier = Modifier, enabled: Boolean = true, small: Boolean = false, … content: @Composable RowScope.() -> Unit ) { Button( … ) } -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ w *0༻ͷ$PSPVUJOF%JTQBUDIFS w TFBMFEJOUFSGBDF3FTVMUPVU5 -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ 1SPUPEBUBTUPSFͰGPMMPXͨ͠ 5PQJDͱ"VUIPSͷJEΛอଘ -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ 3FUPSpUΛͬͯωοτϫʔΫ͔Β σʔλΛऔಘ -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ SPPNʹ5PQJD"VUIPSͳͲͷ σʔλΛอଘ -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ 5PQJD "VUIPS /FXT ͷ3FQPTJUPSZ࣮ -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ 8PSL.BOBHFSΛͬͯΞϓϦ ىಈ࣌ʹσʔλΛಉظ -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ /BWJHBUJPOͷભҠઌΛදݱ͢Δ JOUFSGBDF -
App core-ui core-navigation feature-author sync feature-interests feature-foryou feature-topic core-model core-common
core-data core- datastore core- database core- network ˎUFTU༻ͷNPEVMF আ͍͍ͯ·͢ -
5PQJD/BWJHBUJPOLU object TopicDestination : NiaNavigationDestination { override val route =
"topic_route" override val destination = "topic_destination" const val topicIdArg = "topicId" } fun NavGraphBuilder.topicGraph( onBackClick: () -> Unit ) { composable( route = "${TopicDestination.route}/{${TopicDestination.topicIdArg}}", arguments = listOf( navArgument(TopicDestination.topicIdArg) { type = NavType.StringType } ) ) { TopicRoute(onBackClick = onBackClick) } } /BW(SBQIʹ͜ͷGFBUVSFͷը໘Λ ભҠઌͱͯ͠Ճ͢ΔͨΊͷ֦ுؔ -
@Composable fun NiaNavHost( … ) { NavHost( navController = navController,
startDestination = startDestination, modifier = modifier, ) { … interestsGraph( …, nestedGraphs = { topicGraph(onBackClick = { navController.popBackStack() }) authorGraph(onBackClick = { navController.popBackStack() }) } ) } } -
.BUFSJBMXJUI+FUQBDL$PNQPTF w EZOBNJDDPMPSTDIFNFʢ"OESPJEҎ߱ʣରԠ @Composable fun NiaTheme( darkTheme: Boolean = isSystemInDarkTheme(),
dynamicColor: Boolean = false, androidTheme: Boolean = false, content: @Composable() () -> Unit ) { val colorScheme = when { dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { val context = LocalContext.current if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(co } androidTheme && darkTheme -> DarkAndroidColorScheme androidTheme -> LightAndroidColorScheme darkTheme -> DarkDefaultColorScheme else -> LightDefaultColorScheme } -
*DPOΛ·ͱΊͨPCKFDU object NiaIcons { val AccountCircle = Icons.Outlined.AccountCircle val Add
= Icons.Rounded.Add val ArrowBack = Icons.Rounded.ArrowBack val ArrowDropDown = Icons.Rounded.ArrowDropDown val ArrowDropUp = Icons.Rounded.ArrowDropUp val Bookmark = R.drawable.ic_bookmark … } Icon( painter = painterResource(id = NiaIcons.Bookmark), contentDescription = null ) Icon(imageVector = NiaIcons.Add, contentDescription = null) -
8JOEPX*OTFUTͷѻ͍ w 8JOEPX*OTFUTTBGF%SBXJOHΛओʹ༻ NiaNavRail( …, modifier = Modifier.safeDrawingPadding() ) NavigationBar(
modifier = Modifier.windowInsetsPadding( WindowInsets.safeDrawing.only( WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom ) ), … ) { -
8JOEPX4J[FDMBTT w ը໘෯Ͱ#PUUPN#BSͱ/BW3BJMΛग़͚͠ Scaffold( … bottomBar = { if (windowSizeClass.widthSizeClass
== WindowWidthSizeClass.Compact) { NiaBottomBar( … ) } } ) { padding -> Row(…) { if (windowSizeClass.widthSizeClass != WindowWidthSizeClass.Compact) { NiaNavRail( … ) } … } BOESPJEYDPNQPTFNBUFSJBMNBUFSJBMXJOEPXTJ[FDMBTTBMQIB -
8JOEPX4J[FDMBTT w ը໘෯Ͱ#PUUPN#BSͱ/BW3BJMΛग़͚͠ w هࣄҰཡͷ$PMVNO val numberOfColumns = when (windowSizeClass.widthSizeClass)
{ WindowWidthSizeClass.Compact, WindowWidthSizeClass.Medium -> 1 else -> floor(maxWidth / 300.dp).toInt().coerceAtLeast(1) } BOESPJEYDPNQPTFNBUFSJBMNBUFSJBMXJOEPXTJ[FDMBTTBMQIB -
/BWJHBUJPO 4BWFE4UBUF)BOEMF composable( route = "${TopicDestination.route}/{${TopicDestination.topicIdArg}}", arguments = listOf( navArgument(TopicDestination.topicIdArg)
{ type = NavType.StringType } ) ) { TopicRoute(onBackClick = onBackClick) } @Composable fun TopicRoute( onBackClick: () -> Unit, modifier: Modifier = Modifier, viewModel: TopicViewModel = hiltViewModel(), ) { … 5PQJD%FTUJOBUJPOUPQJD*E"SH ͱ͍͏໊લͰ 4USJOH ͷBSHVNFOU /BW#BDL4UBDL&OUSZͷBSHVNFOUT͕ "CTUSBDU4BWFE4UBUF7JFX.PEFM'BDUPSZ ͷEFGBVMU"SHTʹ͞ΕΔ -
/BWJHBUJPO 4BWFE4UBUF)BOEMF @HiltViewModel class TopicViewModel @Inject constructor( savedStateHandle: SavedStateHandle, …
) : ViewModel() { private val topicId: String = checkNotNull(savedStateHandle[TopicDestination.topicIdArg]) … } 5PQJD%FTUJOBUJPOUPQJD*E"SH ͱ͍͏໊લͰ 4USJOH ͷBSHVNFOU ͕4BWFE4UBUF)BOEMFʹೖ͍ͬͯΔ -
CZTBWFE4UBUF)BOEMFTBWFBCMF\^ TextField( value = viewModel.text, onValueChange = { viewModel.text =
it } ) @HiltViewModel class SampleViewModel @Inject constructor( savedStateHandle: SavedStateHandle ) : ViewModel() { var text by mutableStateOf("") } 1SPDFTTLJMM࣌ʹ5FYU'JFMEͷ ೖྗจࣈΛࣦͬͯ͠·͏ @HiltViewModel class SampleViewModel @Inject constructor( savedStateHandle: SavedStateHandle ) : ViewModel() { var text by savedStateHandle.saveable { mutableStateOf("") } } 1SPDFTTLJMM࣌ೖྗจࣈ ͕อ࣋͞ΕΔ 😫 👍