Sentry provides two Java SDK artifacts:
sentry — for plain Java or Kotlin applications and server-side frameworks.
sentry-android — for Android apps. Extends sentry with ANR detection, activity lifecycle tracking, network monitoring, and more.
Installation
Maven
Gradle (JVM)
Gradle (Android)
Spring Boot
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry</artifactId>
<version>7.0.0</version>
</dependency>
implementation 'io.sentry:sentry:7.0.0'
implementation 'io.sentry:sentry-android:7.0.0'
No manual Sentry.init() is required on Android if you use the auto-init feature. Add your DSN to AndroidManifest.xml:<application>
<meta-data
android:name="io.sentry.dsn"
android:value="https://examplePublicKey@o0.ingest.sentry.io/0" />
<meta-data
android:name="io.sentry.traces-sample-rate"
android:value="1.0" />
</application>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring-boot-starter-jakarta</artifactId>
<version>7.0.0</version>
</dependency>
Then add to application.properties:sentry.dsn=https://examplePublicKey@o0.ingest.sentry.io/0
sentry.traces-sample-rate=1.0
sentry.environment=production
Basic initialization
Java
Kotlin
Android (Kotlin)
import io.sentry.Sentry;
public class Application {
public static void main(String[] args) {
Sentry.init(options -> {
options.setDsn("https://examplePublicKey@o0.ingest.sentry.io/0");
options.setEnvironment("production");
options.setRelease("my-app@1.0.0");
options.setTracesSampleRate(1.0);
});
// Your application code here
}
}
import io.sentry.Sentry
fun main() {
Sentry.init { options ->
options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
options.environment = "production"
options.release = "my-app@1.0.0"
options.tracesSampleRate = 1.0
}
// Your application code here
}
For custom initialization (overrides manifest-based auto-init):import io.sentry.android.core.SentryAndroid
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
SentryAndroid.init(this) { options ->
options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
options.environment = "production"
options.release = BuildConfig.VERSION_NAME
options.tracesSampleRate = 1.0
options.profilesSampleRate = 1.0
}
}
}
Initialize Sentry in your main method (JVM) or Application.onCreate() (Android) before any other code runs. This ensures errors during startup are captured.
Capturing exceptions
try {
processOrder(cart);
} catch (Exception e) {
Sentry.captureException(e);
throw e;
}
try {
processOrder(cart)
} catch (e: Exception) {
Sentry.captureException(e)
throw e
}
Capture a message
Sentry.captureMessage("Inventory below threshold", SentryLevel.WARNING);
Sentry.captureMessage("Inventory below threshold", SentryLevel.WARNING)
Setting context
Sentry.configureScope(scope -> {
scope.setUser(new User());
scope.getUser().setId("42");
scope.getUser().setEmail("user@example.com");
scope.setTag("section", "checkout");
scope.setExtra("orderId", "12345");
});
Sentry.configureScope { scope ->
scope.user = User().apply {
id = "42"
email = "user@example.com"
}
scope.setTag("section", "checkout")
scope.setExtra("orderId", "12345")
}
ITransaction transaction = Sentry.startTransaction("checkout", "task");
ISpan dbSpan = transaction.startChild("db.query", "SELECT orders");
try {
List<Order> orders = orderRepository.findByUser(userId);
dbSpan.setData("row_count", orders.size());
} finally {
dbSpan.finish();
}
transaction.finish();
val transaction = Sentry.startTransaction("checkout", "task")
val dbSpan = transaction.startChild("db.query", "SELECT orders")
try {
val orders = orderRepository.findByUser(userId)
dbSpan.setData("row_count", orders.size)
} finally {
dbSpan.finish()
}
transaction.finish()
Spring Boot: @SentryTransaction
With the Spring Boot starter, annotate methods to create transactions automatically:
import io.sentry.spring.jakarta.tracing.SentryTransaction;
@Service
public class OrderService {
@SentryTransaction(operation = "task", name = "processOrder")
public void processOrder(Order order) {
// method body is wrapped in a Sentry transaction
}
}
Android-specific features
The sentry-android SDK includes capabilities beyond the standard Java SDK:
| Feature | Description |
|---|
| Crash reporting | Captures Java, Kotlin, and native (NDK) crashes. |
| ANR detection | Reports Application Not Responding events with a thread dump. |
| OOM detection | Detects out-of-memory terminations (experimental). |
| Slow/frozen frames | Tracks UI frame rates for performance monitoring. |
| Session tracking | Measures crash-free session and user rates. |
| Screenshot on crash | Optionally attaches a screenshot taken at the moment of a crash. |
| View hierarchy | Attaches a dump of the view hierarchy on crash. |
Enable screenshot and view hierarchy capture in your SentryAndroid.init():
SentryAndroid.init(this) { options ->
options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
options.isAttachScreenshot = true
options.isAttachViewHierarchy = true
}
Screenshots may capture sensitive user data. Review your privacy policy before enabling isAttachScreenshot in production.