Skip to content

Commit 414699d

Browse files
committed
ref(android): Update targetSdk to API 36 (Android 16)
Updates targetSdk and compileSdk to API Level 36 for Android 16 support. Breaking changes addressed: - Created ThreadUtil helper for version-aware thread ID retrieval - Migrated from deprecated Thread.getId() to Thread.threadId() with backward compatibility - Updated sample app to use OnBackPressedCallback instead of deprecated onBackPressed() The scheduleAtFixedRate behavior change is acceptable for our performance collection use case.
1 parent f064536 commit 414699d

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-13
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ springboot2 = "2.7.18"
3434
springboot3 = "3.5.0"
3535
springboot4 = "4.0.0"
3636
# Android
37-
targetSdk = "34"
38-
compileSdk = "34"
37+
targetSdk = "36"
38+
compileSdk = "36"
3939
minSdk = "21"
4040
spotless = "7.0.4"
4141
gummyBears = "0.12.0"

sentry-android-core/api/sentry-android-core.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,7 @@ public abstract interface class io/sentry/android/core/util/AndroidLazyEvaluator
659659
public abstract fun evaluate (Landroid/content/Context;)Ljava/lang/Object;
660660
}
661661

662+
public final class io/sentry/android/core/util/ThreadUtil {
663+
public static fun getThreadId (Ljava/lang/Thread;)J
664+
}
665+

sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public void onConfigurationChanged(@NotNull Configuration newConfig) {
9898
executeInBackground(() -> captureConfigurationChangedBreadcrumb(now, newConfig));
9999
}
100100

101+
@SuppressWarnings("deprecation")
101102
@Override
102103
public void onLowMemory() {
103104
// we do this in onTrimMemory below already, this is legacy API (14 or below)

sentry-android-core/src/main/java/io/sentry/android/core/internal/util/AndroidThreadChecker.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.os.Handler;
44
import android.os.Looper;
55
import android.os.Process;
6+
import io.sentry.android.core.util.ThreadUtil;
67
import io.sentry.protocol.SentryThread;
78
import io.sentry.util.thread.IThreadChecker;
89
import org.jetbrains.annotations.ApiStatus;
@@ -26,12 +27,12 @@ private AndroidThreadChecker() {
2627

2728
@Override
2829
public boolean isMainThread(final long threadId) {
29-
return Looper.getMainLooper().getThread().getId() == threadId;
30+
return ThreadUtil.getThreadId(Looper.getMainLooper().getThread()) == threadId;
3031
}
3132

3233
@Override
3334
public boolean isMainThread(final @NotNull Thread thread) {
34-
return isMainThread(thread.getId());
35+
return isMainThread(ThreadUtil.getThreadId(thread));
3536
}
3637

3738
@Override

sentry-android-core/src/main/java/io/sentry/android/core/performance/ActivityLifecycleSpanHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.sentry.SpanDataConvention;
99
import io.sentry.SpanStatus;
1010
import io.sentry.android.core.AndroidDateUtils;
11+
import io.sentry.android.core.util.ThreadUtil;
1112
import java.util.concurrent.TimeUnit;
1213
import org.jetbrains.annotations.ApiStatus;
1314
import org.jetbrains.annotations.NotNull;
@@ -129,7 +130,8 @@ public void clear() {
129130
}
130131

131132
private void setDefaultStartSpanData(final @NotNull ISpan span) {
132-
span.setData(SpanDataConvention.THREAD_ID, Looper.getMainLooper().getThread().getId());
133+
span.setData(
134+
SpanDataConvention.THREAD_ID, ThreadUtil.getThreadId(Looper.getMainLooper().getThread()));
133135
span.setData(SpanDataConvention.THREAD_NAME, "main");
134136
span.setData(SpanDataConvention.CONTRIBUTES_TTID, true);
135137
span.setData(SpanDataConvention.CONTRIBUTES_TTFD, true);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.sentry.android.core.util;
2+
3+
import android.os.Build;
4+
import org.jetbrains.annotations.ApiStatus;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/**
8+
* Utility class for thread-related operations that handles Android version compatibility.
9+
*
10+
* <p>In Android 16+, {@link Thread#getId()} is deprecated in favor of {@link Thread#threadId()}.
11+
* This class provides a version-aware method to retrieve thread IDs that works across all Android
12+
* versions.
13+
*/
14+
@ApiStatus.Internal
15+
public final class ThreadUtil {
16+
17+
private ThreadUtil() {
18+
// Utility class, no instances
19+
}
20+
21+
/**
22+
* Gets the thread ID in a way that's compatible across Android versions.
23+
*
24+
* <p>Uses {@link Thread#threadId()} on Android 14 (API 34) and above, and falls back to {@link
25+
* Thread#getId()} on older versions.
26+
*
27+
* @param thread the thread to get the ID for
28+
* @return the thread ID
29+
*/
30+
@SuppressWarnings("deprecation")
31+
public static long getThreadId(final @NotNull Thread thread) {
32+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
33+
return thread.threadId();
34+
} else {
35+
return thread.getId();
36+
}
37+
}
38+
}

sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/ProfilingActivity.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.os.Bundle
44
import android.view.View
55
import android.widget.SeekBar
66
import android.widget.Toast
7+
import androidx.activity.OnBackPressedCallback
78
import androidx.appcompat.app.AppCompatActivity
89
import androidx.recyclerview.widget.LinearLayoutManager
910
import io.sentry.ITransaction
@@ -24,6 +25,21 @@ class ProfilingActivity : AppCompatActivity() {
2425

2526
override fun onCreate(savedInstanceState: Bundle?) {
2627
super.onCreate(savedInstanceState)
28+
29+
onBackPressedDispatcher.addCallback(
30+
this,
31+
object : OnBackPressedCallback(true) {
32+
override fun handleOnBackPressed() {
33+
if (profileFinished) {
34+
isEnabled = false
35+
onBackPressedDispatcher.onBackPressed()
36+
} else {
37+
Toast.makeText(this@ProfilingActivity, R.string.profiling_running, Toast.LENGTH_SHORT)
38+
.show()
39+
}
40+
}
41+
},
42+
)
2743
binding = ActivityProfilingBinding.inflate(layoutInflater)
2844

2945
binding.profilingDurationSeekbar.setOnSeekBarChangeListener(
@@ -156,14 +172,6 @@ class ProfilingActivity : AppCompatActivity() {
156172
else -> fibonacci(n - 1) + fibonacci(n - 2)
157173
}
158174

159-
override fun onBackPressed() {
160-
if (profileFinished) {
161-
super.onBackPressed()
162-
} else {
163-
Toast.makeText(this, R.string.profiling_running, Toast.LENGTH_SHORT).show()
164-
}
165-
}
166-
167175
private fun getProfileDuration(): Float {
168176
// Minimum duration of the profile is 100 milliseconds
169177
return binding.profilingDurationSeekbar.progress / 10.0F + 0.1F

0 commit comments

Comments
 (0)