Android uncaught exception handler and ANR
While trying to install a custom exception handler to catch uncaught exceptions (crashes), I ended up writing
While trying to install a custom exception handler to catch uncaught exceptions (crashes), I ended up writing
On Android catching Java exceptions is easy via UncaughtExceptionHandler. Catching NDK crashes is a bit more convoluted. Since the native stack is probably corrupted, you want the crash handler to run on a separate process. Also, since the system might be in an unstable shape, don’t send the crash report to your web server or do anything fancy. Just write the crash report to a file, and on the next restart of the app, send to your web server and delete it from the disk. I ended up using jndcrash package for this.
If you are building an app that uses the user’s contact book then their certain gotchas to avoid.
If a country has a country code “+91”, then no other country will get a country code like “+912” or “+913”. This scheme ensures that numbers are inherently unambiguous.
Since most people don’t dial internationally, telecom systems implicitly assume a domestic call. So, someone dialing 612-555-1234 in the US is dialing “+1-612-555-1234”, while the same person in India is dialing “+91-612-555-1234”. Since international dialing would be more infrequent, telecoms require unique prefix numbers like “00” to distinguish whether someone is 612-555-1234 in their country or 0061-255-51234 in Austria. In some states, even the domestic area code is not explicitly required. So, a user might have stored “555-1234” as the phone number to which telecoms will implicitly prefix the user’s area code. And if the user wants to dial beyond their area, the telecom operator would require an additional “0” prefix to mark that it is an STD call. This localization has a massive implication regarding processing cleaning and normalizing phone numbers retrieved from the user’s contact book. Both country code and area code don’t contain “0”, and usually, that’s superfluous. So, while telecoms might be OK with calling or sending SMS to “0-612-555-1234”, they will treat a number like “91-0-612-555-1234” as incorrect.
USA, Canada, and many countries in the Caribbean share the “+1” telephony code. The carriers would treat calls or SMS as international, though. Italy and Vatican city share “+39”.
As the population grows in certain areas more than others, the codes reserved for other regions can get allotted to them. An example of that is the San Francisco Bay area, where the first 408 and then 669 was allocated on top of the existing 650 area codes to deal with the growing population.
You can never trust an incoming call or incoming SMS’s phone number. Therefore, the only way to verify that the user owns a phone number is by sending them a text message or making them a phone call.
You opened AVD Manager in Android Studio and tried to start an AVD, you got
“Emulator: Process finished with exit code 1”. Following are the steps to debug
this
$(dirname $(which android))/emulator -avd Nexus_5X_API_28_x86 ... PANIC: Broken AVD system path. Check your ANDROID_SDK_ROOT value [/opt/android_sdk]!
Let’s retry in the verbose mode to see a detailed error
$(dirname $(which android))/emulator -avd Nexus_5X_API_28_x86 -verbose ... Not a directory: /opt/android_sdk/system-images/android-28/google_apis/x86/
So, the system image is missing.
# List all possible system images. $(dirname $(which android))/bin/sdkmanager --list # List the images we are interested in. $(dirname $(which android))/bin/sdkmanager --list | ack google_apis | ack android-28 | ack x86 # Install the right one using $(dirname $(which android))/bin/sdkmanager --install 'system-images;android-28;google_apis;x86'
$(dirname $(which android))/emulator -avd Nexus_5X_API_28_x86 -verbose ... emulator:Probing program: /opt/android_sdk/tools/emulator64-x86 emulator:Probing program: /opt/android_sdk/tools/emulator-x86 PANIC: Missing emulator engine program for 'x86' CPU.
Turns out there is another version of the emulator installed in $(dirname $(dirname $(which android)))/emulator/emulator. And the emulator I was using is a stray one.
# This works $(dirname $(dirname $(which android)))/emulator/emulator -avd Nexus_5X_API_28_x86 -verbose
Die with me is a chat app which can be used only when the phone’s battery is below 5%.
Here is a fun way to use the app without draining your phone’s battery. Connect the phone via ADB or start Android emulator and fake the battery level to 4%.
sudo pip3 install adb-enhanced adbe battery level 4 # Set battery level to 4%
And now, you can use the app. After playing with the app, reset the battery level with,
adbe battery reset
Update: As of Mar 2022, I recommend everyone to use GitHub Actions
I maintain a somewhat popular Android developer tool (adb-enhanced). The tool is written in Python, supporting both Python 2 and 3. Testing the tool requires both Python runtime as well a running Android emulator. I, initially, used Travis CI for setting up continuous testing of this tool. Later, I felt that Travis CI was too slow and when I came across Circle CI, I decided to give it a try. As of now, both Travis and Circle CI are used for testing. Here is what I learned from my experience.
WebView debugging can be enabled via “WebView.setWebContentsDebuggingEnabled(true)”. Leaving WebView debugging enabled in production Android apps is a bad idea. Anyone who gets hold of the unlocked phone can access the app’s data forever.
android.app.Fragment
), they have been deprecated and can trigger version-specific bugs. Use the support library fragments (android.support.v4.app.Fragment
) instead.FragmentTransaction#commit()
executes, the user gets a phone call and your activity is backgrounded and destroyed.Activity#finish()
right before FragmentTransaction#commit.FragmentTransaction#commit()
, check that the activity has not been destroyed –Activity#isDestroyed()
should return false
.FragmentTransaction#commit()
executes, the user gets a phone call and your activity is backgrounded and paused.getSupportFragmentManager()
is invoked. The common cause of this is when a new fragment has to be added in response to a user action and the user immediately backgrounds the app, again, say due to a phone call, after clicking the button before getSupportFragmentManager()
is invoked. Another common case is where an AsyncTask which will call getSupportFragmentManager()
in onPostExecute and while the task is engaged in the background processing (doInBackground), the activity is destroyed.getSupportFragmentManager()
is being invoked in the Activity, check if it’s null. If it is being invoked inside a Fragment check if isAdded()
of the Fragment returns true before calling this.commitNow()
or hook into FragmentManager.FragmentLifecycleCallbacks#onFragmentAttached()
. I will admit this I haven’t found a simpler fix for this. And this issue is definitely an edge case.MyFragment myFragment = new MyFragment(); myFragment.setArguments(mBundle); return myFragment;
The Fragment code should then use the getArguments() method to fetch the arguments. In fact, I would recommend a Builder pattern to hide all this complexity.
Consider this complete example,
public class MyFragment { private static final String KEY_NAME = "name"; public static class MyFragment.Builder { private final Bundle mBundle = new Bundle(); public Builder setName(String username) { mBundle.putInt(KEY_NAME, clickCount); return this; } public MyFragment build() { MyFragment myFragment = new MyFragment(); // Set the username myFragment.setArguments(mBundle); return myFragment; } } public MyFragment() { // Get the user name @Nullable String username = getArguments() != null ? getArguments().getString(KEY_NAME, null) : null; } } if (!isDestroyed()) { // Create the Fragment MyFragment myFragment = new MyFragment().Builder().setName(username).build(); // Add the Fragment FragmentTransaction ft = getSupportFragmentManager().beingTransaction(); ft.add(myFragment); ft.commitNowAllowingStateLoss(); }
isAdded()
, if it returns true, it is safe to perform UI modifications, if it returns false, then your Fragment has been detached from the activity either because it has been removed or because the host (Fragment/Activity) is being destroyed.MyFragmentListener
) which the holding activity/Fragment should implement. In Fragment#onCreateView()
get the host via getHost()
, cast it to MyFragmentListener
, and store it in the instance variable of your Fragment class. Set that instance variable to null
in Fragment#onDestroyView()
. Now, you can invoke callbacks on this MyFragmentListener
instance variable.FragmentTransaction#addToBackStack(backStackStateName)
while adding the Fragment via FragmentTransaction
and remove it while removing it. Removal from the back stack is a bit more nuanced. Note that, manual removal of a fragment from the back stack is not required in Activity#onBackPressed()
as long as your Activity inherits from FragmentActivity.
SupportFragmentManager manager = getSupportFragmentManager(); FragmentManager.BackStackEntry entry = manager.getBackStackEntryAt(manager.getBackStackEntryCount() - 1); if (backStackStateName.equals(entry.getName())) { manager.popBackStack(backStackStateName, FragmentManager.POP_BACK_STACK_INCLUSIVE); } }
A JPEG file can have Exif metadata which can provide the rotation/translation field information for a raw JPEG image. So, a landscape raw JPEG image could actually be a portrait because it’s EXIF orientation could be set to ORIENTATION_ROTATE_90, the best way to handle such scenarios is to either use a library like Picasso or Glide or at least learn from them. Here is a piece of code from Picasso which loads a JPEG as an in-memory bitmap and performs the right translation/rotation.
// Get the orientation ExifInterface exifInterface = new ExifInterface(imageFilePath); int exifOrientation = exifInterface.getAttributeInt(TAG_ORIENTATION, ORIENTATION_NORMAL) // Take the source of these methods from // https://github.com/square/picasso/blob/31779ac2cb971c4534cc17bd437fab1aa0083d3d/picasso/src/main/java/com/squareup/picasso3/BitmapHunter.java#L625-L659 int exifRotation = getExifRotation(exifOrientation); int exifTranslation = getExifTranslation(exifOrientation); Matrix matrix = new Matrix(); if (exifRotation != 0) { matrix.preRotate(exifRotation); } if (exifTranslation != 1) { matrix.postScale(exifTranslation, 1); } // Now use this matrix to create a new Bitmap from the existing Bitmap