latly I changed to targetSdkVersion 26
from targetSdkVersion 21
.
By doing so several issues arise.
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
cant get ANDROID_ID
when my project
targetSdkVersion's 21
everthing is ok.
Please help thank you very much.
May be the issue is you don't provide run time permissions.Here is the tutorial