How to Code in Kotlin Using AI
Use an AI assistant to write a Kotlin function, run it, and prove it with tests. Kotlin is the main language for Android apps and also runs on servers.
In short: say the Kotlin version, give the formula or rules and one example with the exact answer, ask for kotlin.test tests, and run them.
Why Kotlin with AI
Kotlin is Google's preferred language for Android. Many Android teams write Kotlin every day. AI speeds up the repeated parts, but money and math must be checked with tests.
What you need
- JDK 17 or 21: Kotlin runs on the JVM.
- Kotlin: through Android Studio or IntelliJ (bundled), or the kotlinc command-line compiler.
- kotlin.test: Kotlin's test library; works with JUnit in Gradle projects.
- IDE + assistant: Android Studio or IntelliJ with Copilot, or VS Code with Claude Code.
How it flows
Install and check
- Install IntelliJ IDEA or Android Studio (Kotlin is included), or the kotlinc compiler from the official Kotlin releases.
- Check:
kotlinc -version(command line) or create a new Kotlin project in the IDE. - In Gradle projects, add
testImplementation(kotlin("test"))and run./gradlew test.
Step 1 — the prompt
Write a Kotlin function emi(principal: Double, annualRatePercent: Double, months: Int): Long for a loan EMI using EMI = P × r × (1+r)^n / ((1+r)^n − 1), where r is the monthly rate. Round to the nearest rupee. Zero interest means principal / months. Use require() for bad input. Example: emi(500000.0, 9.0, 60) = 10379. Only return the code.
Step 2 — the code (tested here)
Emi.kt// Monthly loan EMI. Built with an AI assistant, then tested.
import kotlin.math.pow
import kotlin.math.roundToLong
/** EMI = P * r * (1+r)^n / ((1+r)^n - 1), r = monthly rate. Returns rupees, rounded. */
fun emi(principal: Double, annualRatePercent: Double, months: Int): Long {
require(principal > 0) { "principal must be positive" }
require(months > 0) { "months must be positive" }
require(annualRatePercent >= 0) { "rate cannot be negative" }
if (annualRatePercent == 0.0) return (principal / months).roundToLong()
val r = annualRatePercent / 12 / 100
val f = (1 + r).pow(months)
return (principal * r * f / (f - 1)).roundToLong()
}
fun main() {
println(emi(500_000.0, 9.0, 60)) // 10379
}kotlinc Emi.kt -include-runtime -d emi.jar
java -jar emi.jar
# -> 10379Step 3 — ask for tests, then run them
Write kotlin.test checks for emi: 5 lakh at 9% for 60 months, zero interest, negative principal, and zero months.
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
fun main() {
assertEquals(10379, emi(500_000.0, 9.0, 60)) // 5 lakh, 9%, 5 years
assertEquals(10000, emi(120_000.0, 0.0, 12)) // zero interest
assertFailsWith<IllegalArgumentException> { emi(-1.0, 9.0, 12) }
assertFailsWith<IllegalArgumentException> { emi(100_000.0, 9.0, 0) }
println("4 checks passed")
}# -> 4 checks passedSettings and config
- Rules file: “Kotlin 2.x, null-safety, no !!, kotlin.test/JUnit, coroutines for async”.
- Turn on warnings as errors in CI:
kotlin { compilerOptions { allWarningsAsErrors.set(true) } }in Gradle. - Use the IDE's “Reformat Code” after accepting AI code.
Watch out (common AI mistakes in Kotlin)
- The
!!operator — it crashes on null; ask for safe calls (?.) orrequireNotNull. - Double for money — fine for an EMI estimate; use
BigDecimalfor real accounting. - Old Android APIs — say “Jetpack Compose” or your minSdk.
- Blocking calls on the main thread — ask for coroutines.
Quick quiz
1. What is emi(500000.0, 9.0, 60)?
10379 rupees.
2. Why test 0% interest?
The formula divides by zero there; code must handle it separately.
3. Which Kotlin operator often causes crashes?
!! (not-null assertion).
4. Which type should real accounting use?
BigDecimal.
Save tokens and money
Give the formula in the prompt; the AI does not need to search or guess. Ask for one function plus tests. Paste only the top lines of a Gradle error.