Get started with the Android-Kotlin SDK
このページはまだあなたの言語に翻訳されていません。 現在取り組んでいますので、後でもう一度確認してください。
This guide helps you install the Android-Kotlin SDK (com.amplitude:analytics-android), initialize it, and send one custom event to Amplitude. Each step lists the conditions that commonly break that step, so you can fix a problem where it happens. For the full option list, go to Configuration options. For changes between versions, go to the Android-Kotlin SDK release notes.
The Android-Kotlin SDK is the analytics-only package and works with Kotlin and Java. If you also need Session Replay or Feature Experiment, install the Unified SDK for Android (com.amplitude:unified-android) instead. The Unified SDK includes the Android-Kotlin SDK, so don't add both. If your app still uses the maintenance Android SDK (com.amplitude:android-sdk), go to Migrate to the Android-Kotlin SDK.
Prerequisites
- An Amplitude project and its API key. To find the key, go to Settings > Projects, select your project, and open General.
- The data center your organization uses: US (default) or EU.
- An app with
minSdk21 (Android 5.0) or later. - The
INTERNETpermission in your app'sAndroidManifest.xml. The SDK's own manifest declares no permissions.
Install the SDK
Add the dependency to your module's build file, then sync the project.
dependencies {
implementation("com.amplitude:analytics-android:1.37.0")
}
Then add the permissions to AndroidManifest.xml. ACCESS_NETWORK_STATE is optional. That permission lets the SDK hold events while the device is offline and upload them when it reconnects.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Fix installation problems
| Symptom | Cause | Fix |
|---|---|---|
Logcat warning: No ACCESS_NETWORK_STATE permission, offline mode is not supported. ... | The app doesn't declare ACCESS_NETWORK_STATE. | Add the permission if you want offline support. Without it, the SDK treats the device as always online. |
Logcat error: Request failed: java.lang.SecurityException: ... | The app doesn't declare INTERNET. | Add <uses-permission android:name="android.permission.INTERNET" />. |
Initialize the SDK
Create the instance once, in Application.onCreate, so autocapture sees the whole app lifecycle. Pass the application context, then reuse the instance everywhere.
import android.app.Application
import com.amplitude.android.Amplitude
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
amplitude = Amplitude("YOUR_API_KEY", applicationContext) {
// Set options here, for example serverZone = ServerZone.EU
}
}
companion object {
lateinit var amplitude: Amplitude
}
}
Register the class in AndroidManifest.xml with <application android:name=".MyApplication" ...>.
If your project uses the EU data center, set serverZone = ServerZone.EU (import com.amplitude.core.ServerZone). In Java, build options with ConfigurationBuilder, which requires 1.27.0 or later. Don't use new Configuration(apiKey, context) from Java: it calls a deprecated constructor that can't set newer options such as autocapture.
The constructor returns immediately and finishes loading stored identity in the background. You can track events right away. The SDK queues them until setup finishes. To read a stored value, such as the device ID, wait for amplitude.isBuilt.await() in a coroutine first.
Fix initialization problems
| Symptom | Cause | Fix |
|---|---|---|
java.lang.ClassCastException naming android.app.Application | You passed an Activity or other non-application context. | Pass applicationContext (Kotlin) or getApplicationContext() (Java). |
java.lang.IllegalArgumentException: invalid configuration | The API key is blank, or flushQueueSize, flushIntervalMillis, or minIdLength is 0 or less. | Check the variable that holds your key. Build configurations often inject an empty value when a property is missing. |
Logcat warning: Amplitude instance '<name>' was replaced by a newer instance with the same name and is now inactive. | Your code created a second instance with the same instanceName, for example in an Activity. The older instance drops new events. | Create one instance in Application.onCreate and reuse it. Go to Run more than one instance. |
getDeviceId() returns null | The SDK hasn't finished loading stored identity. | Call amplitude.isBuilt.await() in a coroutine before you read the device ID. |
| No screen views or app lifecycle events | The default autocapture set contains only SESSIONS. | Add the features you want. Go to Autocapture. |
Send a first event
Track one custom event, then flush so you don't wait for the upload interval:
amplitude.track("Sign Up Clicked", mapOf("plan" to "starter"))
amplitude.flush()
Without flush(), the SDK uploads 30 seconds after the first queued event or as soon as the queue holds 30 events. It also uploads when the app goes to the background.
To see the upload result for each event, turn on debug logging and filter Logcat on the tag Amplitude:
amplitude.logger.logMode = Logger.LogMode.DEBUG // import com.amplitude.common.Logger
A successful upload logs Logged event with type: Sign Up Clicked, then Handle response, status: SUCCESS.
Fix upload problems
| Logcat message | Cause | Fix |
|---|---|---|
Skip event for opt out config. | Opt-out is on. The SDK drops every event. | Set amplitude.optOut = false, or remove optOut = true from your configuration. |
Event has an empty event type, dropping it. | The event name is empty. | Pass a non-empty event name. |
Handle response, status: BAD_REQUEST, error: Invalid API key: ..., then Invalid API key. Don't retry. | The key is wrong, or belongs to a project in the other data center. The SDK deletes the events. | Copy the key again and check serverZone. |
Handle response, status: FAILED, error: ..., repeating | The server returned an error, or a proxy at serverUrl returned a status other than 2xx, 400, 408, 413, or 429. The SDK keeps the events and retries with backoff, up to every 5 minutes. | Check the request host and status. Go to Check what the SDK sent. |
Request failed: java.net.UnknownHostException: ... or another Request failed: line | The request didn't reach the server: no network, a DNS failure, a blocked host, or a missing INTERNET permission. The SDK keeps the events and retries. | Check connectivity from the device. If a firewall or network blocks Amplitude, route events through a proxy. |
No Handle response line | The upload hasn't happened yet, or the device is offline. | Call amplitude.flush(). Check that the device is online. |
Confirm the event in Amplitude
- In Amplitude, open User Lookup and search for the device ID from
amplitude.getDeviceId(), or for the user ID you set. - Open the Activity tab and turn on Live event updates.
- Confirm that
Sign Up Clickedappears with theplanproperty.
If the event doesn't appear within a minute, follow Verify events. That page walks through each stage between track() and Amplitude's servers.
Next steps
- Identify signed-in users with
setUserId(), and callreset()when they sign out. - Choose which Autocapture events to collect.
- Add the custom events and properties your tracking plan needs. Go to Track events.
これは役に立ちましたか?
For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.