0

I have an Android game (built in cocos2dx) that was recently published and Google is notifying me of a startup crash with the following exception:

java.lang.ExceptionInInitializerError
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1409)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.UnsatisfiedLinkError: Couldn't load game: findLibrary returned null
    at java.lang.Runtime.loadLibrary(Runtime.java:429)
    at java.lang.System.loadLibrary(System.java:554)
    ...

It appears to be happening in this method:

static {
    System.loadLibrary("game");
}

I've done some research and it appears that this can be a result of a number of problems, even the app getting corrupted somehow during the installation process. I wanted to display a dialog to the user and ask them to redownload the app. I can put a try/catch around the System.loadLibrary call, but am not sure how to do anything without a context. I don't think I have one yet as this is getting called before onCreate() is getting called in my main Activity.

Any ideas on how I would notify the user to try redownloading?

Thanks!

joelpoloney
  • 419
  • 4
  • 13

2 Answers2

0

Build another Activity for your dialog. In your manifest for that Activity specify

android:theme="@android:style/Theme.Dialog"

This will make your Activity have the appearance of a Dialog but give you more flexibility. You can use AlertDialog.Builder in this Activity to display your Dialog then do whatever you need to do from there. You need a Context for a Dialog

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Do I have to actually create the Activity in code too? How would I go about calling it to invoke the AlertDialog.Builder code? – joelpoloney Mar 12 '13 at 00:10
  • If I understand you correctly, you want this to be the first screen that shows up, right? – codeMagic Mar 12 '13 at 00:12
  • The problem I'm having is that the System.loadLibrary() call is throwing the UnsatisfiedLinkError exception (which is possible). From what I've read, one reason for this is a broken/corrupted install from the Google Play store. I want to display a message to the user about this and redirect them to reinstall. – joelpoloney Mar 12 '13 at 00:18
  • So, yes, it would be the first screen that shows up, but I don't have a context here. This exception is thrown before the main activity gets created. – joelpoloney Mar 12 '13 at 00:25
  • If you want this to pop up every time then this is probably how I would do it then you will have a context – codeMagic Mar 12 '13 at 00:27
  • How do I launch in to the Activity without a Context? – joelpoloney Mar 12 '13 at 01:02
  • You would make that `Activity` your `launcher` instead of what you have now. Then, after the user does what they need, they are taken to the `MainActivity`. Unless I am missing something here – codeMagic Mar 12 '13 at 01:05
  • I'm trying to load some C++ libraries for use with JNI (the System.loadLibrary() call). That has to happen in a static {} block and is done before anything ever happens in the app. That's the part that's throwing the exception. I need to catch the exception here and redirect them to try to redownload the app. Problem is, this happens before onCreate() is called on my main activity and I don't have any context yet. – joelpoloney Mar 12 '13 at 01:15
  • I understand that, I've never had to mess with it but does it HAVE to be the first thing that happens? You have to have a `context` for a `Dialog`. So I would suggest my first idea and then call the `static method` to load the `libraries`. Or load them from a `static` method in an `AsyncTask` of your `MainActivity` and show a `Progress Dialog`. If there are errors, catch them and you will have a `context` to display the `Dialog` since `onPostExecute()` runs on the ~UI Thread` – codeMagic Mar 12 '13 at 01:23
0

I had same issue when I've changed the library name. I guess you are programming under NDK. So in your CMakeLists.txt file check if you have like this:

cmake_minimum_required(VERSION 3.4.1)
project(project_name)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
...
add_library(*game* SHARED gl_code.cpp)
...
target_link_libraries(*game* ...)

PS: without ' * '

PS2: I don't know about cocos2dx but I saw on their page that it's C++, so you are programming in native, that error in loadLibrary means that the Java cannot find the module 'game'.

Stoica Mircea
  • 782
  • 10
  • 22