Unexpected tokens (use ';' to separate expressions on the same line) in build.gradle.kts
Asked Answered
V

4

30

that is a screen shot of the error message

I can't add jitpack to build.gradle.kts it shows an error " Unexpected tokens (use ';' to separate expressions on the same line) "

allprojects {
repositories {
    google()
    jcenter()
    maven { url "https://jitpack.io" }
}

}

Vend answered 9/8, 2020 at 23:21 Comment(2)
You are using Groovy syntax in a Kotlin script Gradle file. Try maven { url = uri("https://jitpack.io") }. See the Gradle documentation.Aubergine
@Aubergine thanks it works with meVend
A
93

Support for Kotlin Gradle scripts is fairly new in Android — for example, Android Studio only added support for it in version 4.0. Most of the instructions that you will see online will be for Groovy Gradle scripts, and you will need to make some minor conversions.

In this case, this Groovy:

maven { url "https://jitpack.io" }

...turns into this Kotlin:

maven { url = uri("https://jitpack.io") }
Aubergine answered 9/8, 2020 at 23:32 Comment(1)
great. it works for java also.Allergen
D
6

Problem might be .. as you mentioned that the problem is in "in build.gradle.kts" this means you are using kotlin gradle script. So, simply change maven { url "https://jitpack.io" } to maven { url = uri("https://jitpack.io") }.

Or,

Rather than you can also skip adding maven { url = uri("https://jitpack.io") } in build.kts and simply go to settings.gradle and add this dependency here : -

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        ...
        maven { url = uri("https://jitpack.io") } // Here is your dependency
 

} }

Disquietude answered 21/9, 2023 at 12:56 Comment(0)
D
3

Gradle 8.4 dependency use TOML Kotlin

  1. setting.gradle.kts, Here must register jitpack :

    dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories {

        google()
    
        mavenCentral()
    
        maven { url = uri("https://jitpack.io") }
     }
    

    }

  2. libs.versions.toml: below Gradle Script, there is a file is called TOML.In this version of Gradle, all the decencies must determine in this file by URL, version.(example: GitHub)

enter image description here

[versions]

advanced_menu = "1.0.0"

agp = "8.3.0-alpha17"


[libraries]

advanced_menu_mori = { module = "com.github.Mori-hub:Advanced_Menu", version.ref = "advanced_menu" }

androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }

3.build.gradle.kts(:app): Finally, add new lib in the dependencies by new pattern ->

libs.name is defined in toml file separated by dot

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation (libs.advanced.menu.mori)
}
Domitian answered 9/12, 2023 at 6:38 Comment(0)
G
0

Even though all the above answers are correct in their own way, just wanted to highlight that

Gradle Script files written using the Kotlin DSL do not accept a single quote (') for defining strings. You should use double quotes (") wherever applicable. And also, you should use braces with the because, implementation and api keywords

For example

implementation 'com.simpl.android:fingerprintSDK:1.1.2'  {
        because 'We use Simpl SDK for Payment'
    }

is invalid

But

implementation("com.simpl.android:fingerprintSDK:1.1.2")  { // Replaced with brackets and double quotes
        because("We use Simpl SDK for Payment")
    }

is valid

Grumous answered 26/3 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.