컴포즈에서 권한요청하는기능은 아직 공식적으로 지원하지 않고 activity 객체를 통해서 기능을 구현해야 하는 것 같습니다.
아래 사이트의 라이브러리를 사용하면 컴포즈 함수에서 바로 권한 요청을 할 수 있습니다.
https://google.github.io/accompanist/permissions/#jetpack-compose-permissions
버전이 맞지 않아서 애를 먹었습니다.
버전이 맞지 않아서 삽질했던 과정을 남깁니다.
가이드 사이트 컴포즈 권한을 추가하려면 아래와 같이 라이브러리를 추가해야한다고 가이드합니다.
repositories {
mavenCentral()
}
dependencies {
implementation "com.google.accompanist:accompanist-permissions:<version>"
}
버전은 아래사이트를 통해 찾았습니다.(버전 결정하기도 어렵네요ㅠ)
0.29.1-alpha 적용
implementation "com.google.accompanist:accompanist-permissions:0.29.1-alpha"
가장 낮은버전을 선택하였습니다. 별 문제 없이 잘 적용됩니다.
0.33.0-alpha 최신버전 적용
implementation "com.google.accompanist:accompanist-permissions:0.33.0-alpha"
오류발생
Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1.
코틀린 버전을 1.9.0으로 변경
id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
오류발생
1. Dependency 'androidx.compose.animation:animation-core-android:1.6.0-alpha02' requires libraries and applications that
depend on it to compile against version 34 or later of the
Android APIs.
compile, target sdk 변경
compileSdk 34
targetSdk 34
오류발생
e: This version (1.3.2) of the Compose Compiler requires Kotlin version 1.7.20 but you appear to be using Kotlin version 1.9.0 which is not known to be compatible. Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).
컴포즈 컴파일러 버전 변경
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
최신버전을 적용하려면 여러군데 버전 변경이 필요하였습니다.
컴포즈 샘플코드 실행
@OptIn(ExperimentalPermissionsApi::class)
@Composable
public fun Sample() {
val locationPermissionsState = rememberMultiplePermissionsState(
listOf(
android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.ACCESS_FINE_LOCATION,
)
)
if (locationPermissionsState.allPermissionsGranted) {
Text("Thanks! I can access your exact location :D")
} else {
Column {
val allPermissionsRevoked =
locationPermissionsState.permissions.size ==
locationPermissionsState.revokedPermissions.size
val textToShow = if (!allPermissionsRevoked) {
// If not all the permissions are revoked, it's because the user accepted the COARSE
// location permission, but not the FINE one.
"Yay! Thanks for letting me access your approximate location. " +
"But you know what would be great? If you allow me to know where you " +
"exactly are. Thank you!"
} else if (locationPermissionsState.shouldShowRationale) {
// Both location permissions have been denied
"Getting your exact location is important for this app. " +
"Please grant us fine location. Thank you :D"
} else {
// First time the user sees this feature or the user doesn't want to be asked again
"This feature requires location permission"
}
val buttonText = if (!allPermissionsRevoked) {
"Allow precise location"
} else {
"Request permissions"
}
Text(text = textToShow)
Spacer(modifier = Modifier.height(8.dp))
Button(onClick = { locationPermissionsState.launchMultiplePermissionRequest() }) {
Text(buttonText)
}
}
}
}