Android get unique id

git: GitHub - gzu-liyujiang/Android_CN_OAID: A unique identification solution for Android devices, which can completely replace the closed-source solution of the unified SDK of the Mobile Security Alliance. Including the Open Anonymous Identifier (OAID) of domestic mobile phone manufacturers, the Android Advertising Identifier (AAID) of overseas mobile phone platforms, and also provides methods for obtaining common device identifiers such as IMEI/MEID, AndroidID, WidevineID, PseudoID, and GUID.

 use

in global build.gradle

allprojects {
    repositories {
        maven { url 'https://www.jitpack.io' }
    }
}

 app build.gradle中

    api 'com.github.gzu-liyujiang:Android_CN_OAID:4.2.4'
   // 在`Application#onCreate`里初始化,注意APP合规性,若最终用户未同意隐私政策则不要调用
    @Override
    public void onCreate() {
        super.onCreate();
        if (privacyPolicyAgreed) {
            DeviceIdentifier.register(this);
        }
    }

Get method

         // 获取IMEI,只支持Android 10之前的系统,需要READ_PHONE_STATE权限,可能为空
        DeviceIdentifier.getIMEI(this);
        // 获取安卓ID,可能为空
        DeviceIdentifier.getAndroidID(this);
        // 获取数字版权管理ID,可能为空
        DeviceIdentifier.getWidevineID();
        // 获取伪造ID,根据硬件信息生成,不会为空,有大概率会重复
        DeviceIdentifier.getPseudoID();
        // 获取GUID,随机生成,不会为空
        DeviceIdentifier.getGUID(this);
        // 是否支持OAID/AAID
        DeviceID.supportedOAID(this);
        // 获取OAID/AAID,同步调用
        DeviceIdentifier.getOAID(this);
        // 获取OAID/AAID,异步回调
        DeviceID.getOAID(this, new IGetter() {
            @Override
            public void onOAIDGetComplete(String result) {
                // 不同厂商的OAID/AAID格式是不一样的,可进行MD5、SHA1之类的哈希运算统一
            }

            @Override
            public void onOAIDGetError(Exception error) {
                // 获取OAID/AAID失败
            }
        });

obfuscation rules

-keep class repeackage.com.uodis.opendevice.aidl.** { *; }
-keep interface repeackage.com.uodis.opendevice.aidl.** { *; }
-keep class repeackage.com.asus.msa.SupplementaryDID.** { *; }
-keep interface repeackage.com.asus.msa.SupplementaryDID.** { *; }
-keep class repeackage.com.bun.lib.** { *; }
-keep interface repeackage.com.bun.lib.** { *; }
-keep class repeackage.com.heytap.openid.** { *; }
-keep interface repeackage.com.heytap.openid.** { *; }
-keep class repeackage.com.samsung.android.deviceidservice.** { *; }
-keep interface repeackage.com.samsung.android.deviceidservice.** { *; }
-keep class repeackage.com.zui.deviceidservice.** { *; }
-keep interface repeackage.com.zui.deviceidservice.** { *; }
-keep class repeackage.com.coolpad.deviceidsupport.** { *; }
-keep interface repeackage.com.coolpad.deviceidsupport.** { *; }
-keep class repeackage.com.android.creator.** { *; }
-keep interface repeackage.com.android.creator.** { *; }

Support situation

Manufacturer or brand system or framework
Huawei, Honor HMS Core 2.6.2+ 、Google Play Service 4.0+
Xiaomi (XiaoMi, Redmi, BlackShark) MIUI 10.2+、Google Play Service 4.0+
Vivo (VIVO, IQOO) Funtouch OS 9+、OriginOS 1.0+、Google Play Service 4.0+
Oppo (OPPO, Realme) ColorOS 7.0+、Google Play Service 4.0+
Samsung Android 10+、Google Play Service 4.0+
Lenovo ZUI 11.4+, Google Play Service 4.0+
ASUS Android 10+、Google Play Service 4.0+
Meizu Android 10+、Google Play Service 4.0+
OnePlus Android 10+、Google Play Service 4.0+
Nubia Android 10+、Google Play Service 4.0+
Coolpad CoolOS、Google Play Service 4.0+
Coosea Android 10+、Google Play Service 4.0+
I 易 (Turn) Freeme OS、Google Play Service 4.0+
Others (ZTE, HTC, Motorola, ...) SSUI、Google Play Service 4.0+

 OAID (Open Anonymous Device Identifier)

 

For other mobile phones, please follow the readMe at the bottom of the above URL

 I simply encapsulated it, I need to use my encapsulated

    //获取安卓ID,可能为空
    fun getAndroidId(context: Context): String? {
        return DeviceIdentifier.getAndroidID(context);
    }

    //获取IMEI,只支持Android 10之前的系统,需要READ_PHONE_STATE权限,可能为空
    fun getImei(context: Context): String {
        return DeviceIdentifier.getIMEI(context);
    }

    /**
     * 获取设备唯一ID
     */
    fun getUniqueDeviceId(context: Context): String {
        //oaid
        val supportedOAID = DeviceID.supportedOAID(context);
        if(supportedOAID){
            // 获取OAID/AAID,同步调用
            val oaid = DeviceIdentifier.getOAID(context);
            return oaid
        }
        //android Id
        val androidId = getAndroidId(context);
        if (!TextUtils.isEmpty(androidId)) {
            return androidId!!;
        }
        // 获取数字版权管理ID,可能为空
        val widevineID = DeviceIdentifier.getWidevineID();
        if (!TextUtils.isEmpty(widevineID)) {
            return widevineID!!;
        }
        // 获取伪造ID,根据硬件信息生成,不会为空,有大概率会重复
        val pseudoID = DeviceIdentifier.getPseudoID();
        if (!TextUtils.isEmpty(pseudoID)) {
            return pseudoID!!;
        }
        //串号
        val imei = getImei(context)
        if (!TextUtils.isEmpty(imei)) {
            return imei;
        }
        return "";

    }
    // 获取GUID,随机生成,不会为空 不会重复
    private fun getGuid(context: Context): String? {
        val guid = DeviceIdentifier.getGUID(context);
        return guid
    }
    // 获取UUID,随机生成,不会为空 不会重复
    private fun getUUID(context: Context): String {
        val uniqueID: String = UUID.randomUUID().toString()
        return uniqueID
    }

Guess you like

Origin blog.csdn.net/mp624183768/article/details/123606852