Moritz
Moritz

Reputation: 51

Android studio: cannot access MenuHost class file for androidx.core.view.MenuHost not found

I've got an error while building my project. This is my error message: cannot access MenuHost class file for androidx.core.view.MenuHost not found.

I didn't find any solution for this problem. I would be very happy if you could help me. I've got this error for an empty Activity:

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class CallActivity extends AppCompatActivity {
    /* Access modifiers changed, original: protected */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.activity_call);
    }
}

This is my build.gradle (app):

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"


    defaultConfig {
        applicationId "com.example.ghostcontact"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        // Enabling multidex support.
        multiDexEnabled true


        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"


    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

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

    configurations.all {

        resolutionStrategy {
            force 'androidx.core:core:1.6.0'
            force 'androidx.core:core-ktx:1.6.0'
        }
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'


    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'


    implementation 'com.android.support.constraint:constraint-layout:2.0.4'
    implementation 'com.google.firebase:firebase-database:20.0.2'
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    implementation 'com.google.firebase:firebase-storage:20.0.0'

    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.cardview:cardview:1.0.0'

    implementation 'com.google.android.material:material:1.4.0'
    implementation 'com.hbb20:ccp:2.3.1'

    implementation 'com.google.android.gms:play-services-base:17.6.0'
    implementation 'com.google.firebase:firebase-core:20.0.0'
    implementation 'com.google.firebase:firebase-crashlytics:18.2.4'
    implementation 'com.google.firebase:firebase-analytics:20.0.0'


    implementation 'com.squareup.picasso:picasso:2.71828'

    implementation 'commons-validator:commons-validator:1.6'

    implementation 'com.airbnb.android:lottie:3.4.0'

    //OTP PIN View Design
    implementation 'com.chaos.view:pinview:1.4.3'

    implementation 'androidx.preference:preference:1.1.1'

    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.core:core:1.6.0'


    implementation 'com.google.android.material:material:1.4.0-alpha02'
    implementation 'com.android.support:multidex:1.0.3'

    implementation 'androidx.activity:activity:1.4.0'
    implementation 'androidx.activity:activity-compose:1.4.0'
    implementation 'androidx.activity:activity-ktx:1.4.0'


}

And my build.gradle (project)


// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
        maven{url 'https://maven.fabric.io/public'}
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.4'
        classpath  'com.google.gms:google-services:4.3.10'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven{url 'https://jitpack.io'}
        maven{url 'https://dl.bintray.com/tapsellorg/maven'}
        maven{url 'https://maven.google.com/'}
    }
}

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

Upvotes: 5

Views: 6361

Answers (6)

Ionut Negru
Ionut Negru

Reputation: 6314

For me it worked when I manually cast it to MenuHost:

    private lateinit var menuHost: MenuHost

    override fun onAttach(context: Context) {
        super.onAttach(context)
        menuHost = (requireActivity() as MenuHost)
    }

If this does not work, manually specifying the module you want to use should fix it. In the project build.gradle file you have to add this:

allprojects {
    ...

    configurations.all {
        resolutionStrategy {
            dependencySubstitution {
                substitute module("androidx.activity:activity:1.0.0") using module("androidx.activity:activity:$libVersions.activity")
                substitute module("androidx.fragment:fragment:1.1.0") using module("androidx.fragment:fragment:$libVersions.fragment")
                substitute module("androidx.core:core:1.2.0") using module("androidx.core:core:$libVersions.core")
                substitute module("androidx.core:core-ktx:1.2.0") using module("androidx.core:core-ktx:$libVersions.core")
            }
        }
    }
}

(Double check what version you have in your project that affects you, in my case I noticed 1.00 for activity, 1.1.0 for fragment and 1.2.0 for core - they could be different in your case, you need to check the list of dependencies of your project)

And you can use it directly as:

    private lateinit var menuHost: MenuHost

    override fun onAttach(context: Context) {
        super.onAttach(context)
        menuHost = requireActivity()
    }

Upvotes: 0

Devra Rejava
Devra Rejava

Reputation: 61

MenuHost was added in androidx.core:core:1.7.0 and above.

https://developer.android.com/jetpack/androidx/releases/core#1.7.0-alpha02

Just upgrade your dependencies.

Upvotes: 6

Abigail La'Fay
Abigail La'Fay

Reputation: 829

In the build.gradle i changed this:

configurations.all {
   resolutionStrategy { force 'androidx.core:core:1.6.0' }
}

to this:

configurations.all {
   resolutionStrategy { force 'androidx.core:core:1.7.0' }
}

and this worked for me.

Upvotes: 2

maXp
maXp

Reputation: 1468

Just add in you dependencies:

implementation 'androidx.core:core:{latest version}' // 1.7.0 or above

Upvotes: 1

Bob D. Builder
Bob D. Builder

Reputation: 85

Had the same problem. Fixed the issue by removing this lines:

     configurations.all {
        resolutionStrategy { force 'androidx.core:core:1.6.0' }
     }

under defaultConfig of build.gradle(:app)

I also do not know the source/reason behind the error as well.

Upvotes: 5

Matthew Sparrow
Matthew Sparrow

Reputation: 9

I had exact same situation - changing "extends AppCompatActivity" to "extends Activity" fixed the issue.

Edit: I don't know the source/reason behind that error, sadly...

Upvotes: 0

Related Questions