Search code examples
androidadbandroid-8.0-oreo

ANDROID_ID different in abd and in code on API 26


Using API 25 on an emulator:

adb shell settings put secure android_id 8af8770a27cfd182
adb shell settings get secure android_id //gives 8af8770a27cfd182
Secure.getString(context.getContentResolver(), Secure.ANDROID_ID) //gives 8af8770a27cfd182

Using API 26 on an emulator:

adb shell settings put secure android_id 8af8770a27cfd182
adb shell settings get secure android_id //gives 8af8770a27cfd182
Secure.getString(context.getContentResolver(), Secure.ANDROID_ID) //gives 6e4f84f5513b80e1

I've read about the changes of ANDROID_ID between API 25 and 26 but why is adb and code giving me different results for it?

UPDATE:

Created a simple app to take out the complexity of the old one. New app has a single main activity with a button on it:

package com.example.diolaj01.testandroidid;

import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

protected void GetAndroidId(View view){
    Log.e("myDebugTag", Settings.Secure.getString(view.getContext().getContentResolver(),
            Settings.Secure.ANDROID_ID));

}
}

When clicking the button on a device with API 25 I get the same value as when requesting the ANDROID_ID from adb:

adb shell settings get secure android_id

If I change the android_id I still get the updated one in both the console using the button and using adb.

When clicking the button on a device with API 26 I get a different value than the one I get when using adb. If I change the android_id using adb I'll get the updated value when using adb but not using the button.


Solution

  • From https://developer.android.com/about/versions/oreo/android-8.0-changes.html#privacy-all

    Privacy Android 8.0 (API level 26) makes the following privacy-related changes to the platform.

    The platform now handles identifiers differently.

    For apps that were installed prior to an OTA to a version of Android 8.0 (API level 26) (API level 26), the value of ANDROID_ID remains the same unless uninstalled and then reinstalled after the OTA. To preserve values across uninstalls after OTA, developers can associate the old and new values by using Key/Value Backup.

    For apps installed on a device running Android 8.0, the value of ANDROID_ID is now scoped per app signing key, as well as per user. The value of ANDROID_ID is unique for each combination of app-signing key, user, and device. As a result, apps with different signing keys running on the same device no longer see the same Android ID (even for the same user).

    The value of ANDROID_ID does not change on package uninstall or reinstall, as long as the signing key is the same (and the app was not installed prior to an OTA to a version of Android 8.0).

    The value of ANDROID_ID does not change even if a system update causes the package signing key to change.

    On devices shipping with Google Play services and Advertising ID, you must use Advertising ID. A simple, standard system to monetize apps, Advertising ID is a unique, user-resettable ID for advertising. It is provided by Google Play services.

    Other device manufacturers should continue to provide ANDROID_ID.

    The shell user is different from your app's user id - thus the difference.