Search code examples
androidunity-game-engineandroid-proguard

Unity Android integrated into a different Android app is crashing on Application.Quit()


After we exported the project to an Android project, we integrated it with an existing project from which we're launching our game. This was working correctly, but Application.Quit() was making the entire app quit instead of just the Unity activity. So we made it launch on a different process by adding this to the manifest:

android:process=":UnityKillsMe"

This solved the issue, however, after using Proguard to obfuscate the code, things stopped working. Unity still launches correctly, however when quitting, the app shows a crash error that Unity stopped, even though the app itself is still working. Relaunching Unity even works correctly, but on Application.Quit(), the crash error still shows. Here's the error log when it happens:

12-02 15:01:35.112: E/AndroidRuntime(4342): FATAL EXCEPTION: UnityMain
12-02 15:01:35.112: E/AndroidRuntime(4342): Process: mobi.foo.touch:UnityKillsMe, PID: 4342
12-02 15:01:35.112: E/AndroidRuntime(4342): java.lang.Error: FATAL EXCEPTION [UnityMain]
12-02 15:01:35.112: E/AndroidRuntime(4342): Unity version     : 5.2.3f1
12-02 15:01:35.112: E/AndroidRuntime(4342):  at com.unity3d.player.UnityPlayer.nativeDone(Native Method)
12-02 15:01:35.112: E/AndroidRuntime(4342):  at com.unity3d.player.UnityPlayer.f(Unknown Source)
12-02 15:01:35.112: E/AndroidRuntime(4342):  at com.unity3d.player.UnityPlayer.g(Unknown Source)
12-02 15:01:35.112: E/AndroidRuntime(4342):  at com.unity3d.player.UnityPlayer$15.run(Unknown Source)
12-02 15:01:35.112: E/AndroidRuntime(4342):  at com.unity3d.player.UnityPlayer.executeGLThreadJobs(Unknown Source)
12-02 15:01:35.112: E/AndroidRuntime(4342):  at com.unity3d.player.UnityPlayer$b.run(Unknown Source)
12-02 15:01:35.122: E/ActivityManager(890): App crashed! Process: mobi.foo.touch:UnityKillsMe

How can this be solved? What's causing this issue?


Solution

  • I have just encountered the exact issue and managed to resolve it.

    Proguard + Unity3D launching in a separate process will crash if Application.Quit() was called in Unity3D.

    Solution: Quit the Unity3D Application by destroying it natively.

    Unity3D:

    // Add this code in, replace Application.Quit() to AndroidQuit()
    void AndroidQuit(){
        AndroidJavaClass myClass = new AndroidJavaClass("com.yourpackagenamehere.UnityPlayerActivity");
        AndroidJavaObject activity = myClass.GetStatic<AndroidJavaObject>("UnityActivity");
        activity.Call("QuitUnity");
    }
    

    Android Project:

    // In UnityPlayerActivity.java
    
    // Declare static object for Unity to make calls from
    public static Context UnityActivity;
    
    // Instantiate it in the onCreate Method right after super.onCreate
    UnityActivity = this;
    
    // Add the following method for Unity3D function to trigger the Quit    
    public void QuitUnity() {
        android.os.Process.killProcess(android.os.Process.myPid());
        //finish();
        //this.onDestroy();
    }
    

    EDIT: After I have updated my project, this.OnDestroy() and finish() will show a crash dialog. android.os.Process.killProcess() works great.

    However directly killing the process works out fine.

    On a side note, one of the reason why there is the crash could be due to my usage Render Texture and the following unity error surfaces in the logcat.

    Destroying active render texture. Switching to main context.