Unity Android app crashes when activated a few times with Android's SDK's startActivity(intent).

Hey all.

I have an Android Java application that activates a Unity game by creating and starting an intent. The Unity game has an initial small scene, and a second major one that holds the game itself. The problem is that after a few times that the Java application will start the Unity application, the Unity application will not longer be able to load the major scene, and will then crash.

The process I’m reproducing is as follows:

  1. Restart an Android Galaxy Tab.
  2. Start the native Java application.
  3. Tell the native Java application to run the Unity game by creating an intent with ‘PackageManager.getLaunchIntentForPackage(“some.package.name”)’ and running it with ‘startActivity(intent)’.
  4. Click a button in the first scene to load the second, major scene.
  5. Kill the game with Unity’s Application.Quit();
  6. Wait a minute to make sure Unity had enough time to shut down the game.
  7. repeat steps 3, 4, 5 and 6. After 2-5 times this happens, the Unity application will crash after step 4, when trying to load the second, major scene.

Info about the crash:

  • When the Unity application crashes, the Java application is still running.
  • Once the Unity application crashes once, it will continue to crash if I will try to start it again, even if I will kill the Java application and start again. restarting the tablet lets me run the Unity application again (for a few times till it crashes again).
  • Debug log shows that the application crashes after scripts have been initialized. (But not at a specific point).
  • It seems that some tablets tend to crash after 2-3 cycles, and some only after 5-7 cycles.

Things I’ve done:

  • Changed textures to RGBA16 format, which was said to be best for Android.
  • Split large images into smaller pieces. This seemed to make the game start properly a few more times.
  • Removed all unneeded assets. The APK is 17MB.
  • Restarted the tablet, removed widgets, and made sure that no applications are running besides the Java application.
  • Since I’m currently using Unity3d 3.4.1, I’ve tried compiling it on 3.5.2, but this had no effect.

It seems to me like a memory problem, but I suppose that shutting the application with Application.Quit() should be good enough, and there is no way that I know of to remove leftovers if there are any after the Unity application is closed.

I know that what I’m doing here is not too conventional, but I see no reason it shouldn’t work. (moreover, it seems to work with other Unity applications, but there is no clear difference between the problematic one and others).

Any help would be appreciated.

Thanks.

If unity app also created by you, I suggest you follow the way of RolfHarris.
You can find his thread here.

Using his technique I successfully created a game in unity3d and made it as a android library and contained it in Android Project.