Hi. I posted this to the Support forum about a week ago without any replies, but just realized it’s probably more appropriate here:
I have to integrate a Unity (+Vuforia) app with an existing Android project, using the new Gradle build system. To do this, I build the Unity project for Android, transplant the activity in AndroidManifest.xml and pull out the relevant files into my project:
Unity’s release classes.jar (Unity classes, including native activity)
assets/bin/Data
libs/armeabi-v7a
I can launch the Unity activity from my app using the usual startActivityForResult call. When I exit the Unity activity (using Application.quit() or Activity.finish() via the java bridge) it closes the activity and returns as expected, but then crashes:
Has anyone successfully used Unity with the new gradle build system? Our release is blocked by this, keen to hear from anyone with experience combining the two.
Can you try selecting “Google Android Project” in the build window, and exporting the project by clicking “Export”? This makes sure you are not missing any necessary files.
I tried selecting “Google Android Project” and copying the output from Temp/StagingArea as before, same crash. The logged call stack wasn’t particularly helpful:
The sample project I posted it has the same behaviour if you would like to try to instrument the issue. I included a script “update.sh” that I use to perform the copy step.
Nonono, don’t copy from StagingArea -
When selecting Google Android Project and clicking Export, you are asked for an output folder - use this folder, this is a ready Eclipse project.
To clarify, I can get the generated project (just the UnityPlayerNativeActivity) to run with no crashes with the same Gradle setup as in the project I posted. The problem arises when I try to integrate it with another activity, on return from an Application.Quit() or Activity.finish().
Unity deliberately kills the app process on exit - in onDestroy there is a call to UnityPlayer.quit(), which eventually calls Process.killProcess(Process.myPid()).
I can only guess why this has been put in - presumably it was difficult to clean up the native activity correctly. But this completely breaks interoperability with other activities, one of the foundations of Android apps.
Disappointed to hear that, I hope this does get fixed in a future version.
In the meantime I found a workaround. In AndroidManifest.xml, add android:process=“:foo” to the Unity native activity tag. This will force Unity’s activity to launch in a separate process. When the process ends it is terminated as usual but activities outside the process will continue running. This means you can’t get a result back from a Unity activity, but otherwise works as expected.