错误:无法解析以下类的超类型。请确保类路径中有所需的依赖项

问题描述 投票:0回答:18

我无法编译我的 Android Kotlin 项目。

我不知道这是什么...

Gradle 日志:

错误:无法解析以下类的超类型。请确保类路径中具有所需的依赖项: 类 android.support.v7.app.AppCompatActivity,未解析的超类型:SupportParentable

build.gradle(应用程序)

buildscript {
    ext.android_plugin_version = "2.3.3"
    ext.kotlin_version = '1.1.2-5'

    repositories {
        maven { url 'https://maven.google.com' }
        maven { url "https://jitpack.io" }

        mavenCentral()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:$android_plugin_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.google.com' }
        maven { url "https://jitpack.io" }

        mavenCentral()
    }
}

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

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

def compat_version = '26.+'
def play_services_version = '11.0.1'
def firebase_version = '9.6.1'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"

    defaultConfig {
        applicationId "com.site.app"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["library" : "true"]
            }
        }
    }

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

    dexOptions {
        javaMaxHeapSize "4g"
    }

    dataBinding {
        enabled true
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

kapt {
    generateStubs = true
    correctErrorTypes = true
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'

    // android
    compile 'com.android.support:multidex:1.0.1'
    compile "com.android.support:appcompat-v7:${compat_version}"
    compile "com.android.support:design:${compat_version}"
    compile "com.android.support:cardview-v7:${compat_version}"
    compile "com.android.support:recyclerview-v7:${compat_version}"
    compile "com.android.support:gridlayout-v7:${compat_version}"
    compile "com.google.android.gms:play-services:${play_services_version}"
    compile "com.google.android.gms:play-services-ads:${play_services_version}"
    compile "com.google.android.gms:play-services-maps:${play_services_version}"
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.maps.android:android-maps-utils:0.4+'
    kapt "com.android.databinding:compiler:$android_plugin_version"

    // kotlin
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
android compilation kotlin
18个回答
63
投票

在我的例子中,我有一个库模块,其中有一个实现 Dagger 的

HasActivityInjector
的抽象应用程序类,以及一个依赖于库的应用程序模块及其自己的(非抽象)应用程序类,扩展了基本应用程序类。

我的 Gradle 依赖项是

implementation
,因此无法访问应用程序模块的应用程序类(即使那里没有导入,否则问题会立即显而易见,因为您会收到“无法解析”错误。修复方法是将 dagger
implementation
依赖项替换为
api
,这使得它们也可用于依赖模块。


15
投票

我通过更改插件调用的顺序修复了它!

build.gradle

buildscript {
    ext.android_plugin_version = '2.3.3'
    ext.kotlin_version = '1.1.3-2'

    repositories {
        jcenter()
        mavenCentral()

        maven { url "https://maven.google.com" }
        maven { url 'https://maven.fabric.io/public' }
        maven { url "https://jitpack.io" }
    }

    dependencies {
        classpath "com.android.tools.build:gradle:$android_plugin_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()

        maven { url "https://maven.google.com" }
        maven { url 'https://maven.fabric.io/public' }
        maven { url "https://jitpack.io" }
    }
}

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

应用程序/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"

    defaultConfig {
        applicationId "com.sample.app"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    buildTypes {
        debug {
            ext.alwaysUpdateBuildId = false
        }

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

    dexOptions {
        javaMaxHeapSize "4g"
    }

    dataBinding {
        enabled true
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        abortOnError false
        disable 'InvalidPackage'
    }
}

kapt {
    generateStubs = true
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'

    // kotlin
    kapt "com.android.databinding:compiler:$android_plugin_version"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    // android
    compile 'com.android.support:multidex:1.0.1'
    compile "com.android.support:appcompat-v7:${compat_version}"
    compile "com.android.support:design:${compat_version}"
    compile "com.android.support:cardview-v7:${compat_version}"
    compile "com.android.support:recyclerview-v7:${compat_version}"
    compile "com.android.support:gridlayout-v7:${compat_version}"
    compile "com.android.support:support-vector-drawable:${compat_version}"
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile "com.google.android.gms:play-services-ads:${play_services_version}"
    compile "com.google.android.gms:play-services-maps:${play_services_version}"
    compile "com.google.android.gms:play-services-gcm:${play_services_version}"
    compile 'com.google.maps.android:android-maps-utils:0.4+'

    // fabric
    compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
        transitive = true;
    }

    // logger
    compile 'com.orhanobut:logger:2.1.1'

    // dexter
    compile 'com.karumi:dexter:4.1.0'

    // firebase
    compile "com.google.firebase:firebase-messaging:${firebase_version}"

    // persistence
    compile "android.arch.persistence.room:runtime:1.0.0-alpha3"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha3"

    // facebook
    compile 'com.facebook.android:facebook-android-sdk:4.+'

    // retrofit
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'

    // gson
    compile 'com.google.code.gson:gson:2.8.1'

    // jobs
    compile 'com.evernote:android-job:1.1.11'

    // chart
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'

    // fresco
    compile 'com.facebook.fresco:fresco:1.3.0'

    // indicator
    compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
}

14
投票

在尝试其他操作之前,请尝试: Android Studio -> 文件 -> 无效缓存/重启

这对我有用。


4
投票

对我来说,问题是子模块不使用 AndroidX Activity。我猜类名冲突了。

添加此解决方案:

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


3
投票

我通过从

dagger hilt
java(generated)
我的项目
中删除与
rebuild

相关的文件解决了我的问题

2
投票

确保在项目级别添加相同的依赖项以及添加到模块中的依赖项


1
投票

(应用程序)-----取决于----(库)

就我而言,我没有更改任何依赖项配置,也没有更改顺序。 我必须将本地 pojo(模型)jar 文件从库模块复制到应用程序。

应用程序是用 kotlin 编写的 库是java的

这是一个错误,我不应该在应用程序和库中包含 pojo.jar


1
投票

清理项目并重建它这对我有用


1
投票

在我的例子中,这个错误是随机发生的,通常在通过 Android Studio 执行期间发生。

根本原因是我的 Activity 类扩展了另一个嵌套包中的一些接口:

套餐电视活动

public class MainActivity implements MyListener {

} 

包tv.activities.error

interface MyListener {

}

因此它通常在执行期间发生,但在第二次运行后它会自动解决,因此没有人费心去解决它。有时 Android Studio 无法识别该界面是可访问的,因此没有显示相同错误的感觉:

Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
    class tv.activities.MainActivity, unresolved supertypes: MyListener

最终解决方案:

公开接口可以永久解决此问题。

public interface MyListener {
}

或者重构包的可访问性,使 AS 不会混淆


0
投票

就我而言,我必须添加对主模块所需的依赖项,并使用以下命令编译最后一个版本:

android {
    configurations.all {
        resolutionStrategy.force "com.squareup.picasso:picasso:${picassoVersion}"
    }
}

0
投票

我在应用程序级别 build.gradle 中收到以下库的错误

  implementation 'com.google.firebase:firebase-iid:20.0.2'

所以解决方案是项目级别build.gradle中的顺序,所以 在解决错误之前,结构是

buildscript {
ext.kotlin_version = '1.3.50'
repositories {

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

}
dependencies {
    classpath 'com.android.tools.build:gradle:3.5.3'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
    classpath 'com.google.gms:google-services:4.3.3'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {

    google()
    jcenter()
    mavenCentral()
    maven {
        url "https://maven.google.com" // Google's Maven repository
    }

}
}

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

解决错误后结构是:

buildscript {
ext.kotlin_version = '1.3.50'
repositories {
     maven {
        url "https://maven.google.com"
    }
    google()
    jcenter()
    mavenCentral()


}
dependencies {
    classpath 'com.android.tools.build:gradle:3.5.3'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
    classpath 'com.google.gms:google-services:4.3.3'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    maven {
        url "https://maven.google.com" // Google's Maven repository
    }
    google()
    jcenter()
    mavenCentral()


}
}

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

默认情况下,它会尝试使用 google() 下载,因为它在序列中排在第一位,但是当我们将 Maven 移到所有内容之上时,它会再次使用 Maven 创建构建,并且构建成功。干杯:)


0
投票

在为我的应用程序设置 crashlytics 时,我也使用了“推荐”依赖项,如下所示。但这是导致错误的部分。

// Firebase Crashlytics
// (Recommended) Add the Google Analytics dependency.
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

删除“分析”行解决了我的问题。

// Firebase Crashlytics
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

希望这对其他人也有帮助,

最好的


0
投票

我修复了升级其他库时出现的错误。 当我添加 firebase=ads (来自 google adsmob)时,出现了我的错误。 所以我发现我的 firebase-auth (16.0.5) 和 firebase ads (19.0.1) 存在冲突。

将 firebase 迁移到 19.3.0 解决了问题。


0
投票

就我而言,我删除了片段中未使用的

OnFragmentInteractionListener

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

但是,在我的活动中,我未能删除对此的引用

OnFragmentInteractionListener

public class MainActivity extends AppCompatActivity implements
        MyFragment.OnFragmentInteractionListener {

奇怪的是,我没有给出指示未找到

OnFragmentInteractionListener
的错误,而是收到了上面的 “无法解析以下类的超类型”,而没有引用这些类。


0
投票

就我而言,我进入了项目结构并将所有 Firebase 依赖项更新为最新和最高。目前这里是其版本的实现:

    implementation 'com.google.firebase:firebase-auth:19.3.1'
    implementation 'com.google.firebase:firebase-database:19.3.1'
    implementation 'com.google.firebase:firebase-storage:19.1.1'
    implementation 'com.google.firebase:firebase-analytics:17.4.3'

0
投票

我删除了构建文件夹并重新启动了项目 应用程序/构建


0
投票

为 api 模块添加 gradle 依赖项后,我遇到了同样的错误。我正在使用

implementation(projects.someDependency)
这使得 api 模块中的内容无法访问。将其更改为
api(projects.someDependency)
效果很好。


-1
投票

将您的 google play gradle 版本更新到最新

© www.soinside.com 2019 - 2024. All rights reserved.