supertypes of the following classes cannot be resolved. make sure you have the required dependencies

Posted 舒泱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了supertypes of the following classes cannot be resolved. make sure you have the required dependencies相关的知识,希望对你有一定的参考价值。

目录


       

1、问题

  • android Studio 编译报错: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath : class X, unresolved supertypes: Y
  • 重启且清除Android Studio缓存没用,clean后重新build也没用。

       

2、出现这个问题的原因

        咱翻译一下Android Studio 的这个报错信息: 下列类的父类找不到,请确保添加了必要的依赖:类X,未找到的父类Y。
       

为什么找不到类X的父类Y呢?下面举例说明,参考下图:

  • 假设现在有3个module,module A、module B 和 moduleC。
  • 你在module B里定义了类X,在moduleC里定义了类Y,类X的父类是Y,所以你在module B的build.gradle里写上了implementation “moduleC”,这样module B就依赖上了moduleC。
  • 你在module A里想使用类X,所以你在module A的build.gradle里写上了implementation “module B”,这样module A就依赖上了moduleB
  • 现在编译报错了,在module A中,找不到类X的父类Y
  • 然后你很生气,明明已经在build.gradle添加了依赖了呀。你找到类X定义的地方,明明能找到X的爸爸Y,为什么编译要报错!
           

下面说说implementation 和 api的区别:

  • 让 A 依赖 B (implementation、api 都可以)
  • 若B implementation C,A 不能调用 C 的方法
  • 若 B api C,A 可以调用 C 的方法(同 compile)

所以,虽然你让module A implementation “module B”,module B implementation “moduleC”,
A中能找到定义在B中的类X,但你在A里无法调用定义在C中的类Y的方法,所以报错了

       

3、解决办法

方法一、把module B的build.gradle中的implementation “moduleC” 改成 api “moduleC”
方法二、在module A中implementation “module C”

以上两种方法都能使module A能调用到module C中的方法。这样就能找到定义在module C中的类Y了。

以上是关于supertypes of the following classes cannot be resolved. make sure you have the required dependencies的主要内容,如果未能解决你的问题,请参考以下文章

supertypes of the following classes cannot be resolved. make sure you have the required dependencies

:app:compileDebugKotlin Supertypes of the following classes cannot be resolved. Please make sure you

:app:compileDebugKotlin Supertypes of the following classes cannot be resolved. Please make sure you

:app:compileDebugKotlin Supertypes of the following classes cannot be resolved. Please make sure you

The method getByName(String) of type UserServiceImpl must override or implement a supertype method(示

greenDaoCannot access ‘org.greenrobot.greendao.AbstractDaoSession‘ which is a supertype of