Search code examples
kotlinandroid-jetpack-composeandroid-permissions

Why is "ACCESS_COARSE_LOCATION" and " ACCESS_FINE_LOCATION" giving me errors?


I'm trying to get a better understanding of permissions by reading documentation, working with AI and building the most basic request permission app so I can analyze it better, but I get an error of unresolved reference for "ACCESS_COARSE_LOCATION" and " ACCESS_FINE_LOCATION" everywhere in my code and I don't understand it. I have declared them in my manifest so there shouldn't be an error. Here is the code:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
        PermissionsTheme {
        Surface( modifier = Modifier.fillMaxSize()
        ){
          val context = LocalContext.current
          val requestPermissionLauncher = rememberLauncherForActivityResult(
             contract = ActivityResultContracts.RequestMultiplePermissions() ,
             onResult = {permissions ->
                if(permissions[Manifest.permission.ACCESS_COARSE_LOCATION] == true
                   && permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true){
                   ...
                }else{
                   val rationaleRequired = ActivityCompat.shouldShowRequestPermissionRationale(
                       context as MainActivity,
                       Manifest.permission.ACCESS_FINE_LOCATION
                       ) || ActivityCompat.shouldShowRequestPermissionRationale(
                          context as MainActivity,
                          Manifest.permission.ACCESS_COARSE_LOCATION
                          )
                       if(rationaleRequired){...}else{...}}})
                Column() {
                Button(onClick = {
                    requestPermissionLauncher.launch( arrayOf(
                         Manifest.permission.ACCESS_FINE_LOCATION,
                         Manifest.permission.ACCESS_COARSE_LOCATION ))}) {Text(text = "Get Location")}}}}}}}

here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application




Solution

  • I get an error of unresolved reference for "ACCESS_COARSE_LOCATION" and " ACCESS_FINE_LOCATION" everywhere in my code

    This would indicate that you're missing the import for the constants: import android.Manifest