Android 10(target SDK 29) does not meet the requirements to access device identifiers

개발 블로그

회사에서 오래된 프로젝트 sdk업데이트를 진행했다.

22년 11월부터 targetSdkVersion이 29이하인 앱들은 플레이스토어에서 앱이 내려간다.

이 정책때문에 도대체 sdk업데이트를 몇 개의 프로젝트를 진행했는지 셀 수가 없다;;


그렇게 targetSdkVersion을 올리면서 해당 오류를 발견하였다.

Android 10(target SDK 29) does not meet the requirements to access device identifiers

android OS 10부터 TelephonyManager 클래스를 통해 제공되는 고유ID를 사용할 수 없게되었다.

기존의 프로젝트의 코드는 아래와 같았다.

 /**
     * UUID를 가져온다
     * @param mContext
     * @return
     */
    public static String getDevicesUUID(Context mContext){
        String tmDevice = null, tmSerial = null, androidId;
        UUID deviceUuid;

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            deviceUuid = UUID.randomUUID(); // UUID 대체코드
        } else {
            // 기존 UUID 로직
            TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
            tmDevice = "" + tm.getDeviceId();
            tmSerial = "" + tm.getSimSerialNumber();
            
            androidId = "" + android.provider.Settings.Secure.getString(mContext.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
            deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
        }

        String deviceId = deviceUuid.toString();

        return deviceId;
    }

이 부분을 아래와 같이 바꾸어주면된다.

/**
     * UUID를 가져온다
     * @param mContext
     * @return
     */
    public static String getDevicesUUID(Context mContext) {
        String tmDevice = null, tmSerial = null, androidId;
        UUID deviceUuid;
        String deviceId = null;

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // Q(29)이상일 때 UUID가져오는 방식
            deviceUuid = new UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L);
            try {
                // DRM - WIDEVINE_UUID(앱 삭제 or 데이터 삭제해도 변경x)
                MediaDrm mediaDrm = new MediaDrm(deviceUuid);
                deviceId = android.util.Base64.encodeToString(mediaDrm.getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID), 0).trim();

            } catch (UnsupportedSchemeException e) {
                e.printStackTrace();
            }
        } else { // 기존 UUID 조합해서 사용하는 방식
            if (ContextCompat.checkSelfPermission(EBookCaseApplication.Instance().getApplicationContext(), Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                TelephonyManager tm = (TelephonyManager) EBookCaseApplication.Instance().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
                tmDevice = "" + tm.getDeviceId();
                tmSerial = "" + tm.getSimSerialNumber();
                androidId = "" + Settings.Secure.getString(EBookCaseApplication.Instance().getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);

                deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
                deviceId = deviceUuid.toString();
            }
        }

        return deviceId;
    }

기존의 tmDevice와 tmSerial을 WedevineID로 대체한다.

앱을 삭제하고 재설치하여도 변하지않는 고유 ID값이다.