1

I am getting few NoClassDefFoundError in my Android mobile app only from specific devices of Samsung and QMobile.

Will adding the below code detect the error?

  //Check for Class not found error for Samsung 4.2.2 devices case
  try {
      Class.forName("android.support.v7.internal.view.menu.MenuBuilder");
  } catch (ClassNotFoundException e) {
      // Handle Exception by displaying an alert to user to update device firmware.
  }catch (NoClassDefFoundError e){
      // Handle Exception by displaying an alert to user to update device firmware.
  }

What is the best way to detect a NoClassDefFoundError proactively without ending up in app crash?

Edit:

The documentation says that the method throws the below exceptions and it does not mentions anything about NoClassDefFoundError.

Throws:
LinkageError - if the linkage fails
ExceptionInInitializerError - if the initialization provoked by this method fails
ClassNotFoundException - if the class cannot be located

I can rephrase the question like

Will Class.forName(String)throw NoClassDefFoundError or How can I check if a class has a "Def"?

brainless
  • 5,698
  • 16
  • 59
  • 82
  • 1
    You are handling it in the catch block, it should not crash – Soumitri Pattnaik Mar 08 '16 at 10:51
  • @SoumitriPattnaik I am not surrounding issue causing statement with try/catch block. I am just trying to trigger the exception proactively and check if the app continues to run, will it crash. – brainless Mar 08 '16 at 10:55
  • if it is in a particular method, why don't do a `throws NoClassDefFoundError` and handle it in the calling method. I am just saying, there may be a better solution. – Soumitri Pattnaik Mar 08 '16 at 10:57
  • @SoumitriPattnaik The exception is happening in Android Library. I can't modify the source code of the android library. – brainless Mar 08 '16 at 11:13
  • Unclear what you're asking. You've posted code that answers your question completely. – user207421 Mar 08 '16 at 11:21
  • @EJP My question is that, whether the code I have posted will detect NoClassDefFoundError? Or is there a better way to do that. – brainless Mar 08 '16 at 11:23
  • I believe it will, there's no way `NoClassDefFoundError` could escape those catch. If it does, then your looking at the wrong source; that `NoClassDefFoundError` was thrown elsewhere. – Hadi Satrio Mar 08 '16 at 11:34
  • @ridsatrio I have updated the question. Please have a look. – brainless Mar 08 '16 at 16:37

1 Answers1

0

The try-catch is, I think, the best way to handle exceptions (which what NoClassDefFoundError essentially is).

But it might be wise to handle the things that caused this to happen rather than just complying with the effect; NoClassDefFoundError is an effect, there must be something you can do to prevent it from ever popping out.

If as you said it happened within a library then there's 2 things you can do:

  • Report it to the maintainer (if you feel like this is truly their error), or
  • Try to fix the cause rather than the effect (the exception). NoClassDefFoundError usually marks that you are missing something the library needs; be it another dependency to yet another library or perhaps something else.

Try to dig deeper into the library's documentations, you might figure something out of it.

Hadi Satrio
  • 4,272
  • 2
  • 24
  • 45