【问题标题】:Error MyApplication Execution failed for task ':app:kaptDebugKotlin'任务':app:kaptDebugKotlin'的错误MyApplication执行失败
【发布时间】:2021-04-06 22:45:02
【问题描述】:

我的MyApplication有错误,每次看到我构建的项目出现,我觉得和房间有关系但是我解决不了

日志:

Task :app:kaptDebugKotlin FAILED
C:\Users\matheus.correa_est\Desktop\Pokedex- 
master\app\build\tmp\kapt3\stubs\debug\com\example\pokedex\MyApplication.java:13: error: cannot find 
symbol
    public final AppDataBase getDataBase() {
                 ^
  symbol:   class AppDataBase
  location: class MyApplication
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more 
log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 2s
24 actionable tasks: 1 executed, 23 up-to-date

MyApplication.java

package com.example.pokedex;

import java.lang.System;

d2 = {"Lcom/example/pokedex/MyApplication;", "Landroid/app/Application;", "()V", "dataBase", 
"LAppDataBase;", "getDataBase", "()LAppDataBase;", "dataBase$delegate", "Lkotlin/Lazy;", 
"repository", "Lcom/example/pokedex/repository/PokedexRepository;", "getRepository", " 
()Lcom/example/pokedex/repository/PokedexRepository;", "repository$delegate", "app_debug"})
public class MyApplication extends android.app.Application {
    @org.jetbrains.annotations.NotNull()
    private final kotlin.Lazy dataBase$delegate = null;
    @org.jetbrains.annotations.NotNull()
    private final kotlin.Lazy repository$delegate = null;

    @org.jetbrains.annotations.NotNull()
    public final AppDataBase getDataBase() {   //  line where the error is being shown in the log
        return null;
    }

    @org.jetbrains.annotations.NotNull()
    public final com.example.pokedex.repository.PokedexRepository getRepository() {
        return null;
    }

    public MyApplication() {
        super();
    }
}

类应用数据库

@Database(entities = [PokemonItem::class], version = 1, exportSchema = false)
abstract class AppDataBase: RoomDatabase() {
abstract fun pokemonDAO(): PokemonDAO

companion object {
    @Volatile
    private var INSTANCE: AppDataBase? = null
    fun getDataBase(context: Context): AppDataBase {
        return INSTANCE ?: synchronized(this) {
            val instance = Room.databaseBuilder(
                context.applicationContext,
                AppDataBase::class.java,
                "pokemon-db"
            )
                .build()
            INSTANCE = instance
            instance
        }
    }
   }
 }

自从我将 AppDatabase 用于数据持久性以来,此错误开始发生,我认为问题出在 gradle 中,但据我所见,我无法通过其版本修复它

【问题讨论】:

    标签: java android kotlin android-room


    【解决方案1】:

    看起来很不对劲。只需关注谷歌文档:

    https://developer.android.com/training/data-storage/room

    【讨论】:

      【解决方案2】:

      这是一个不同的基本应用程序的工作示例,它混合了 Kotlin 和 Java,并包含一个 Room 数据库。它基于您的代码的基本思想/意图。然后,您可以对此进行扩展。

      Kotlin 中的 PokemonItem 实体(表)类:-

      @Entity
      class PokemonItem {
          @PrimaryKey
          var id: Long? = null
          var name: String = ""
      }
      

      PokemonDao 接口 (Kotlin) 只需一个查询即可提取所有内容(因此使用它会创建数据库):-

      @Dao
      public interface PokemonDao {
      
          @Query("SELECT * FROM pokemonitem")
          fun getAllFromPokemonItem(): List<PokemonItem>
      }
      

      一个简化的 AppDatabase 抽象类(Kotlin)(不是单例):-

      @Database(entities = arrayOf(PokemonItem::class),version = 1)
      abstract class AppDatabase: RoomDatabase() {
          abstract fun pokeMonDao(): PokemonDao
      }
      

      一个 Activity (Java) 使用上述方法提取所有(没有添加,因为没有添加)PokemonItems:-

      public class MainActivity extends AppCompatActivity {
      
          AppDatabase pokemonDB;
          PokemonDao pokemonDao;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              pokemonDB = Room.databaseBuilder(
                      this,
                      AppDatabase.class,
                      "poekmon.db"
              )
                      .allowMainThreadQueries()
                      .build();
              pokemonDao = pokemonDB.pokeMonDao();
              List<PokemonItem> pokemonItems = pokemonDao.getAllFromPokemonItem();
          }
      }
      

      结果

      运行然后使用 Database Inspector (Android Studio) 显示:-

      即它已编译并运行,并且由于尝试打开数据库而创建了数据库。

      • 请注意,.allowMainThreadQueries 允许它在主线程上运行,并且为简洁起见。

      :-

      【讨论】:

      • 执行上述操作后,我在 Main: private AppDataBase pokemonDB; 上出现错误。 ^ 符号:类 AppDataBase 位置:类 MainActivity FAILURE:构建失败并出现异常。 * 出了什么问题:任务 ':app:kaptDebugKotlin' 执行失败。 > 执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障 > java.lang.reflect.InvocationTargetException(无错误消息)
      【解决方案3】:

      问题解决了,这个类不在包中,因此它没有导入,所以我指出了错误

      package com.example.pokedex.dao
      

      【讨论】:

        猜你喜欢
        • 2018-04-01
        • 1970-01-01
        • 2020-11-09
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-17
        相关资源
        最近更新 更多