Kotlin Multiplatform
Instructions for installing Purchases SDK for Kotlin Multiplatform
What is RevenueCat?
RevenueCat provides a backend and a wrapper around StoreKit and Google Play Billing to make implementing in-app purchases and subscriptions easy. With our SDK, you can build and manage your app business on any platform without having to maintain IAP infrastructure. You can read more about how RevenueCat fits into your app or you can sign up free to start building.
Requirements
Android 5.0+ (API 21+)
iOS 13.0+
Installation
Adding the dependency
Purchases for Kotlin Multiplatform (Google Play and iOS App Store) is available on Maven Central and can be included via Gradle.
Add the following coordinates to your libs.versions.toml.
- libs.versions.toml
[versions]
purchases-kmp = "<latest version>"
[libraries]
# Required
purchases-core = { module = "com.revenuecat.purchases:purchases-kmp-core", version.ref = "purchases-kmp" }
# Optional: adds suspending functions that return Arrow's Either to indicate success / failure.
purchases-either = { module = "com.revenuecat.purchases:purchases-kmp-either", version.ref = "purchases-kmp" }
# Optional: adds suspending functions that return kotlin.Result to indicate success / failure.
purchases-result = { module = "com.revenuecat.purchases:purchases-kmp-result", version.ref = "purchases-kmp" }
You can now add the dependency to your commonMain source set in your module's build.gradle.kts.
- build.gradle.kts
kotlin {
// ...
sourceSets {
// ...
commonMain.dependencies {
// Add the purchases-kmp dependencies.
implementation(libs.purchases.core)
implementation(libs.purchases.either) // Optional
implementation(libs.purchases.result) // Optional
}
}
}
Opt in to ExperimentalForeignApi
Since the SDK uses generated Kotlin bindings for native code on iOS, you will need to opt in to ExperimentalForeignApi in your iOS source sets. To do so, add the following to your module's build.gradle.kts.
- build.gradle.kts
kotlin {
// ...
sourceSets {
// ...
named { it.lowercase().startsWith("ios") }.configureEach {
languageSettings {
optIn("kotlinx.cinterop.ExperimentalForeignApi")
}
}
}
}
Set the correct launchMode for Android
Depending on your user's payment method, they may be asked by Google Play to verify their purchase in their (banking) app. This means they will have to background your app and go to another app to verify the purchase. If your Activity's launchMode is set to anything other than standard or singleTop, backgrounding your app can cause the purchase to get cancelled. To avoid this, set the launchMode of your Activity to standard or singleTop in your Android app's AndroidManifest.xml file:
- AndroidManifest.xml
<activity
android:name="com.your.Activity"
android:launchMode="standard" /> <!-- or singleTop -->
You can find Android's documentation on the various launchMode options here.
Build static binaries for iOS targets
In some cases it may be required to configure your iOS targets to build static binaries, in order for your project to compile. Make sure isStatic is set to true in your iOS targets' binary output settings in your build.gradle.kts:
- build.gradle.kts
kotlin {
// ...
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
iosTarget.binaries.framework {
baseName = "ComposeApp"
isStatic = true
}
}
}
Import Purchases
You should now be able to import Purchases.
- commonMain
import com.revenuecat.purchases.kmp.Purchases
import com.revenuecat.purchases.kmp.models.CustomerInfo
import com.revenuecat.purchases.kmp.models.EntitlementInfo
import com.revenuecat.purchases.kmp.models.Offering
import com.revenuecat.purchases.kmp.models.Period
import com.revenuecat.purchases.kmp.models.Price
import com.revenuecat.purchases.kmp.models.StoreProduct
On Android, Purchases uses AndroidX App Startup under the hood. Make sure you have not removed the androidx.startup.InitializationProvider completely in your manifest. If you need to remove specific initializers, such as androidx.work.WorkManagerInitializer, set tools:node="merge" on the provider, and tools:node="remove" on the meta-data of the initializer you want to remove.
- AndroidManifest.xml
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data
android:name="androidx.work.WorkManagerInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
Next Steps
- Now that you've installed the Purchases SDK in Kotlin Multiplatform, get started by configuring an instance of Purchases →