# Android shared-library for Unity

**URL:** <https://forum.juce.com/t/android-shared-library-for-unity/34015>\
**Category:** Android\
**Created:** [June 20, 2019, 12:16pm UTC](https://forum.juce.com/t/android-shared-library-for-unity/34015 "2019-06-20T12:16:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garrilla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/garrilla/32/103_2.png) [@garrilla](https://forum.juce.com/u/garrilla)\
**Post date:** [June 20, 2019, 12:16pm UTC](https://forum.juce.com/t/android-shared-library-for-unity/34015/1 "2019-06-20T12:16:21Z")

</div>

I’m trying to create an android shared library for use in Unity3D.

I have it building in Android Studio with the NDK and I can get Unity to use it deploying to an emulator and device

but I’m not having any success at getting JUCE to run.

i’m not clear how I should initialiseJUCE, or interact with the deviceManager etc

```cpp
#include "jni.h"
#include "shalib.h" // just has #include "../JuceLibraryCode/JuceHeader.h"

extern "C" JNIEXPORT const char* JNICALL
Java_com_example_droidtest(
        JNIEnv* env,
        jobject /* this */) {
        //Thread::initialiseJUCE(env, whatgoeshere?); // this crashes the app

        AudioFormatManager formatManager;
        AudioDeviceManager deviceManager;

        formatManager.registerBasicFormats();
        //deviceManager.initialiseWithDefaultDevices(0, 2);// this crashes the app

   // returns right at the end if the app is still alive!!
   std::string hello = "This *is* the string you are looking for";
    return hello.c_str();
}

```

---

<div class="post-metadata">

**Author:** ![garrilla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/garrilla/32/103_2.png) [@garrilla](https://forum.juce.com/u/garrilla)\
**Post date:** [June 25, 2019, 12:54pm UTC](https://forum.juce.com/t/android-shared-library-for-unity/34015/2 "2019-06-25T12:54:05Z")

</div>

I’ve been hacking around with this and I think I’m getting closer.

i’m building an AAR, which is hosted in unity3D and I can call both java (where, i think, i’m getting JUCE to initialise) and native classes.(via Java-\>JNI & directly)

i’m able to set-up a device manager and get back the Sample Rate

```cpp
        AudioDeviceManager deviceManager;
        deviceManager.initialiseWithDefaultDevices(0, 2);
        auto* device = deviceManager.getCurrentAudioDevice();
        i = device->getCurrentSampleRate(); // i'm returning tis via another method

        deviceManager.playTestSound();

```

returning `i` back to Unity3D gives 48000

Does this confirm that JUCE is initialised?

`deviceManager.playTestSound();` does not cause any kind of exception, but does not play the tone. Any suggestions?

---

<div class="post-metadata">

**Author:** ![garrilla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/garrilla/32/103_2.png) [@garrilla](https://forum.juce.com/u/garrilla)\
**Post date:** [June 26, 2019, 12:43pm UTC](https://forum.juce.com/t/android-shared-library-for-unity/34015/3 "2019-06-26T12:43:51Z")

</div>

got the audio to work now.

need to add `Thread::sleep(5000);`
