Android Studio and Unity3d

If you are building native Android plugins for Unity with Android Studio here are a couple of items that I struggled with. Note that all examples are using Unity 4.6 and Android Studio.

1. Unity Android plugin folder needs a jar file to function – but Android Studio build process does not produce a class jar file.

It turns out that Android Studio actually does create a jar file during the build process and stores it in an intermediate location (on the Mac it is in ‘build/intermediates/bundles/release/’).

Based on suggestions from here I created two new tasks in my gradle file – cleanjar and makejar – after a build I run both and now have the required jar file in my ‘build/outputs/’ folder.

 task clearJar(type: Delete) {
delete ‘build/outputs/MergeVRBridge.jar’
delete ‘build/outputs/AndroidManifest.xml’
}

task makeJar(type: Copy) {
from(‘build/intermediates/bundles/release/’)
into(‘build/outputs/’)
include(‘classes.jar’)
include(‘AndroidManifest.xml’)
rename (‘classes.jar’, ‘MergeVRBridge.jar’)
}

2. If your Android library file requires a context – you can pass it to your lib from Unity.

AndroidJNI.AttachCurrentThread();
           androidClass = new AndroidJavaClass(com.mergelabs.MergeVR.MergeVRBridge);

            using(AndroidJavaClass activityClass = new AndroidJavaClass(com.unity3d.player.UnityPlayer)) {
              
                activityContext = activityClass.GetStatic<AndroidJavaObject(currentActivity);
            }
            androidClass.CallStatic(setContextactivityContext);
            androidClass.CallStatic<int>(mergeVRInit);

and in your Android Java clase you’ll have the appropriate function

private static Context mContext;

 static public void setContext(Context context) {

mContext = context;
}

3. You need bluetooth permissions set in your Android plugins manifest file.

If you just copy and paste the manifest file from your Android Studio build directory to the Unity Android plugin directory you will see compile and build errors. It appears in Unity 4.6 that if you place a manifest file in the Android plugin directory it overrides the Unity created manifest file for the entire app.

The best solution is to find the Unity generated manifest file, copy it to a temp directory and modify by adding any permissions you need (in my case bluetoth related), then add to the Android plugins folder – Unity will use this manifest file as the default and all will be well.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221; package=”com.merge.demo” android:versionName=”1.0″ android:versionCode=”1″ android:installLocation=”preferExternal”>

<uses-permission android:name=”android.permission.BLUETOOTH”/>
<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”/>
<uses-feature android:name=”android.hardware.bluetooth_le” android:required=”true”/>

<supports-screens android:smallScreens=”true” android:normalScreens=”true” android:largeScreens=”true” android:xlargeScreens=”true” android:anyDensity=”true” />

<application android:theme=”@android:style/Theme.NoTitleBar” android:icon=”@drawable/app_icon” android:label=”@string/app_name” android:debuggable=”false”>
<activity android:name=”com.unity3d.player.UnityPlayerNativeActivity” android:label=”@string/app_name” android:screenOrientation=”sensorLandscape” android:launchMode=”singleTask” android:configChanges=”mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
<category android:name=”android.intent.category.LEANBACK_LAUNCHER” />
</intent-filter>
<meta-data android:name=”unityplayer.UnityActivity” android:value=”true” />
<meta-data android:name=”unityplayer.ForwardNativeEventsToDalvik” android:value=”false” />
</activity>
</application>
<uses-sdk android:minSdkVersion=”18″ android:targetSdkVersion=”21″ />
<uses-feature android:glEsVersion=”0x00020000″ />
<uses-feature android:name=”android.hardware.touchscreen” android:required=”false” />
<uses-feature android:name=”android.hardware.touchscreen.multitouch” android:required=”false” />
<uses-feature android:name=”android.hardware.touchscreen.multitouch.distinct” android:required=”false” />
<uses-permission android:name=”android.permission.WAKE_LOCK” />
</manifest>

 

 

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s