Search code examples
javaandroidandroid-support-libraryandroid-appcompatandroidx

Cannot resolve symbol 'ActivityCompat' and 'content'


I am new to android and was learning how to implement Location Based Services, and i encounter the following errors -

Cannot resolve symbol 'ActivityCompat' in line 'import android.support.v4.app.ActivityCompat;'

Cannot resolve symbol 'content' in the line 'import android.support.v4.content.ContextCompat;'

After doing some research I found out you need to add the following to the gradle.build file compile 'com.android.support:support-v4:23.0.0'

So I did that, But I still get the same errors. I tried clean still nothing.

How do i fix the errors?

This is the link from where i am trying to follow

https://www.tutorialspoint.com/android/android_location_based_services.htm#

This is my java file -

package com.example.location;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.content.pm.PackageManager;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat; # error
import android.support.v4.app.ActivityCompat;    #error
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;

// Remaining code

This is my gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.location"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:support-compat:+'
    implementation 'com.android.support:appcompat-v7:+'

    implementation 'com.android.support:support-v4:26.1.0'
}
'''

Solution

  • You have to use androidx classes:

    Add the dependency:

    implementation "androidx.core:core-ktx:+"
    

    and change your import:

    import android.support.v4.app.ActivityCompat -> import androidx.core.app.ActivityCompat
    import android.support.v4.content.ContextCompat -> import androidx.core.content.ContextCompat
    

    Also remove in your build.gradle the support libraries dependencies:

    //implementation 'com.android.support:support-compat:+'
    //implementation 'com.android.support:appcompat-v7:+'
    //implementation 'com.android.support:support-v4:26.1.0'