I'm new to Android Studio (SDK version 23), and trying to obtain ANDROID_ID by following the top answer in this post.
After importing the 'secure' class, when I try the suggested answer below, I get a "cannot resolve method getContext()" warning:
private String androidId = Secure.getString(getContext().getContentResolver(),Secure.ANDROID_ID);
I did a bit of digging around, and thought getApplicationContext() was worth a try:
private String androidId = Secure.getString(getApplicationContext().getContentResolver(),Secure.ANDROID_ID);
I don't get any warnings, and the app builds successfully. However if I try running it on a connected device, it crashes with the following error (summarized):
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/R$string;
at com.google.android.gms.measurement.zza.(Unknown Source)
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.R$string" on path: DexPathList[[zip file "/data/app/application.myproject/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
...
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
The app doesn't crash when I manually specify my androidId string:
private String androidId = "This works";
Any help would be greatly appreciated.