Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room - cannot find symbol @androidx.room.TypeConverters(value = {StringDataConverter.class, ...})

Tags:

android

how do you use List's in Android Room I've a Table Entity which i like to save in my SQLDatabase via Android Room. I've followed everything i could fine Online and without List's it worked. But when i added List items in my Entity it stopped working:

Table.java:5: error: cannot find symbol
@androidx.room.TypeConverters(value = {StringDataConverter.class, RowDataConverter.class})

 

@Entity(tableName = "tables")
    data class Table(
        var name : String,
        var description : String,
        var image : String,
        var colNames : List<String> = emptyList(),
        var rows : List<Row> = emptyList()
    ){
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0

        override fun toString(): String {
            return "TableEntity(name='$name', description='$description', image='$image', id=$id)"
        }
    }

 

data class Row(var items: List<Unit>)

 

data class Unit(var value : Object, var type : String)

I also tried it without TypeConverters in Table same error, but i got it at File TablesDatabase

 

@Database(entities = [Table::class], version = 1)
@TypeConverters(StringDataConverter::class, RowDataConverter::class)
abstract class TablesDatabase : RoomDatabase(){
    abstract fun tableDao(): TableDao

    companion object {
        @Volatile private var instance: TablesDatabase? = null
        private val LOCK = Any()

        operator fun invoke(context: Context) = instance ?: synchronized(LOCK){
            instance ?: buildDataBase(context).also{
                instance = it
            }
        }    

        private fun buildDataBase(context: Context) =  Room.databaseBuilder(context.applicationContext, TablesDatabase::class.java, "tablesx2.db").build()
    }
}

 

public class StringDataConverter {
    @TypeConverter
    public static List<String> fromString(String value) {
        Type listType = new TypeToken<List<String>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromList(List<String> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
}

public class RowDataConverter {
    @TypeConverter
    public static List<Row> fromString(String value) {
        Type listType = new TypeToken<List<Row>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromList(List<Row> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
}

Ty Stackoverflow

Edit: I tried it with and without Annotating the Table Class as @TypeConverters(StringDataConverter::class, RowDataConverter::class)

Edit 2: I also tried to rewrite the Entity and use Java instead of Kotlin (just for the Entitiy File) didnt change the Exception (Appendix 1)

Edit 3:

i Added the Following lines to my build.gradle (whole Gradle-File Appendix 2)

allprojects {
        gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                options.compilerArgs << "-Xmaxerrs" << "5000"
            }
        }
    }

    android {
        defaultConfig {
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [
                            "room.schemaLocation":"$projectDir/schemas".toString(),
                            "room.incremental":"true",
                            "room.expandProjection":"true"]
                }
            }
        }
    }

And now i see the errors i already saw:

TablesDatabase.java:5: error: cannot find symbol
@androidx.room.TypeConverters(value = {StringDataConverter.class, RowDataConverter.class})
  symbol: class StringDataConverter

and

TablesDatabase.java:5: error: cannot find symbol
@androidx.room.TypeConverters(value = {StringDataConverter.class, RowDataConverter.class})
  symbol: class RowDataConverter

and in addition this new one:

TablesDatabase.java:8: error: [RoomProcessor:MiscError] androidx.room.RoomProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract class TablesDatabase extends androidx.room.RoomDatabase {

Appendix 1

@Entity(tableName = "tables")
    public class Table {
        @PrimaryKey(autoGenerate = true)
        Integer id = 0;
        String name = "";
        String description = "";
        String image = "";
        List<String> colNames = new ArrayList<>();
        List<Row> rows = new ArrayList<>();

        public Table(String name, String description, String image, List<String> colNames, List<Row> rows) {
            this.name = name;
            this.description = description;
            this.image = image;
            this.colNames = colNames;
            this.rows = rows;
        }

        public Table(String name, String description, String image, List<String> colNames) {
            this.name = name;
            this.description = description;
            this.image = image;
            this.colNames = colNames;
        }

        public Table(String name, String description, String image) {
            this.name = name;
            this.description = description;
            this.image = image;
        }

        public Table() {}

        @Override
        public String toString() {
            return "Table{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", description='" + description + '\'' +
                    ", image='" + image + '\'' +
                    ", colNames=" + colNames +
                    ", rows=" + rows +
                    '}';
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public List<String> getColNames() {
            return colNames;
        }

        public void setColNames(List<String> colNames) {
            this.colNames = colNames;
        }

        public List<Row> getRows() {
            return rows;
        }

        public void setRows(List<Row> rows) {
            this.rows = rows;
        }
    }

Appendix 2

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

    allprojects {
        gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                options.compilerArgs << "-Xmaxerrs" << "5000"
            }
        }
    }

    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.2"
        defaultConfig {
            applicationId "de.hsos.ma.adhocdb"
            minSdkVersion 22
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true // This line

            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [
                            "room.schemaLocation":"$projectDir/schemas".toString(),
                            "room.incremental":"true",
                            "room.expandProjection":"true"]
                }
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.core:core-ktx:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
        implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


        def cardViewVersion = "1.0.0"
        def recyclerViewVersion = "1.0.0"
        def glideVersion = "4.11.0"
        def roomVersion = "2.2.3"
        def materialIoVersion = "1.2.0-alpha04"
        def kotlinCoroutinesVersion = "1.3.3"
        def materialDialogVersion = "3.1.1"
        def gsonVersion = "2.8.6"

        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion"


        // room
        kapt "androidx.room:room-compiler:$roomVersion"
        implementation "androidx.room:room-runtime:$roomVersion"
        implementation "androidx.room:room-ktx:$roomVersion"

        //material io
        implementation "com.google.android.material:material:$materialIoVersion"
        // Card View
        implementation "androidx.cardview:cardview:$cardViewVersion"

        // Recyclerview
        implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"

        //glide
        implementation "com.github.bumptech.glide:glide:$glideVersion"
        annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion"

        implementation "com.afollestad.material-dialogs:core:$materialDialogVersion"
        implementation "com.google.code.gson:gson:$gsonVersion"
    }
like image 647
backup backup Avatar asked Jan 24 '20 18:01

backup backup


People also ask

What is Androidx room?

Room is a Database Object Mapping library that makes it easy to access database on Android applications. Rather than hiding the details of SQLite, Room tries to embrace them by providing convenient APIs to query the database and also verify such queries at compile time.

What is TypeConverter in Android?

Use type converters You support custom types by providing type converters, which are methods that tell Room how to convert custom types to and from known types that Room can persist. You identify type converters by using the @TypeConverter annotation.

What is room persistence library?

The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite. Latest Update.

Is room database deprecated?

This method is deprecated. Called by Room when it is initialized. Convenience method to query the database with arguments. Wrapper for SupportSQLiteDatabase. query .


1 Answers

In your table class, you have defined colNames and rows variables as List while in your TypeConverters classes you used ArrayList data type. Because List can not be cast to ArrayList automatically, then Room can't find a suitable TypeConverter for these variables and this is the source of error. To fix it, you must change ArrayList to List in both StringDataConverter and RowDataConverter classes.

public class StringDataConverter {
    @TypeConverter
    public static List<String> fromString(String value) {
        Type listType = new TypeToken<ArrayList<String>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromArrayList(List<String> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
}

public class RowDataConverter {
    @TypeConverter
    public static List<Row> fromString(String value) {
        Type listType = new TypeToken<ArrayList<Row>>() {}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromArrayList(List<Row> list) {
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
}



Update
Please check have you imported StringDataConverter and RowDataConverter classes correctly.

like image 145
Mir Milad Hosseiny Avatar answered Oct 13 '22 21:10

Mir Milad Hosseiny