Unity C#和Layar SDK Java - AndroidJavaException

时间:2016-12-21 12:12:56

标签: java c# android unity3d layar

我正在尝试将Layar SDK集成到Unity。 但我有一些问题。

这是 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.layar.player"
android:versionCode="1"
android:versionName="8.5.2" >

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

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

<application>
    <activity
        android:name="com.layar.sdk.LayarSDKActivity"
        android:launchMode="singleTask"
        android:screenOrientation="nosensor"
        android:theme="@android:style/Theme.Light.NoTitleBar" />
    <activity
        android:name="com.layar.player.geo.FilterSettingsActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:theme="@style/FilterTheme" />
    <activity
        android:name="com.layar.LayerDetailsActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:theme="@style/LayarTitleTheme" />
    <activity
        android:name="com.layar.WebActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:theme="@style/LayarTitleTheme" />
    <activity
        android:name="com.layar.ShareSocialActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:theme="@style/Layar.Theme.Default" />
    <activity
        android:name="com.layar.VideoActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:theme="@style/FullScreenTheme" />
    <activity
        android:name="com.layar.AudioActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:theme="@style/SmartDialogTheme" />
    <activity
        android:name="com.layar.ScreenshotActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:screenOrientation="nosensor"
        android:theme="@android:style/Theme.Light.NoTitleBar" />
    <activity
        android:name="com.layar.ShareOnFacebookActivity"
        android:theme="@android:style/Theme.Light.NoTitleBar"
        android:windowSoftInputMode="stateAlwaysVisible|adjustResize" />

    <receiver
        android:name="com.layar.localytics.CloseSesionBroadcastReciever"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.layar.close_session" />
        </intent-filter>
    </receiver>
</application>

这是我的c#

using UnityEngine;

public class IntegrationLayar : MonoBehaviour {

private AndroidJavaObject LayarSDK = null;
private AndroidJavaObject activity = null;
private AndroidJavaObject context = null;

private string oauthKey = "code";
private string oauthSecret = "code2";


void Start ()
{
        if (LayarSDK == null)
        {
            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
                context = activity.Call<AndroidJavaObject>("getApplicationContext");

                //activity.Call("runOnUiThread", new AndroidJavaRunnable(runOnUiThread));
                Debug.Log("unity3D JAVA - Working");
            }

            using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.layar.player"))
            {
                if (pluginClass != null)
                {
                    Debug.Log("Layar Java - Working");
                    pluginClass.CallStatic("initialize", context, oauthKey, oauthSecret);
                    pluginClass.CallStatic("startLayarActivity", context);
                }
            }
    }
}
}

错误

  

AndroidJavaException:java.lang.ClassNotFoundException:com.layar.player

据我了解,它没有在Java中找到这样命名的类。 那么问题是什么?我打错了活动?

以下是Layar API link

的链接

正如我在Layar API中看到的那样,该类是“LayarVisionSDK”。

AndroidJavaClass pluginClass = new AndroidJavaClass("com.layar.player.LayarVisionSDK")

所以也许这应该有效?

我当然做错了,但如果你有任何指示要遵循,我会很高兴得知:)

更新1:

这是来自APK

Class LayarVisionSDK

java.lang.Object
com.layar.sdk.LayarVisionSDK

所以不要使用

AndroidJavaClass pluginClass = new AndroidJavaClass("com.layar.player.LayarVisionSDK")

我正在使用

AndroidJavaObject LayarVisionSDK = new AndroidJavaObject("com.layar.sdk.LayarVisionSDK");

但仍然没有成功。我问自己这个问题是否可能不存在。

更新2:

好的!我发现为什么我得到这个错误,API文档没有更新,所以我检查了java包,发现在哪里是“初始化”方法。

所以这是新的c#代码。

using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.layar.sdk.LayarSDK"))
                {
                    if (pluginClass != null)
                    {
                        Debug.Log("SDK LAYAR FOUND !!! ");
                        pluginClass.CallStatic("initialize", context, oauthKey, oauthSecret);
                        Debug.Log("INITIALIZED");
                        //pluginClass.Call("startLayarActivity", context);
                    }
                }

但我收到了一个新错误:

AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;

我错过了一个安卓库吗?

2 个答案:

答案 0 :(得分:1)

确保在插件/ Android / libs 文件夹下有android-support-v4.jar。

您可以从here下载文件。

答案 1 :(得分:0)

好的我能让它发挥作用!

我只是添加了android支持lib和kotlin lib。

感谢您的帮助:)