Is it possible to have a RuntimeException 🚫 in your Android app and 𝗻𝗼𝘁 have it crash? 🤔
- Nikolay Miroshnychenko
- Mar 1, 2024
- 1 min read
Yes, it is!
We can do this in two ways:
1. Via Thread.setDefaultUncaughtExceptionHandler 🧵 which is a mechanism of Java’s Thread API. It allows us to catch an exception in the Thread that we have specified - if it was not caught beforehand.
2. Define a component in a separate process 🔄. Defining a component to run in a separate process allows it to be independent of other components that run in distinct processes. Hence, when you have a crash in one process - it will not affect others.
How can we have a component that runs in a separate process?
By specifying the 𝚊𝚗𝚍𝚛𝚘𝚒𝚍:𝚙𝚛𝚘𝚌𝚎𝚜𝚜 attribute in our manifest like so:
<𝚜𝚎𝚛𝚟𝚒𝚌𝚎
𝚊𝚗𝚍𝚛𝚘𝚒𝚍:𝚗𝚊𝚖𝚎=".𝙼𝚢𝚂𝚎𝚙𝚊𝚛𝚊𝚝𝚎𝙿𝚛𝚘𝚌𝚎𝚜𝚜𝚂𝚎𝚛𝚟𝚒𝚌𝚎"
𝚊𝚗𝚍𝚛𝚘𝚒𝚍:𝚙𝚛𝚘𝚌𝚎𝚜𝚜=":𝚜𝚎𝚙𝚊𝚛𝚊𝚝𝚎_𝚙𝚛𝚘𝚌𝚎𝚜𝚜" />
Such service will launch in a separate process, making it independent of your application.
There are also other benefits to having components run in separate processes like:
- Improved security 🔒
- Reduced memory usage 💾
- Reduced likelihood of the app / process being killed ☠️
Do you use separate processes in your apps?
Comments