AndroidX Room未解析的超类型RoomDatabase

时间:2018-11-05 10:43:26

标签: android kotlin compiler-errors

当我尝试构建我的应用程序时,出现此编译错误:

Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
com.example.persistence.AppDatabase, unresolved supertypes: androidx.room.RoomDatabase

持久性设置位于单独的Android模块(持久性)中。

build.gradle

// Kotlin StdLib
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

// Room
implementation      "androidx.room:room-runtime:$androidXRoom"
kapt "androidx.room:room-compiler:$androidXRoom"
implementation      "androidx.room:room-rxjava2:$androidXRoom"

ext.androidXRoom =“ 2.1.0-alpha02”

我尝试将Kotlin版本,会议室版本更改回Android Arch Room,但无法正常工作。我还尝试清理项目并使Android Studio的缓存无效。但这不起作用。

编辑:AppDatabase源

package com.example.persistence.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.persistence.post.PostDbDao
import com.example.persistence.post.PostDbEntity

@Database(entities = [PostDbEntity::class], version = 1)
abstract class AppDatabase : RoomDatabase() {

    abstract fun favoritePostsDao(): PostDbDao

    companion object {

        var INSTANCE: AppDatabase? = null

        fun getDatabase(context: Context): AppDatabase? {
            if(INSTANCE == null) {
                synchronized(AppDatabase::class) {
                     INSTANCE = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "post_db").build()
                }
            }
            return INSTANCE
        }

        fun destroy() {
            INSTANCE = null
        }
    }

}

4 个答案:

答案 0 :(得分:6)

问题很可能在于您如何定义依赖项,RoomDatabase是公共API的一部分,因为您的AppDatabase对其进行了扩展,并且您大概在下游依赖项中使用了该类。但是,RoomDatabase被声明为仅实现依赖项。这意味着该类在编译期间通常不可用于下游依赖项。

尝试将"androidx.room:room-runtime:$androidXRoom"更改为api配置,使其成为公共API的一部分。那应该可以解决您遇到的错误。

答案 1 :(得分:1)

更改gradle依赖项,例如

services.Configure<IdentityOptions>(options =>
{
    options.ClaimsIdentity.UserNameClaimType = OpenIdConnectConstants.Claims.Name;
    options.ClaimsIdentity.UserIdClaimType = OpenIdConnectConstants.Claims.Subject;
    options.ClaimsIdentity.RoleClaimType = OpenIdConnectConstants.Claims.Role;
});

var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(settings.TokenSigningKey));
services.AddOpenIddict<OpenIddictApplication, Authorization, OpenIddictScope, Token>(options =>
{
    options.AddAuthorizationStore<OpenIddictAuthorizationStore>();
    options.AddTokenStore<OpenIddictTokenStore>();
    options.AddApplicationStore<OpenIddictApplicationStore<MyDbContext>>();
    options.AddScopeStore<OpenIddictScopeStore<MyDbContext>>();
    options.AddMvcBinders();
    options.EnableTokenEndpoint("/api/v1/oauth/token");
    options.AllowPasswordFlow();
    options.AllowRefreshTokenFlow();
    options.UseJsonWebTokens();
    options.AddSigningKey(signingKey);
    options.SetAccessTokenLifetime(settings.AccessTokenLifetime);
    options.SetRefreshTokenLifetime(settings.RefreshTokenLifetime);
    if (!settings.EnableTokenAuthHttpsRequirement)
    {
        options.DisableHttpsRequirement();
    }
});

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.Clear();

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.Authority = settings.JwtAuthority;
        options.Audience = settings.JwtAudience;
        options.MetadataAddress = settings.JwtMetadataAddress;
        options.RequireHttpsMetadata = settings.EnableTokenAuthHttpsRequirement;
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = signingKey,
            ValidIssuers = settings.JwtIssuers,
            NameClaimType = OpenIdConnectConstants.Claims.Subject,
            RoleClaimType = OpenIdConnectConstants.Claims.Role
        };
        options.IncludeErrorDetails = hostingEnvironment.IsDevelopment();
    });

services.AddScoped<IOpenIddictAuthorizationStore<Authorization>, OpenIddictAuthorizationStore>();
services.AddScoped<IOpenIddictTokenStore<Token>, OpenIddictTokenStore>();

enter image description here

答案 2 :(得分:0)

其他答案可以编译,但是无法为数据库/持久性功能使用单独的模块。

您不应该在persistence模块之外公开Room实体或Room数据库。相反,您应该在persistence模块中编写一个函数,该函数将PostEntity @Entity映射为简单的Post数据类。

我在这里更详细地介绍了它:https://jacquessmuts.github.io/post/modularization_room/

答案 3 :(得分:0)

除了答案。当我在创建新模块时选择Module-> Android Library时,“ api”过程已成功完成,我提供了解决方案。