Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve "Cannot access androidx.activity.contextaware.ContextAware'"?

I'm building a simple application using LiveData and viewmodels but iam getting the following warning messages in my activity surrodning my activity some of the warning

Cannot access 'androidx.activity.contextaware.ContextAware' which is a supertype of 'com.example.movies.presentation.home.MoviesActivity'. Check your module classpath for missing or conflicting dependencies

  dependencies {
    
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$life_cycle_version"

    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$life_cycle_version"

    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    kapt "androidx.lifecycle:lifecycle-compiler:$life_cycle_version"

    // Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$life_cycle_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"

    //moshi
    implementation("com.squareup.moshi:moshi:$moshi_version")
    kapt("com.squareup.moshi:moshi-kotlin-codegen:$moshi_version")

    //retrofit
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
    implementation("com.squareup.okhttp3:logging-interceptor:$okhttp_version")

    //espresso testing
    androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_core"
    androidTestImplementation "androidx.test:runner:$test_runner"
    androidTestImplementation "androidx.test:rules:$test_runner"

    // Hilt For instrumentation tests
    androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_testing"
    androidTestAnnotationProcessor "com.google.dagger:hilt-compiler:$hilt_testing"

    //Hilt For local unit tests
    testImplementation "com.google.dagger:hilt-android-testing:$hilt_testing"
    testAnnotationProcessor "com.google.dagger:hilt-compiler:$hilt_testing"

    //mockk for testing
    testImplementation "io.mockk:mockk:$mockk_version"

    //coroutine testing
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutine_test_version"

    testImplementation("org.junit.jupiter:junit-jupiter-api:$junit_5")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junit_5")

    testImplementation "android.arch.core:core-testing:$arch_version"

my module build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.4.31"
    ext.hilt_version = '2.31.2-alpha'
    ext.junit = '1.7.1.1'


    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
        classpath "de.mannodermaus.gradle.plugins:android-junit5:$junit"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
ext {
    life_cycle_version = "2.2.0"
    espresso_core = "3.3.0"
    test_runner = "1.3.0"
    hilt_testing = "2.33-beta"
    moshi_version = "1.10.0"
    mockk_version = "1.11.0"
    retrofit_version = "2.9.0"
    okhttp_version = "4.9.0"
    coroutine_test_version = "1.4.3"
    junit_5 = "5.7.1"
    arch_version = "2.2.0"
}

enter image description here

like image 905
a3adel Avatar asked Mar 26 '21 02:03

a3adel


People also ask

What is a contextaware class in Android?

The class representing a car app activity. Base class for activities that want to use the support-based Fragments. A ContextAware class is associated with a Context sometime after the class is instantiated. By adding a OnContextAvailableListener, you can receive a callback for that event.

What is oncontextavailablelistener in Android Studio?

By adding a OnContextAvailableListener, you can receive a callback for that event. Classes implementing ContextAware are strongly recommended to also implement androidx.lifecycle.LifecycleOwner for providing a more general purpose API for listening for creation and destruction events.

How do I get a callback for a contextaware event?

A ContextAware class is associated with a Context sometime after the class is instantiated. By adding a OnContextAvailableListener, you can receive a callback for that event.

How to listen for creation and destruction events in contextaware?

Classes implementing ContextAware are strongly recommended to also implement androidx.lifecycle.LifecycleOwner for providing a more general purpose API for listening for creation and destruction events. Add a new OnContextAvailableListener for receiving a callback for when this class is associated with a android.content.Context.


Video Answer


2 Answers

It was resolved when I added the activity jetpack module to the gradle file

def activity_version = "1.2.2"

// Java language implementation
    implementation "androidx.activity:activity:$activity_version"
    // Kotlin
implementation "androidx.activity:activity-ktx:$activity_version"
like image 176
a3adel Avatar answered Oct 19 '22 16:10

a3adel


I got rid of this error message by upgrading the appCompat library to the latest version from 1.2.0 to 1.3.0-rc01.

like image 3
rajndev Avatar answered Oct 19 '22 17:10

rajndev