# CNSDK init error in C++/OpenGL 3D model viewer app

**URL:** <https://forums.leialoft.com/t/cnsdk-init-error-in-c-opengl-3d-model-viewer-app/3944>\
**Category:** Core SDK Android\
**Created:** [December 8, 2023, 4:49am UTC](https://forums.leialoft.com/t/cnsdk-init-error-in-c-opengl-3d-model-viewer-app/3944 "2023-12-08T04:49:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lostart](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@lostart](https://forums.leialoft.com/u/lostart)\
**Post date:** [December 8, 2023, 4:49am UTC](https://forums.leialoft.com/t/cnsdk-init-error-in-c-opengl-3d-model-viewer-app/3944/1 "2023-12-08T04:49:57Z")

</div>

Attempting to integrate Leia CNSDK into existing 3D model viewer app which uses C++ and OpenGL.

Basic CNSDK integration (download and extract .zip and include .aar) working.

But when attempting to call `doCNSDKInit()`, getting this error:

```auto
[LeiaSDK] [info] version: 0.7.28 (refs/heads/main - 1c3d3935a7689b416e48d5ebfeaddb3b9fd03fff)
[LeiaSDK] [error] leia_core *leia::sdk::leia_core_init_async(leia_core_init_configuration *):
    android platform cannot be initialized without JavaVM. Set PlatformInitArgs.javaVM in
    leia::AndroidPlatform::AndroidPlatform(const leia::PlatformInitArgs &)
    D:/a/CNSDK/CNSDK/leia/common/android/androidPlatform.cpp:114 (-2)

```

Will probably be able to figure it out eventually but if anyone else has been able to solve this please let me know, thank you

---

<div class="post-metadata">

**Author:** ![lostart](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@lostart](https://forums.leialoft.com/u/lostart)\
**Post date:** [December 9, 2023, 2:57am UTC](https://forums.leialoft.com/t/cnsdk-init-error-in-c-opengl-3d-model-viewer-app/3944/3 "2023-12-09T02:57:24Z")

</div>

_(Leia, please allow editing of posts; had to delete and re-create to fix a typo and add a bit of text)_

For others facing this problem, there is a simple workaround (which hopefully can be refined in additional comments from people with better knowledge).

A bit of background: any android app with a non-trivial amount of C++ is highly likely to define function `JNI_OnLoad()`. Unfortunately this creates a minor conflict since the Leia CNSDK also defines `JNI_OnLoad()`, and the top level app version of the function will take precedence; i.e. Leia SDK `JNI_OnLoad()` won’t be called, and this leads directly to the error reported above.

This can be easily verified by adding a dummy `JNI_OnLoad()` in file `app/src/main/cpp/native-lib.cpp` in the CNSDK sample app. Build and launch, and it will crash with the same error.

It is common practice to store a global handle to the `JavaVM *` passed into `JNI_OnLoad()` (e.g. `g_vm`). So by simply adding one line in the CNSDK init function, we can fix the error and get our app running (this snippet is from `doCNSDKInit()`):

```auto
        leia::sdk::CoreInitConfiguration config;
        config.SetPlatformAndroidJavaVM(g_vm); // <-- Add this line (g_vm should be set in JNI_OnLoad())
        config.SetPlatformAndroidHandle(LEIA_CORE_ANDROID_HANDLE_ACTIVITY, activity);

```

HTH

---

<div class="post-metadata">

**Author:** ![lostart](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@lostart](https://forums.leialoft.com/u/lostart)\
**Post date:** [December 9, 2023, 4:10am UTC](https://forums.leialoft.com/t/cnsdk-init-error-in-c-opengl-3d-model-viewer-app/3944/5 "2023-12-09T04:10:12Z")

</div>

_(Due to lack of permission to edit own post, had to delete and re-create instead of updating in-place)_

If you see this error in your “real world” (non-“toy”) project:

```auto
[LeiaSDK] [info] version: 0.7.28 (refs/heads/main - 1c3d3935a7689b416e48d5ebfeaddb3b9fd03fff)
[LeiaSDK] [error] leia_core *leia::sdk::leia_core_init_async(leia_core_init_configuration *):
    JNI call failed: methodId = jniEnv->GetMethodID(clazz, name, signature) in 
    leia::jni::InstanceMethod<_jobject *()>::InstanceMethod(
        JNIEnv *, jclass, const char *, const char *
    ) [T = _jobject *()] D:/a/CNSDK/CNSDK\leia/common/android/jniUtils.hpp:556 (-2)
java.lang.NoSuchMethodError: no non-static method 
    "Ljava/lang/Class;.getApplication()Landroid/app/Application;"

```

it may be necessary to pass the activity explicitly via 3rd parameter, rather than the way the sample app does it where because the native function is defined in `MainActivity`, the 2nd parameter implicitly contains a reference to the activity, e.g.

_Managed-side_

```auto
Before
public native boolean doCNSDKInit();

After
public native boolean doCNSDKInit(Activity activity);

```

_Native-side_

```auto
Before
Java_com_leia_cnsdkgettingstartedglandroidnative_MainActivity_doCNSDKInit(
        JNIEnv*,
        jobject activity)

After
Java_com_leia_cnsdkgettingstartedglandroidnative_MainActivity_doCNSDKInit(
        JNIEnv*,
        jobject jObj,
        jobject activity)

```

HTH
