Unity+ARToolkit embedded in AndroidStudio Help

Hi everyone, i am working on a project right now and i hope to find some help here. i saw lots of stuff regarding these topics separately but not working together and they helped me for previous dev but not for this one.

The goal is to use augmented reality with Unity3D + ARToolKit in native android using Android Studio.
First, i started by exporting unity projects into AndroidStudio. Thanks to updates on AndroidStudio, it is now quite easy.
Right now i am able to:

  • Run directly the Unity view by running the UnityPlayerActivity in Android Studio,
  • Run the app with a first activity, say the MainActivity, and i can launch the unity activity by clicking on a button.
  • Run unity with both previous cases and by adding a framelayout on the unity view, i can interact in realtime with the unity view and change a gui text depending of which button i click on. The button are all native android and are sending string messages to unity which i can get thanks a c# script and as a consequence, changing the Gui text in unity.
  • Run unity by converting an unity project imported as an android studio project and converted as an android library (.aar). So basically, i have one project with my main activities and one project with the unity features. I converted the android studio project with unity features as .aar library and imported this library into my main project. i can launch unity from my main project by calling the library activity.

This is all working so if someone is interested in one of this features, i can help them.

Here is my problem:
I want to add some AR. I am using an AR plugin in unity called ARToolKit. It’s not as robust as vuforia but it’s working.
I followed the same method as for the other features and completed my unity project with AR. In unity, it’s working.
I then exported it as a google android project and imported the created directory into Android Studio so i have my android studio project with unity+AR features.
I let the gradle build and if i launch the default activity on my phone, i can load my AR scene in unity and play with the AR so it’s great.

The problem is the following: i want to add an activity, say MainActivity and launch it first, i will see my first view and by clicking on the button, i could launch unity+AR. Unfortunately, when i do that, the app crash and i don’t know why.
I already did that with unity only, without the ar plugin and it worked fine so why doesn’t it work now??

As i said, the app is working if i launch the default activity, the UnityARPlayerActivity but crashes if i try to run another activity first.

I think it may be due to some manifest problems. For the AR to work when running, it may need to access some hardwares or permissions or other stuff and by trying to launch another activity, it crashes because of some exceptions not catch.

i am using unity Unity 5.3.4f1 (64-bit), Android Studio 2.0
i started two weeks ago so i have recent SDK and Jre.


update:
one of the problem i found looking at the logcat was
“java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity”,
i found other answers and fix it by replacing
“public class MainActivity extends AppCompatActivity” by
“public class MainActivity extends Activity”

So basically, to launch my AR scene, i am using this intent from my MainActivity.
« Intent intent = new Intent(MainActivity.this, UnityARPlayerActivity.class); »

Right now i am able to launch unity + AR directly on my smartphone and I don’t really understand what i did to make it work but i succeeded in launching the MainActivity first and then unity+AR thanks to a button. I don’t really get how but it is working

new problems are: launching the main activity and then unity+AR is working on my phone (sony xperiaZ C6603) API 22 et android 5.1.1) but only mine, it doesn’t work on my colleague’s tablet (samsung SM-T550, android 5.0.2 and API 21), it doesn’t work on emulators using genymotion and i tried for different APIs and Android versions. The logcat is showing the following error :

Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ludo.AR/org.artoolkit.ar.unity.UnityARPlayerActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0

MainActivity:

package com.ludo.AR;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import org.artoolkit.ar.unity.UnityARPlayerActivity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                goToSecondActivity();
            }
        });
    }

    private void goToSecondActivity() {
        Intent intent = new Intent(MainActivity.this, UnityARPlayerActivity.class);
        startActivity(intent);
    }
}

Logcat:

04-27 17:01:23.028 25322-25322/com.ludo.AR E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: com.ludo.AR, PID: 25322
                                                             java.lang.Error: FATAL EXCEPTION [main]
                                                             Unity version     : 5.3.4f1
                                                             Device model      : samsung SM-T550
                                                             Device fingerprint: samsung/gt510wifixx/gt510wifi:5.0.2/LRX22G/T550XXU1AOJ1:user/release-keys
                                                             
                                                             Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ludo.AR/org.artoolkit.ar.unity.UnityARPlayerActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2790)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:181)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:145)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6117)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                                                              Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
                                                                 at android.content.res.Resources.getValue(Resources.java:2358)
                                                                 at android.content.res.Resources.loadXmlResourceParser(Resources.java:3958)
                                                                 at android.content.res.Resources.getXml(Resources.java:2223)
                                                                 at android.preference.GenericInflater.inflate(GenericInflater.java:261)
                                                                 at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
                                                                 at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:485)
                                                                 at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:444)
                                                                 at org.artoolkit.ar.unity.UnityARPlayerActivity.onCreate(UnityARPlayerActivity.java:118)
                                                                 at android.app.Activity.performCreate(Activity.java:6374)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2743)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855) 
                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:181) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:145) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6117) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

manifest code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ludo.AR"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="23" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-feature
        android:name="android.hardware.camera.any"

        android:required="false" />
    <uses-feature
        android:name="android.software.leanback"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />
    <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" />

    <application
        android:banner="@drawable/app_banner"

        android:icon="@drawable/app_icon"
        android:isGame="true"
        android:label="@string/app_name">
        <activity
            android:name="org.artoolkit.ar.unity.UnityARPlayerActivity"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait">


            <meta-data
                android:name="unityplayer.ForwardNativeEventsToDalvik"
                android:value="true" />
        </activity>
        <activity android:name="org.artoolkit.ar.base.camera.CameraPreferencesActivity"></activity>
        <activity android:name=".MainActivity">
            <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>
        </activity>
    </application>

</manifest>

Sorry if this is long, i try to explain clearly everything and it’s my first time posting questions here

i am posting a reply myself as i found a solution.
it seems there is a bug when exporting to android and a library is not compiled so you need to add it manually in your android studio project.

Check this link to understand how to do it.

@pirou Can you please repost the answer since i have the exact same problem and i am keeping getting a black camera screen… Thanks in advance.,@pirou The link is broken can you please post somewhere the answer since i have the same exactly problem.