Integrating with existing Gradle project

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:

D/dalvikvm( 5197): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodUnblockStreaming:I
D/dalvikvm( 5197): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodProcess:IL
D/dalvikvm( 5197): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodInitJni:I
D/dalvikvm( 5197): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodGetInfo:II
D/dalvikvm( 5197): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodBlockStreaming:I
D/dalvikvm( 5197): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodProcessMicData:ILI
D/dalvikvm( 5197): Unregistering JNI method Lcom/unity3d/player/ReflectionHelper;.nativeProxyInvoke:LILL
D/dalvikvm( 5197): Unregistering JNI method Lcom/unity3d/player/ReflectionHelper;.nativeProxyFinalize:VI
I/Process ( 5197): Sending signal. PID: 5197 SIG: 9
I/ActivityManager( 582): Process com.example.CustomApp (pid 5197) has died.

I’ve created a small repro project - in the CustomApp folder you can run “./gradlew installDebug” to try it out:

CustomApp project

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.

A call stack for the crash would be very helpful.

Thanks for replying.

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:

W/ActivityManager( 583): Activity pause timeout for ActivityRecord{42a06310 u0 au.com.adapptor.mirvac/.UnityActivity t65 f}
I/ActivityManager( 583): Config changes=480 {1.0 505mcc2mnc en_GB ldltr sw384dp w384dp h567dp 320dpi nrml port finger -keyb/v/h -nav/h s.45}
I/InputReader( 583): Reconfiguring input devices. changes=0x00000004
I/InputReader( 583): Device reconfigured: id=6, name=‘touch_dev’, size 768x1280, orientation 0, mode 1, display id 0
I/InputReader( 583): Reconfiguring input devices. changes=0x00000004
D/PhoneStatusBar( 851): mSettingsPanelGravity = 55
D/InfoFragment(27618): Fetch assigned 5 estates
D/MirvacActivity(27618): onResume()
I/WindowManager( 583): Screen frozen for +223ms due to Window{42b38f88 u0 au.com.adapptor.mirvac/au.com.adapptor.mirvac.MirvacActivity}
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeVideoFrameCallback:VILII
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSoftInputClosed:V
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetTouchDeltaY:VF
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetInputString:VL
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetInputCanceled:VZ
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetExtras:VL
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetDefaultDisplay:VI
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeResume:V
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeResize:VIIII
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRequestedAA:I
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRequested32bitDisplayBuffer:Z
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRender:Z
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeRecreateGfxState:VL
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativePause:Z
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeInjectEvent:ZL
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeInitWWW:VL
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeFocusChanged:VZ
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeFile:VL
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeDone:V
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeActivityIndicatorStyle:I
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.initJni:VL
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.UnitySendMessage:VLLL
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetLocationStatus:VI
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeSetLocation:VFFFFDF
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeForwardEventsToDalvik:VZ
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/UnityPlayer;.nativeDeviceOrientation:VI
D/dalvikvm(27618): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodUnblockStreaming:I
D/dalvikvm(27618): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodProcess:IL
D/dalvikvm(27618): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodInitJni:I
D/dalvikvm(27618): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodGetInfo:II
D/dalvikvm(27618): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodBlockStreaming:I
D/dalvikvm(27618): Unregistering JNI method Lorg/fmod/FMODAudioDevice;.fmodProcessMicData:ILI
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/ReflectionHelper;.nativeProxyInvoke:LILL
D/dalvikvm(27618): Unregistering JNI method Lcom/unity3d/player/ReflectionHelper;.nativeProxyFinalize:VI
I/Process (27618): Sending signal. PID: 27618 SIG: 9
I/ActivityManager( 583): Process au.com.adapptor.mirvac (pid 27618) has died.
I/WindowState( 583): WIN DEATH: Window{42b38f88 u0 au.com.adapptor.mirvac/au.com.adapptor.mirvac.MirvacActivity}
I/WindowState( 583): WIN DEATH: Window{42c69490 u0 au.com.adapptor.mirvac/au.com.adapptor.mirvac.UnityActivity}
W/ActivityManager( 583): Force removing ActivityRecord{42a00ee0 u0 au.com.adapptor.mirvac/.MirvacActivity t65}: app died, no saved state
W/HandlerScheduledExecuto( 1064): Task does not implement UiTask. Consider using NamedUiRunnable for eww@422590d0
W/Binder ( 996): Caught a RuntimeException from the binder stub implementation.
W/Binder ( 996): java.lang.NullPointerException
W/Binder ( 996): at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
W/Binder ( 996): at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
W/Binder ( 996): at android.os.Binder.execTransact(Binder.java:404)
W/Binder ( 996): at dalvik.system.NativeStart.run(Native Method)

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.

Ah yes okay, I figured this out shortly after posting :slight_smile: I tried copying the files from the Eclipse/Android project with the same result.

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().

Ok, I’ve found the cause of the crash.

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.

Are there plans to fix this?

Right, this is how it is currently implemented, and unlikely to be changed in upcoming versions. Sad but true. :eyes:

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.

Heya, could you please elaborate more? where exactly did you put that process=“:foo” ?