user3162374
user3162374

Reputation: 11

UnitySendMessage crash game

I have such a question I do a plug-in for Unity Android. Unity 5.6.5. I collect in Android Studio 3.3. I assemble the plugin through assembleRelease. settings gradle:

apply plugin: 'com.android.library'

android 
{
    compileSdkVersion 28
    defaultConfig 
    {

        minSdkVersion 21
        targetSdkVersion 28

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes 
    {
        release 
        {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies 
{
    implementation fileTree(dir: 'libs', include: ['*.jar'], exclude: ['classes.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compileOnly files('libs/classes.jar')
}

Class Code:

package andlancer.unity.custom.web;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;


public class TrueWeb extends UnityPlayerActivity 
{
    public static String EXTRA_URL = "extra.url";

    static CustomTabsClient mClient;
    static String packageName = "com.android.chrome";

    private boolean mCustomTabsOpened = false;

    private static final int CHROME_CUSTOM_TAB_REQUEST_CODE = 100;

    public static Context mContext;



    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        mContext=this;

        Log.e("Unity", "OnCreate...........");
    }

    public static void startFunc(UnityPlayerActivity activity)
    {
        Log.e("test","-----------------test");
        UnityPlayer.UnitySendMessage("Core", "CloseWeb", null);
    }
}

manifest plugin (into aar):

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="andlancer.unity.custom.web">

    <application
        android:allowBackup="true"

        android:label="@string/app_name">
        <activity
            android:name=".TrueWeb"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Unity code:

AndroidJavaClass plugin;
plugin = new AndroidJavaClass("andlancer.unity.custom.web.TrueWeb");

using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
        plugin.CallStatic("startFunc", activity);

Log:

Log.e("Unity", "OnCreate..........."); // not shown
Log.e("test","-----------------test"); // shown

and when calling UnityPlayer.UnitySendMessage the application crashes without errors on the device in the logs writes:

03-04 19:24:23.183 201-201/? A/DEBUG: backtrace:
03-04 19:24:23.183 201-201/? A/DEBUG:     #00 pc 000188ac  
/system/lib/libc.so (strlen+71)
03-04 19:24:23.183 201-201/? A/DEBUG:     #01 pc 0059d270  
/data/app/bla.bla.huyar-2/lib/arm/libunity.so (UnitySendMessage+24)
03-04 19:24:23.183 201-201/? A/DEBUG:     #02 pc 005f3a2c  
/data/app/bla.bla.huyar-2/lib/arm/libunity.so
03-04 19:24:23.183 201-201/? A/DEBUG:     #03 pc 00b34b45  
/data/app/bla.bla.huyar-2/oat/arm/base.odex (offset 0x6ce000)

Upvotes: 0

Views: 925

Answers (3)

Jasil
Jasil

Reputation: 175

Please send empty string as argument, its expecting string null can be reason for crash

Upvotes: 0

Di Zhang
Di Zhang

Reputation: 346

try replacing the last paramter from 'null' to "".

UnityPlayer.UnitySendMessage("Core", "CloseWeb", "");

Upvotes: 1

Christos Themelis
Christos Themelis

Reputation: 189

If you have another aar with a class that contains "extends UnityPlayerActivity", only the first loaded will take this context. For me, to solve familiar issue, I pass the context from Unity to aar (as an Init function). Create a function inside "TrueWeb" class that will returns you the mContext. Then use this context for every place you need context inside Android aar

Upvotes: 0

Related Questions