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

5

22

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

Hyman answered 6/3, 2019 at 7:9 Comment(0)
O
80

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

Ontologism answered 6/3, 2019 at 7:33 Comment(4)
You just killed me :D It works. Incredible... It seems that there is a invisble character at the end.Vertumnus
In my case there was an extra closing bracesBangweulu
Just when I though that there is no place for black magic in the coding. hehe. Thanks.Overshine
I found the same error because I used a - dash in fun name...Antenna
S
6

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

Sharpnosed answered 24/5, 2021 at 2:12 Comment(1)
I had a slightly fancier version: I had to delete the last three brackets and put them back in again.Criseyde
D
1

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.

Deflocculate answered 8/9, 2023 at 14:36 Comment(0)
M
0

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(){} }`

Marlenamarlene answered 20/6, 2022 at 12:49 Comment(0)
P
-1

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.

Pearliepearline answered 2/7, 2022 at 19:13 Comment(2)
Hi, can you explain why should we put function invoked(i.e println()) into main ?Miniature
Only in vscode. It automatically runs it.Pearliepearline

© 2022 - 2024 — McMap. All rights reserved.