Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Settings.Secure.ANDROID_ID in favor of generated installationId #1455

Merged
merged 3 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

Breaking Changes:

* Remove Settings.Secure.ANDROID_ID in favor of generated installationId (#1455)

# 5.0.0-beta.1

* Fix: Activity tracing auto instrumentation for Android API < 29 (#1402)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ final class DefaultAndroidEventProcessor implements EventProcessor {

@TestOnly static final String PROGUARD_UUID = "proGuardUuids";
@TestOnly static final String ROOTED = "rooted";
@TestOnly static final String ANDROID_ID = "androidId";
@TestOnly static final String KERNEL_VERSION = "kernelVersion";
@TestOnly static final String EMULATOR = "emulator";
@TestOnly static final String SIDE_LOADED = "sideLoaded";
Expand Down Expand Up @@ -117,11 +116,6 @@ public DefaultAndroidEventProcessor(

map.put(ROOTED, rootChecker.isDeviceRooted());

String androidId = getAndroidId();
if (androidId != null) {
map.put(ANDROID_ID, androidId);
}

String kernelVersion = getKernelVersion();
if (kernelVersion != null) {
map.put(KERNEL_VERSION, kernelVersion);
Expand Down Expand Up @@ -896,40 +890,13 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf

private @Nullable String getDeviceId() {
try {
Object androidId = contextData.get().get(ANDROID_ID);

if (androidId != null) {
return (String) androidId;
}
return Installation.id(context);
} catch (Exception e) {
logger.log(SentryLevel.ERROR, "Error getting androidId.", e);
logger.log(SentryLevel.ERROR, "Error getting installationId.", e);
}
return null;
}

@SuppressWarnings("HardwareIds")
private @Nullable String getAndroidId() {
// Android 29 has changed and -> Avoid using hardware identifiers, find another way in the
// future
String androidId =
Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

// https://android-developers.googleblog.com/2011/03/identifying-app-installations.html
if (androidId == null
|| androidId.isEmpty()
|| androidId.toLowerCase(Locale.ROOT).contentEquals("9774d56d682e549c")) {
try {
androidId = Installation.id(context);
} catch (RuntimeException e) {
logger.log(SentryLevel.ERROR, "Could not generate device Id.", e);

return null;
}
}

return androidId;
}

private @Nullable String[] getProguardUUIDs() {
final AssetManager assets = context.getAssets();
// one may have thousands of asset files and looking up this list might slow down the SDK init.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ final class Installation {
@TestOnly static String deviceId = null;

@TestOnly static final String INSTALLATION = "INSTALLATION";

private static final Charset UTF_8 = Charset.forName("UTF-8");

private Installation() {}

/**
* Generates a random UUID and writes to a file to be used as an unique installationId. Reads the
* installationId if already exists.
*
* @param context the Context
* @return the generated installationId
* @throws RuntimeException if not possible to read nor to write to the file.
*/
public static synchronized String id(final @NotNull Context context) throws RuntimeException {
if (deviceId == null) {
File installation = new File(context.getFilesDir(), INSTALLATION);
final File installation = new File(context.getFilesDir(), INSTALLATION);
try {
if (!installation.exists()) {
deviceId = writeInstallationFile(installation);
Expand All @@ -37,8 +46,8 @@ public static synchronized String id(final @NotNull Context context) throws Runt

@TestOnly
static @NotNull String readInstallationFile(final @NotNull File installation) throws IOException {
try (RandomAccessFile f = new RandomAccessFile(installation, "r")) {
byte[] bytes = new byte[(int) f.length()];
try (final RandomAccessFile f = new RandomAccessFile(installation, "r")) {
final byte[] bytes = new byte[(int) f.length()];
f.readFully(bytes);
return new String(bytes, UTF_8);
}
Expand All @@ -47,8 +56,8 @@ public static synchronized String id(final @NotNull Context context) throws Runt
@TestOnly
static @NotNull String writeInstallationFile(final @NotNull File installation)
throws IOException {
try (OutputStream out = new FileOutputStream(installation)) {
String id = UUID.randomUUID().toString();
try (final OutputStream out = new FileOutputStream(installation)) {
final String id = UUID.randomUUID().toString();
out.write(id.getBytes(UTF_8));
out.flush();
return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.SentryTracer
import io.sentry.TransactionContext
import io.sentry.android.core.DefaultAndroidEventProcessor.ANDROID_ID
import io.sentry.android.core.DefaultAndroidEventProcessor.EMULATOR
import io.sentry.android.core.DefaultAndroidEventProcessor.KERNEL_VERSION
import io.sentry.android.core.DefaultAndroidEventProcessor.PROGUARD_UUID
Expand Down Expand Up @@ -263,7 +262,6 @@ class DefaultAndroidEventProcessorTest {
assertNotNull(contextData)
assertEquals("test", (contextData[PROGUARD_UUID] as Array<*>)[0])
assertNotNull(contextData[ROOTED])
assertNotNull(contextData[ANDROID_ID])
assertNotNull(contextData[KERNEL_VERSION])
assertNotNull(contextData[EMULATOR])
assertNotNull(contextData[SIDE_LOADED])
Expand Down