yasirkula / UnityIonicIntegration

A guide to integrating Unity 3D content into an Ionic app and sending messages between them (for Android & iOS)(tested with Vuforia plugin)
104 stars 32 forks source link

Android build is crashing when launching Unity #51

Closed BeRMaNyA closed 5 years ago

BeRMaNyA commented 5 years ago

Hi, I'm facing an issue for about two days and I couldn't fix it yet... I think I tried everything.

I have implemented this plugin for IOS without issues, Unity works perfect but now I compile a new build for android and when I try to launch unity inside my Cordova app I got a crash with this error:

"App has stopped"

Logcat:

2019-06-05 16:02:19.350 7753-7753/com.crisalix.My:unityselfprocess E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.crisalix.My:unityselfprocess, PID: 7753
    java.lang.Error: FATAL EXCEPTION [main]
    Unity version     : 5.5.1f1
    Device model      : samsung SM-G920F
    Device fingerprint: samsung/zerofltexx/zeroflte:7.0/NRD90M/G920FXXU6ERF5:user/release-keys

    Caused by: java.lang.RuntimeException: Unable to destroy activity {com.crisalix.My/com.unitycaller.ionic.UnityPlayerExtendedActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.sendMessage(android.os.Message)' on a null object reference

It seems the line that crash is this one:

public class UnityPlayerExtendedActivity extends UnityPlayerActivity
{
  ...
    @Override protected void onDestroy ()
    {
        unregisterReceiver( broadcastReceiver );
        super.onDestroy();
    }

I tried with an empty project and I got the same exception. I also tried Unity 2018 and Unity 2019.

berna@berna ~/crisalix/MyCordova (master●●)$ cordova info                                                                                                                                                                             [2.4.2]
cordova-lib@9.0.1 with:
  cordova-common@3.1.0
  cordova-create@2.0.0
  cordova-fetch@2.0.1
  cordova-serve@3.0.0

Environment:
  OS: darwin
  Node: v11.1.0
  npm: 6.9.0

Plugins:
  cordova-plugin-camera
  cordova-plugin-geolocation
  cordova-plugin-ionic-keyboard
  cordova-plugin-ionic-webview
  cordova-plugin-ios-camera-permissions
  cordova-plugin-network-information
  cordova-plugin-statusbar
  cordova-plugin-whitelist
  ionic-plugin-deeplinks
  phonegap-plugin-multidex
  unityARCaller

Android platform:
  *************************************************************************
  The "android" command is deprecated.
  For manual SDK, AVD, and project management, please use Android Studio.
  For command-line tools, use tools/bin/sdkmanager and tools/bin/avdmanager
  *************************************************************************
  Running /usr/local/Caskroom/android-sdk/4333796/tools/bin/avdmanager list target

  Available Android targets:==============] 100% Fetch remote repository...
  ----------
  id: 1 or "android-28"
       Name: Android API 28
       Type: Platform
       API level: 28
       Revision: 6

Any help would be appreciated.

Thanks!

yasirkula commented 5 years ago

I just can't figure out where in onDestroy the android.os.Handler.sendMessage is called. Does changing onDestroy like this resolve the issue:

@Override protected void onDestroy ()
{
    if( broadcastReceiver != null )
        unregisterReceiver( broadcastReceiver );

    super.onDestroy();
}
BeRMaNyA commented 5 years ago

@yasirkula thanks, the original issue was caused due I placed this line compile project(':UnityProject') into the wrong file.

Actually I searched for these lines releaseCompile, debugCompile and I got the wrong .gradle.

The weird thing that using compile project(':UnityProject)` works fine inside this file: cordova/lib/plugin-build.gradle but makes the app crashes...

Then when I put this line compile project(':UnityProject') into the app/build.gradle, I got a deprecation error.

In the end my build.gradle looks like this:

dependencies {
    implementation fileTree(include: '*.jar', dir: 'libs')
    // SUB-PROJECT DEPENDENCIES START
    implementation project(path: ':CordovaLib')
    implementation 'com.android.support:support-v4:24.1.1+'
    implementation 'com.android.support:support-annotations:27.+'
    // SUB-PROJECT DEPENDENCIES END
    implementation project(':unityProject')
    implementation files('/Users/berna/crisalix/MyCordova/platforms/android/libs/unity-classes.jar')
}

I needed to import the unity-classes.jar library into the android module.

Also I had to do this in the Unity project's build.gradle:

dependencies {
    implementation files('/Users/berna/crisalix/MyCordova/platforms/android/libs/unity-classes.jar')
    implementation files('libs/InAppBrowser.jar')
    implementation files('libs/NativeGallery.jar')
    implementation files('libs/UniWebView.aar')
}

Not sure if it's the best solution but worked fine for Unity 2018, now I'm going to try with Unity 2019.

Thanks for your time.