我有一个问题,我为Unity Android做了一个插件。Unity 5.6.5。我在Android Studio 3.3中收集。我通过assembleRelease组装插件。设置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')
}
类代码:
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);
}
}
清单插件(到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>
单位代码:
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.e("Unity", "OnCreate..........."); // not shown
Log.e("test","-----------------test"); // shown
当调用UnityPlayer.UnitySendMessage时,应用程序在设备上崩溃,日志中写道:
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)
发布于 2019-03-04 18:48:40
尝试将最后一个参数从“null”替换为"“。
UnityPlayer.UnitySendMessage("Core", "CloseWeb", "");
发布于 2019-03-04 18:31:46
如果您有另一个包含“扩展UnityPlayerActivity”的类的aar,则只有第一个加载的aar将采用此上下文。对我来说,为了解决熟悉的问题,我将上下文从Unity传递到aar (作为一个Init函数)。在"TrueWeb“类中创建一个函数,该函数将返回mContext。然后在Android aar中的每个需要上下文的地方使用这个上下文
发布于 2019-03-04 18:56:37
请将空字符串作为参数发送,其预期的字符串null可能是崩溃的原因
https://stackoverflow.com/questions/54980191
复制相似问题