如何检查Android项目是否包含Kotlin支持及后续添加方法
Hey there! Let's walk through how to check if your Android project has Kotlin support, and how to add it if it's missing. Here's what you need to know:
There are a few straightforward ways to verify if Kotlin is configured in your project:
Check the project-level build file (either
build.gradlefor Groovy orbuild.gradle.ktsfor Kotlin DSL):- Look for the Kotlin Gradle plugin dependency. In older setups, this will be in the
buildscript > dependenciesblock:classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:x.y.z" - For newer Android Gradle Plugin (AGP) versions, you'll see it in the top-level
pluginsblock:id("org.jetbrains.kotlin.android") version "x.y.z" apply false
(Replace
x.y.zwith the actual Kotlin version number, like1.9.0.)- Look for the Kotlin Gradle plugin dependency. In older setups, this will be in the
Check the module-level build file:
- In the
pluginsblock, look for the Kotlin Android plugin:
Groovy:id 'kotlin-android'
Kotlin DSL:id("org.jetbrains.kotlin.android") - Also check the
dependenciesblock for the Kotlin standard library:implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:x.y.z"
- In the
Quick test via Android Studio:
- Right-click on your app's package name in the Project view →
New→ If you seeKotlin Class/Fileas an option, that means Kotlin support is already enabled. If not, it's likely missing.
- Right-click on your app's package name in the Project view →
If you find Kotlin isn't set up, follow these steps to add it:
Update the project-level build file:
- For Groovy:
Add the Kotlin plugin to thebuildscript > dependenciesblock:
Or for newer AGP versions, add it to the top-levelbuildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0" } }pluginsblock:plugins { id 'com.android.application' version '8.1.0' apply false id 'org.jetbrains.kotlin.android' version '1.9.0' apply false } - For Kotlin DSL:
Add this to the top-levelpluginsblock:plugins { id("com.android.application") version "8.1.0" apply false id("org.jetbrains.kotlin.android") version "1.9.0" apply false }
- For Groovy:
Update the module-level build file:
- Add the Kotlin Android plugin to the
pluginsblock:
Groovy:
Kotlin DSL:plugins { id 'com.android.application' id 'kotlin-android' }plugins { id("com.android.application") id("org.jetbrains.kotlin.android") } - Add the Kotlin standard library to the
dependenciesblock:
Groovy:
Kotlin DSL:dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.0" // ... other dependencies }dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.0") // ... other dependencies }
- Add the Kotlin Android plugin to the
Sync your project:
Click the "Sync Now" button that pops up in the top-right corner of Android Studio. Wait for the sync to complete—this will download the necessary Kotlin dependencies.Verify the setup:
Try creating a new Kotlin class (right-click package →New→Kotlin Class/File) or convert an existing Java file to Kotlin (go toCode→Convert Java File to Kotlin File). If everything compiles without errors, you're good to go!
A quick note: Make sure the Kotlin version you use is compatible with your AGP version. Using the latest stable versions of both usually works smoothly.
内容的提问来源于stack exchange,提问作者Horay

