Installation
Moshi is distributed as a set of JARs, available on Maven Central. To use Moshi in your project, add the necessary dependencies to your build configuration.
Snapshots of the development version are available in the Central Portal Snapshots repository.
Core Library (Java)
For Java projects, you only need the core moshi
artifact.
Gradle (Kotlin DSL)
implementation("com.squareup.moshi:moshi:1.15.2")
Maven
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.15.2</version>
</dependency>
Kotlin Support
Moshi has first-class support for Kotlin, which requires additional dependencies depending on your chosen approach.
Kotlin Reflection
For reflection-based serialization of Kotlin classes, which is easy to set up, you need moshi-kotlin
and the kotlin-reflect
library.
Gradle (Kotlin DSL)
implementation("com.squareup.moshi:moshi-kotlin:1.15.2")
// The kotlin-reflect library is a transitive dependency of moshi-kotlin
Maven
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi-kotlin</artifactId>
<version>1.15.2</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>...</version> <!-- Use your project's Kotlin version -->
</dependency>
Kotlin Codegen (KSP)
For the best performance and to avoid the kotlin-reflect
dependency, you can use Moshi's code generation with the Kotlin Symbol Processing API (KSP).
Gradle (Kotlin DSL)
First, apply the KSP plugin in your build.gradle.kts
file:
plugins {
id("com.google.devtools.ksp") version "1.8.21-1.0.11" // Or the latest version
}
Then, add the moshi-kotlin-codegen
dependency to your KSP configuration:
dependencies {
implementation("com.squareup.moshi:moshi:1.15.2")
ksp("com.squareup.moshi:moshi-kotlin-codegen:1.15.2")
}
Extra Adapters
Moshi provides a separate artifact, moshi-adapters
, which contains useful, pre-built adapters for common types like java.util.Date
and advanced enum handling.
Gradle (Kotlin DSL)
implementation("com.squareup.moshi:moshi-adapters:1.15.2")
Maven
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi-adapters</artifactId>
<version>1.15.2</version>
</dependency>