Cannot access ActivityCompatApi23 class

2019-01-28 06:48发布

问题:

I am having runtime problems with my gradle file. I added this compile 'com.google.android:flexbox:0.3.1' as a compile time dependency to my Gradle file. I encountered an error and added this in my project level Gradle file.

maven {
            url "https://maven.google.com"
        }

Which finally looked liked this after adding the above

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

After adding the above in my app level Gradle file I am now encountering a different error when I am trying to run my app. So I did the following as per some answers from SO.

  1. Tried a Clean and Rebuild.
  2. Navigated to the path projectName\.idea\libraries and deleted the files that contained the support library version other than the current versions 25.3.1 3.In order to solve the error I further removed this line from my app level Gradle file,

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' })

Now the final Gradle file looks like this with the error,

Error:

Error:(28, 8) error: cannot access ActivityCompatApi23
class file for android.support.v4.app.ActivityCompatApi23 not found

My Gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.google.android:flexbox:0.3.1'
    compile 'uk.co.chrisjenx:calligraphy:2.3.0'
    testCompile 'junit:junit:4.12'
}

回答1:

You have declared compileSdkVersion equal to 25, whereas 0.3.1 version of flexbox layout uses support libs version 26.0.0 - that's a problem, compileSdkVersion should match support libs major version.

Either upgrade your project to 26 or use a version of flexbox layout that relies on sdk 25, which seems to be v0.2.7:

compile 'com.google.android:flexbox:0.2.7'


回答2:

Like the Problem I meet。

When I Use Android Room like this :

compileSdkVersion 25 compile "android.arch.persistence.room:runtime:1.0.0"

I get the same Error.

Because compileSdkVersion should match support libs major version.

More Detail you can see this : Error in support lib after room persistence

Room depends on 26.1 of support library, which is probably why it is broken because SupportLibrary does not promise interop between versions.

Also, you can fix the problem use this

compile ("android.arch.persistence.room:runtime:1.0.0") {
                exclude group: 'com.android.support'
}



回答3:

use

compile 'com.android.support:appcompat-v7:26.1.0'


回答4:

I just changed the line

compile 'com.android.support:appcompat-v7:26.0.0-alpha1'

to compile 'com.android.support:appcompat-v7:26.+'

and it worked for me