Azure / azure-spatial-anchors-samples

Azure Spatial Anchors sample code
Other
293 stars 139 forks source link

Azure spatial anchors error on unity android : azurespatialanchorsndk not found #235

Closed rjtshrm closed 3 years ago

rjtshrm commented 3 years ago

I am trying to use azure spatial anchors with the unity android app. However, I am always getting the error mentioned below when try to run on unity. DllNotFoundException: azurespatialanchorsndk Microsoft.Azure.SpatialAnchors.CloudSpatialAnchorSession..ctor () (at Library/PackageCache/com.microsoft.azure.spatial-anchors-sdk.android@2.7.0/Runtime/AzureSpatialAnchorsBridge_Android.cs:2056) AzureSpatialAnchor.CreateAzureSession () (at Assets/Scripts/AzureSpatialAnchor.cs:82) AzureSpatialAnchor.Awake () (at Assets/Scripts/AzureSpatialAnchor.cs:58)

Also, when I try to deploy the app on android, the app always crashes with azure spatial ancors functionality. Below are the logs from android logcat

2021/01/21 19:51:59.359 13344 18592 Info multiplayerdem Thread[17,tid=18592,Native,Thread*=0xcf456800,peer=0x12d406f8,"UnityMain"] recursive attempt to load library "/data/app/com.DefaultCompany.armultiplayerdemo-p-urYsoMMitWiE2Z6JXUqg==/base.apk!/lib/armeabi-v7a/libazurespatialanchorsndk.so" 2021/01/21 19:51:59.404 13344 18592 Error CRASH #01 Microsoft.Azure.SpatialAnchors.CloudSpatialAnchorSession:Start () <C:\Users\rash8327\Desktop\ar-multiplayer-demo\Library\PackageCache\com.microsoft.azure.spatial-anchors-sdk.android@2.7.0\Runtime\AzureSpatialAnchorsBridge_Android.cs:2593>

Below is my mainTemplate.gradle file that I have used for the build

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        // Must be Android Gradle Plugin 3.6.0 or later. For a list of
        // compatible Gradle versions refer to:
        // https://developer.android.com/studio/releases/gradle-plugin
        classpath 'com.android.tools.build:gradle:3.6.0'
    }
}

allprojects {
   repositories {
      google()
      jcenter()
      flatDir {
        dirs 'libs'
      }
   }
}

apply plugin: 'com.android.library'
**APPLY_PLUGINS**

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation('com.squareup.okhttp3:okhttp:[3.11.0]')
    implementation('com.microsoft.appcenter:appcenter-analytics:[1.10.0]')
**DEPS**
    // Azure Spatial Anchors
    //implementation ("com.microsoft.azure.spatialanchors:spatialanchors_ndk:[2.7.0]") {
    //    exclude group: 'com.google.ar'
    //}
    //natives "com.microsoft.azure.spatialanchors:spatialanchors_ndk:[2.7.0]"
}

android {
    compileSdkVersion **APIVERSION**
    buildToolsVersion '**BUILDTOOLS**'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion **MINSDKVERSION**
        targetSdkVersion **TARGETSDKVERSION**
        ndk {
            abiFilters **ABIFILTERS**
        }
        versionCode **VERSIONCODE**
        versionName '**VERSIONNAME**'
        consumerProguardFiles 'proguard-unity.txt'**USER_PROGUARD**
    }

    lintOptions {
        abortOnError false
    }

    aaptOptions {
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }**PACKAGING_OPTIONS**
}**REPOSITORIES****SOURCE_BUILD_SETUP**
**EXTERNAL_SOURCES**

I have tried everything but this error does not resolve.

------- Versions-------- Unity : 2019.4.15f1 AR Foundation : 4.1.1 AR Subsystems : 4.1.1 ARCore XR Plugin: 4.1.1 Azure Spatial Anchor : 2.7.0

msftradford commented 3 years ago

Thanks for reaching out @rjtshrm! I'll have to do some digging around to try and replicate this issue. Do you by chance have a minimal repro that you can share with us? Have you been able to successfully deploy our sample app for Unity on Android? Have you followed the steps to add the ASA registry and SDK package to your Unity project?

rjtshrm commented 3 years ago

Yes, I followed the same approach but for android device. I followed the unity setup from ASA Setup Unity And, I have not been able to set up the sample app on Android as well. I got build errors

java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class com.microsoft.CloudServices found in modules classes.jar (:spatialanchors_ndk:) and classes.jar (com.microsoft.azure.spatialanchors:spatialanchors_ndk:2.2.1)
...

Also, unity created a new mainTemplate.gradle as It was using the old format. However, I am trying to write my own functionality for azure spatial anchors, the android app craches when calling CloudSpatialAnchorSession . I have setup a minimal repo to recreate this behaviour which can find azure-spatial-anchors-sample. Below are the android logs captured from logcat.

logcat_1 logcat_2

msftradford commented 3 years ago

Thanks for all of the info @rjtshrm! I just went through the your repro repo and noticed a couple of issues. It would be quite valuable to get one of the sample applications running first so that you can compare to your own app.

Azure Spatial Anchors is currently only supported on ARFoundation 3.1. You'll want to change your version settings in the package manager.

After making that change, the app was crashing on the call to GetPlatformPointer(), which is an internal method provided as a part of the SpatialAnchorExtensions in the Azure Spatial Anchors SDK Core. You can find the file by navigating to the Project pane->Packages->Azure Spatial Anchors SDK Core->Runtime->Scripts. To provide the implementation in-line, it would look something like this:

IntPtr intPtr = aRSession.subsystem.nativePtr;
if (intPtr == IntPtr.Zero) return;
UnityPointer pointerGetter = Marshal.PtrToStructure<UnityPointer>(intPtr);
cloudSession.Session = pointerGetter.platformPointer;

I would recommend, however, that you consider using the provided SpatialAnchorManager to handle this. It is used in all of our Unity samples and can be used by developers in their own apps to abstract away much of the ARFoundation/ARKit/ARCore boilerplate code. If you are having issues getting the samples running or using SpatialAnchorManager, please file a separate issue so that we do not confuse future devs. Thanks.

rjtshrm commented 3 years ago

Thanks for the update, I manage to run the sample app from azure as well as my app using SpatialAnchorManager. To write my own cloud session I followed the same advice as mentioned here. There it is mentioned to get session pointer and create cloud session by the following code

#if UNITY_ANDROID || UNITY_IOS
    this.cloudSession.Session = aRSession.subsystem.nativePtr.GetPlatformPointer();
#elif UNITY_WSA || WINDOWS_UWP
    // No need to set a native session pointer for HoloLens.
#else
    throw new NotSupportedException("The platform is not supported.");
#endif

    this.cloudSession.Start();

But doing the same here gives me an error as you can see in the below screenshot. getplatformpointer

rjtshrm commented 3 years ago

To write my own functionality for ASA, I have to go through the whole sample app. It would be good If there is proper documentation for the ASA setup. I am closing this issue as I resolved the main issue.