I have an Android application that builds and runs find before the introduction of the following code to an object in my scene. My goal was to vibrate at different lengths for my application instead of just using the default Handheld.Vibrate()
function. Once this code is added, my application still builds to my device just fine, but crashes on startup.
I am using Unity 2019.2.9f1, and my minimum API level is set to 25, using Mono and .NET 2.0.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HapticController : Singleton<HapticController> {
#if UNITY_ANDROID && !UNITY_EDITOR
public static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
public static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
public static AndroidJavaObject vibrator = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
#else
public static AndroidJavaClass unityPlayer;
public static AndroidJavaObject currentActivity;
public static AndroidJavaObject vibrator;
#endif
bool isVibrating;
...
// Update is called once per frame
void Update()
{
if(!isVibrating && ( *other condition* ) ){
isVibrating = true; //this will later be changed, just trying to get it to vibrate once
Vibrate(200);
}
}
public static void Vibrate(long milliseconds)
{
if(isAndroid() && vibrator != null){
print("vibrating");
vibrator.Call("vibrate", milliseconds);
} else {
Handheld.Vibrate();
}
}
private static bool isAndroid()
{
#if UNITY_ANDROID && !UNITY_EDITOR
return true;
#else
return false;
#endif
}
}
I know that it is the AndroidJavaClass/AndroidJavaObject calls at the top that are causing the crash. I have checked the logs in logcat in Android Studio, but there is no notable output that would have anything to do with the crash. Is there something I am not accounting for that would cause this?
EDIT: I should add that I am using an SDK (Mapbox) that has its own AndroidManifest.xml. It instructs you to rename it and move it to the Plugins/Android folder. You can see it here:
<!--
Android Manifest for UniAndroid Permission (2016/03/19 sanukin39)
--- if already have AndroidManifest file at Assets/Plugins/Android/ ----
Copy the activity and meta-data sentence to your AndroidManifest.xml
--- if not ---
Rename this file to AndroidManifest.xml and add permission you want to add And move the file to Assets/Plugins/Android
-->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.product">
<application android:icon="@drawable/app_icon" android:label="@string/app_name">
<activity android:name="net.sanukin.OverrideUnityActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
</application>
</manifest>