I was experimenting with CryptoPP library and I noticed it crashes in some cases, on investigating further, I saw that when it threw an exception, on linux it caught it too , but on android, it doesnt. So i made a very simple sanity check, here it is :
void random_test() {
LOGD("EXCEPTION : THROW ");
throw Exception();
}
And then I call it so :
try {
random_test();
} catch (Exception e ) {
LOGD("EXCEPTION CAUGHT");
}
It doesnt go to Exception caught.
This is the output from logcat:
2022-07-07 01:54:23.156 24102-24186/ D/JNIpart: EXCEPTION : THROW
2022-07-07 01:54:23.157 24102-24186/ E/libc++abi: terminating with uncaught exception of type cv::Exception
Then I tried this
try {
random_test();
} catch (...) {
LOGD("EXCEPTION CAUGHT");
}
And this catches the exception.
Now in CryptoPP library:
https://github.com/weidai11/cryptopp/blob/9ea66ce4d97d59d61e49c93f5af191107ebd1925/asn.cpp#L559
This is where exception is thrown, and when we try to catch it like this :
catch (const BERDecodeErr e)
{
LOGD("QRCPP : CATCH THE EXCEPTION FFS %s ", e.what());
}
And the code still crashes, even with this code :
catch (...)
{
LOGD("QRCPP : CATCH THE EXCEPTION FFS %s ", e.what());
}
The most common cause is exception types that are missing key functions. https://developer.android.com/ndk/guides/common-problems#rttiexceptions_not_working_across_library_boundaries
RTTI behaves differently in a plugin environment like JNI than it does in a typical executable. Without key functions, type info may not merge correctly depending on your exact library load order and symbol use. If they do not merged, they are different types according to the C++ ABI. Adding a key function to your classes ensures that a single definition of the typeinfo will be emitted and that no merging is required.
https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vague-vtable
https://itanium-cxx-abi.github.io/cxx-abi/abi.html#rtti
Some platforms do not follow the C++ ABI, and allow two types to be equal if their type names match (as defined by strcmp). This can result in false positives, so Android follows the spec explicitly. aiui Apple does too. GNU is the only platform I know of that doesn't.
Related
I'm using OpenCV(4.2.0) in an android project. When calling some opencv methods from android project's C++ files, opencv throws CV_Error and the application terminates/crashes. Instead of the crash I want to handle the exception and resume the program. Is there anyway to catch those exceptions in the projects' C++ file? I have tried the following and it doesn't work.
try {
......
} catch (...) {
}
Here is the stack trace from crashlytics
For some reason the CV_error #define CV_Error( code, msg ) cv::error( code, msg, CV_Func, __FILE__, __LINE__ ) isn't catchable in android project owned C++ files. I have even tried the following just to check out. The catch block isn't executed.
try {
CV_Error(Error::StsBadArg, "Quadrangle is nonconvex or degenerated.");
} catch (...) {
// not catched
}
Just adding the answer if someone else also bumps into the same issue.
The issue is somehow related to ndkVersion used in the project. After upgrading the ndkVersion to 22.1.7171670, CV_Error exceptions were catched successfully in the android project.
So the solution is to add the following line in build.gradle file of the app.
ndkVersion '22.1.7171670'
android {
defaultConfig {
.......
ndkVersion '22.1.7171670'
}
}
1) I implement firebase crashlytics in my react native project.
2) I used below npm
npm install #react-native-firebase/app#alpha
react-native link #react-native-firebase/app
npm install #react-native-firebase/crashlytics#alpha
react-native link #react-native-firebase/crashlytics
3) I can do crash app by programmatically using
async forceCrash() {
firebase.crashlytics().crash();
await firebase.crashlytics().setAttributes({ something:
"something" });
firebase.crashlytics().log("A woopsie is incoming :(");
firebase.crashlytics().recordError(new Error("Error Log"));
}
but i can't error log report in firabase console.
Please give me your value able suggestion. where i am wrong.
React native is not officially supported but developers have been able to use crashlytics with React native. From your snippet of code, it seems that you are try trying make a test crash and send a non-fatal exception at the same time.
If you want to see logs on your crash report, the code should look something like this:
Crashlytics.log("Crash occurred! Bailing out...");
Make sure that you set these logs before your app crashes.
If you want to send non-fatal exception:
try {
throw new NullPointerException("It is Pointer No-Fatal Error");
} catch (Exception e) {
Crashlytics.logException(e);
// handle your exception here!
}
Logs will also appear with non-fatal exceptions as long as they were set before the exception occurs.
In which SDK, jar this class can be found? I need to check at runtime if an exception is an instance of it.
I know this class can be found in AOSP sources. But this doesn't help much in runtime.
That is a hidden class in the Android SDK (see the Android 8.1 edition).
You can download the android sdk source, see this link:
Android SDK source code
With regards to checking the exception type at runtime, you can do the following (in Kotlin - transcribe to Java if required):
fun doSomething() {
try {
// Do something that may cause an exception
} catch (ex: KeyStoreException) {
}
}
or:
fun checkType() {
try {
// Something that could throw an exception
} catch(ex: Exception) {
when (ex) {
is KeyStoreException -> {
// Handle
}
}
}
}
This is a hidden class in Android . This can only be accessed by the framework or below layers and system apps , cannot be used by third party applications . You can check out the defination in the below link .
http://androidxref.com/8.0.0_r4/xref/frameworks/base/keystore/java/android/security/KeyStoreException.java.
There are 3 such classes in Android . All the 3 are under different packages and used in different context .
http://androidxref.com/8.0.0_r4/search?q=&defs=&refs=&path=%22KeyStoreException.java%22&hist=&project=art&project=bionic&project=bootable&project=build&project=cts&project=dalvik&project=developers&project=development&project=device&project=docs&project=external&project=frameworks&project=hardware&project=kernel&project=libcore&project=libnativehelper&project=packages&project=pdk&project=platform_testing&project=prebuilts&project=sdk&project=system&project=test&project=toolchain&project=tools
When I perform a throw command in my native library, my Android application crashes. Does Android not support these calls? It never reaches the catch.
try
{
__android_log_print(ANDROID_LOG_ERROR, "nativeLib", "throw");
throw;
}
catch (...) {
__android_log_print(ANDROID_LOG_ERROR, "nativeLib", "catch");
}
I recently switched from gnustl_shared to c++_shared, I'm not sure if this has anything to do with my problem.
Developer guides say that gnustl_shared has exceptions enabled by default, but c++_shared does not. I have included the -fexceptions flag as describe in the guide.
https://developer.android.com/ndk/guides/cpp-support.html
This is expected behavior. From the C++ standard:
If no exception is presently being handled, executing a throw-expression with no operand calls terminate()(15.5.1).
If you want it to catch you need to throw something:
try
{
__android_log_print(ANDROID_LOG_ERROR, "nativeLib", "throw");
throw new std::exception();
}
catch (...) {
__android_log_print(ANDROID_LOG_ERROR, "nativeLib", "catch");
env->ExceptionCheck();
}
It looks like this is a known issue:
https://developer.android.com/ndk/guides/cpp-support.html
Compatibility
The NDK's libc++ is not stable. Not all the tests pass, and the test suite is not comprehensive. Some known issues are:
•Using c++_shared on ARM can crash when an exception is thrown.
•Support for wchar_t and the locale APIs is limited.
Has this severe error been established as a official bug within the Android developer community? If not, it should. It is really a major, major flaw in the design. Catastrophic.
I'm trying to split my application into two modules in android studio, a java library and a normal phone & tablet module, with the phone module depending on the java library. I added the json library to my java library module this way:
dependecies {
compile 'org.json:json:20160212'
}
And then I added the dependency on the library module to the phone module this way:
dependencies {
provided project(':libraryModule')
}
Now I have method in the java library throwing a JSONException:
public JSONObject fun() throws JSONException {
JSONObject obj = new JSONObject();
obj.put("some","value");
return obj;
}
I'm calling that method in a try-catch-block in my phone module:
public JSONObject boo() {
MyObject my = new MyObject();
try {
return my.fun();
} catch (JSONException e) { //Exception works here
e.printStackTrace();
}
return null;
}
Android Studio shows me on MouseOver within boo() that the fun() method throws an exception but it also tells me
Exception 'org.json.JSONException' is never thrown in the corresponding try block'
but catching the parent class of JSONException, namely RuntimeException works. Furthermore Android Studio shows me no hint or error when I delete the try catch block entirely, which it did when I hadn't yet distributed the code into two modules via 'Unhandled Exception' hint.
Note:
I intend to use the java library in a different non-android project.
Question:
Is this an Android Studio issue and I shouldn't worry about it or do I have an entirely different problem, or am I just missing something here?