GETTING STARTED
Common Gradle and Kotlin Build Errors When Setting Up Android Projects on Linux (And How to Fix Them)
By John Simiyu • jonysimiyu75@gmail.com
Setting up a fresh Android/Kotlin project from scratch on Linux, outside of Android Studio's guided setup, surfaces a couple of errors that are not always obvious from the message alone. Here are two real ones, with root cause and fix.
Error 1: Repository mode conflict
Build was configured to prefer settings repositories over project repositories
but repository 'Google' was added by build file 'build.gradle'
Cause
Modern Gradle projects often declare repositories centrally in settings.gradle using:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
This mode explicitly forbids individual build files from declaring their own repositories. The conflict shows up when a top-level build.gradle still has a leftover legacy block:
allprojects {
repositories {
google()
mavenCentral()
}
}
This allprojects pattern was standard in older Android project templates but directly conflicts with the newer centralized declaration style.
Fix
Remove the allprojects { repositories {...} } block entirely from the top-level build.gradle. You can safely keep a buildscript { repositories {...} } block if present, since that one is exempt, being for build tooling rather than project dependencies.
Error 2: Unresolved Kotlin extension functions
e: Unresolved reference: toMediaType
e: Unresolved reference: toRequestBody
Cause
A wildcard import like:
import okhttp3.*
does not pull in Kotlin extension functions such as .toMediaType() or .toRequestBody(). These are defined as Kotlin extensions on companion objects and require an explicit import, since wildcard imports do not resolve them.
Fix
Add the specific imports:
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.MediaType.Companion.toMediaType
After adding these, a clean rebuild should succeed:
./gradlew assembleDebug
# BUILD SUCCESSFUL in 27s
General debugging approach that helped
For both errors, the fix came from reading the actual error message carefully rather than assuming a generic clean and rebuild would help. Gradle and Kotlin error messages are usually specific about the exact reference or repository causing the problem, so it is worth resisting the urge to skip past them.
It is also worth verifying file contents directly (cat, grep -c for expected patterns, or line counts) after any scripted edit, rather than trusting that a paste or scripted insertion succeeded. Terminal scrollback can sometimes make a complete file look truncated, and conversely, a silent partial write can look fine at a glance.
Summary
- Repository conflicts: check for legacy allprojects blocks when using centralized repository management in settings.gradle.
- Missing Kotlin extensions: wildcard imports do not cover extension functions, so import them explicitly.
- Always verify the actual file content after any scripted or automated edit.