nmw01223
nmw01223

Reputation: 1723

Unable to resolve checkSelfPermission() or requestPermissions()

This is probably a very dumb question.

I have an app written using Eclipse years ago, for Android OS 8 (2.3.x). I am trying to bring it into the modern world (still built with 8, but targetting 28), and have moved it to Android Studio. I haven't touched Android code for some years, know very little indeed about Android Studio, and less still about Gradle.

I am currently trying to deal with the different permissions environment, ie checking on start up and requesting if necessary. I have the following code in the main activity (extended from Activity) - I know this is simple and could do with other logic, I am just trying to get it going in principle at the moment:

...
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
...
    onCreate(...) {
    ...
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(
                this,
                new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                ID_ACCESS_FINE_LOCATION);
    ...

android-support-v4.jar is under /libs in the project.

The build cannot resolve checkSelfPermission() or requestPermissions(), despite the imports.

I have found other questions on this, and one (self-)answer was "AS imported the supportlib as a jar and this jar was from like 2014. I just replaced the jarimport with the real dependency and now it is working."

This sounds a bit like me, but unfortunately with my limited knowledge of the build environment, I don't really understand the answer, and I am worried about screwing something big time by mistake. I'd be very grateful if someone could explain in words of one syllable what I actually need to do to resolve this, or at least point me to somewhere it is explained ....

Thanks.

POSTSCRIPT

build.gradle as requested in comment (apart from my changing the target SDK, it was created automatically on import from the Eclipse tree):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 8
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "uk.co.nightshadearts.gpscompass"
        minSdkVersion 8
        targetSdkVersion 28
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile project(':googleplayservices_lib')
    compile files('libs/android-support-v4.jar')
}

Upvotes: 0

Views: 1242

Answers (1)

Sandi
Sandi

Reputation: 2731

android-support-v4.jar is a very old and very deprecated library. You should familiarize yourself with the new Gradle-based build system. To use the latest version of ContextCompat and ActivityCompat do the following:

  • Remove android-support-v4.jar from libs/
  • Open your module's build.gradle file (the nested one, not the one in the root of your project)
  • Add implementation "androidx.core:core:1.1.0" (latest stable as of writing) or implementation "androidx.core:core:1.2.0-beta01" (latest release as of writing) to your dependencies block:
...
dependencies {
    ...
    implementation "androidx.core:core:1.1.0"
}

(taken from here)

  • Sync project, you should get a prompt for syncing when you change your build.gradle file contents
  • Reimport androidx.core.content.ContextCompat and androidx.core.app.ActivityCompat

Upvotes: 2

Related Questions