androidandroid-studiogradleandroid-gradle-plugin

Android Studio lint error calling getApplicationContext inside AppCompatActivity


I have just attempted to setup a new project in Android studio, with the following settings:

build.gradle

plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

settings.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        mavenLocal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        mavenLocal()
    }
}
rootProject.name = "Example"
include ':app'

app/build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdk 34
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example"
        minSdk 26
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

Using a very basic Main Activity, I am attempting to store the Application context in a variable in the main activity:

package com.example;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Set context
        Context ctx = getApplicationContext();
    }
}

In the IDE, the method cannot be resolved and the following error is given when highlighting over the red method text: Cannot resolve method 'getApplicationContext' in 'MainActivity'

Lint error in MainActivity

The app compiles and runs without any errors, so it is certainly a lint issue in the IDE itself. I have tried multiple restarts, invalidating cache and project rebuilds but nothing has worked. What is causing this?


Solution

  • This appears to be caused by the version of google android materials library used. Downgrading from 'com.google.android.material:material:1.12.0' to 'com.google.android.material:material:1.11.0' resolved the lint error.