ΩlostA
ΩlostA

Reputation: 2601

Android/Kotlin: Error: "Expecting a top level declaration > Task :app:buildInfoGeneratorDebug"

I try to write a class to manage a SQLite DB, but I have the error message "Expecting a top level declaration > Task :app:buildInfoGeneratorDebug".

   package com.xexxxwxxxxs.GMP

    import android.database.sqlite.SQLiteDatabase
    import android.database.sqlite.SQLiteOpenHelper
    import android.content.Context
    import android.content.ContentValues

    class DBHandler(context: Context, name: String?, factory: SQLiteDatabase.CursorFactory?, version: Int) : SQLiteOpenHelper(context, DATABASE_NAME, factory, DATABASE_VERSION)
    {
        override fun onCreate(db: SQLiteDatabase)
        {

        }

        override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int)
        {

        }

        companion object
        {
            private val DATABASE_VERSION = 1
            private val DATABASE_NAME = "GMP.db"
        }
    } 

Do you have any ideas?

Thanks in advance

Upvotes: 24

Views: 82153

Answers (5)

NelsonGon
NelsonGon

Reputation: 13309

If, like me, you are working in code (VSCode), you should remove the call to main() at the end of the file. For example

fun main(){
println("This will fail")
}
main()

For the above, we shall get the following error:

your_file.kt:50:6: error: expecting a top level declaration main()

Removing the call to main() fixes the issue.

Upvotes: -1

Sludge
Sludge

Reputation: 7385

I had recently changed my package name (e.g., com.example.myappname) and found that the MainActivity.kt package name on Line 1 was not updated to the new name -- which caused this error.

Upvotes: 1

Hamid Raza Goraya
Hamid Raza Goraya

Reputation: 159

This error can show up if you have defined any object outside of class declaration

example :
class someActivity extands something{ } private someMethod(){}

to solve this just move it inside

`class someActivity extands something{

private someMethod(){} }`

Upvotes: 0

Ensar Bayhan
Ensar Bayhan

Reputation: 943

I just delete the last curly brace and write it again. It's working :)

Upvotes: 84

michael ditrick
michael ditrick

Reputation: 101

The same happened to me, you just need to remove an auto-generated extra curly bracket at the end, then it works.

Upvotes: 6

Related Questions