在build.gradle.kts中集成Glide库报错,求正确集成方法
Glide库正确集成方法
问题原因
你写的依赖声明格式错误,Gradle无法解析无引号的依赖参数,导致报Unexpected tokens (use ';' to separate expressions on the same line)错误。
正确集成步骤
1. 添加正确的依赖声明
在模块级build.gradle(通常是app/build.gradle)的dependencies块中,添加Glide核心库和注解处理器:
- Java项目:
dependencies { // Glide核心库 implementation 'com.github.bumptech.glide:glide:4.16.0' // Glide注解处理器(用于自定义GlideModule等注解) annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0' }
- Kotlin项目:
dependencies { implementation 'com.github.bumptech.glide:glide:4.16.0' // Kotlin项目用kapt替代annotationProcessor kapt 'com.github.bumptech.glide:compiler:4.16.0' }
2. 确认仓库配置正确
确保项目已配置Maven Central仓库:
- Gradle 7.0+版本(修改
settings.gradle):
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() // 若需使用快照版本,添加以下仓库 // maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } }
- 旧版Gradle(修改根目录
build.gradle):
allprojects { repositories { google() mavenCentral() } }
3. 同步项目并验证
添加完成后,点击Android Studio右上角的「Sync Now」同步Gradle。同步成功后,即可正常使用Glide加载图片,示例代码:
- Kotlin:
Glide.with(this) .load("https://example.com/test.jpg") .into(yourImageView)
- Java:
Glide.with(this) .load("https://example.com/test.jpg") .into(yourImageView);
内容的提问来源于stack exchange,提问作者Aditya Deshwal
相关产品推荐
相关产品推荐

