0

latly I changed to targetSdkVersion 26 from targetSdkVersion 21. By doing so several issues arise.

  1. I have written a class and run but gose to throw new RuntimeException(e)

    The code is as follows

    package com.xxxxxxxxxx.android.webapp.util;
    
    import android.content.Context;
    import android.telephony.TelephonyManager;
    
    import com.xxxxxxxxxx.android.webapp.BuildConfig;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    
    public class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";
    
    public synchronized static String id(Context context) {
        if (sID == null) {
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation,context);
                sID = readInstallationFile(installation,context);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }
    
    private static String readInstallationFile(File installation,Context context) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }
    
    private static void writeInstallationFile(File installation,Context context) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = getUniqueID(context);   //UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
    
    private static String getUniqueID(Context context){
        String android_id = android.provider.Settings.Secure.getString(
                context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
    
        String eid = null;
        try {
            eid = Encryptor.encrypt("gignoGawaAppli", android_id );
    
            if (BuildConfig.uidEncryptionMd5) {
                eid = md5(eid);
            }
    
        }catch (Exception e){};
        return eid;
    }
    
    public static String md5(String s) {
        try {
            // Create MD5 Hash
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();
    
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                if ((0xff & messageDigest[i]) < 0x10) {
                    hexString.append("0" + Integer.toHexString((0xFF & messageDigest[i])));
                } else {
                    hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
                }
            }
            return hexString.toString();
    
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
    
    }
    

appeard on the console like this

Caused by: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method byte[] java.lang.String.getBytes() on a null object reference

  1. cant get ANDROID_ID

    when my project targetSdkVersion's 21 everthing is ok.

Please help thank you very much.

Devil10
  • 1,853
  • 1
  • 18
  • 22
iwahei0813
  • 61
  • 10
  • Try to check if(f !=null) inside readInstallationFile() function before byte[] bytes = new byte[(int) f.length()]; – Ankita Aug 07 '18 at 06:49
  • Do you ask for runtime permissions? You're trying to read/write some file to internal storage. Targeting Android 21 doesn't require run-time permissions. – Dmitriy Mitiai Aug 07 '18 at 06:51
  • - For targetVersion above 23, you need to add runtime permission check. https://developer.android.com/training/permissions/requesting#java – shizhen Aug 07 '18 at 06:58

2 Answers2

0

May be the issue is you don't provide run time permissions.Here is the tutorial

Umer Farooq
  • 168
  • 1
  • 12
0

「Caused by: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference」

This is because below code

String id = getUniqueID(context);   //UUID.randomUUID().toString(); 

returns a null pointer, which means method getUniqueID is not executed correctly. You need to debug this method to get more details.

shizhen
  • 12,251
  • 9
  • 52
  • 88