# Native android ext and .so

**URL:** <https://forum.defold.com/t/native-android-ext-and-so/77859>\
**Category:** Questions\
**Created:** [August 22, 2024, 6:53pm UTC](https://forum.defold.com/t/native-android-ext-and-so/77859 "2024-08-22T18:53:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SalavatR](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/salavatr/32/43217_2.png) [@SalavatR](https://forum.defold.com/u/SalavatR)\
**Post date:** [August 22, 2024, 6:53pm UTC](https://forum.defold.com/t/native-android-ext-and-so/77859/1 "2024-08-22T18:53:42Z")

</div>

"The topic of .so files has come up many times, and I need to ask about it again. I’m not very familiar with them, so I have a question: how do I add a .so library in an Android build?  
No matter how I try, I can’t access the methods from the .so file.  
Also, wherever I add it to the .apk, ‘\_test.so’ is appended to the file name.  
unpacked aar

 ![image](https://forum-defold.b-cdn.net/uploads/default/original/3X/b/0/b08ca22293d515c2de58545bfbbdb7f46d1dc15d.jpeg)  
apk  
 ![image](https://forum-defold.b-cdn.net/uploads/default/original/3X/2/a/2a7604762769dfccb9429b19560a69850c0f6af1.png)  
NE structure  
 ![image](https://forum-defold.b-cdn.net/uploads/default/original/3X/6/3/638b6a76a932a9e00e2709056b8cae1c39a21d01.png)

cmake from sdl example  
[CMakeLists.txt](https://forum.defold.com/uploads/short-url/va3UQDpnxq0j3d4rpNYcOZJSFrV.txt) (6.3 KB)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 22, 2024, 9:16pm UTC](https://forum.defold.com/t/native-android-ext-and-so/77859/2 "2024-08-22T21:16:23Z")

</div>

A shared object file is loaded and used at runtime by the main application. You usually include .so files in your final application bundle from a Gradle dependency with an .aar file containing .so files:

> **[Anatomy of an AAR file - Create an Android library  |  Android Studio  | ...](https://developer.android.com/studio/projects/android-library#aar-contents)**
>
> An Android library is structurally the same as an Android app module. It includes everything needed to build an app, including source code, resource files, and an Android manifest. | Find out how to create an Android library.

When you bundle for Android and you have a dependency which includes a /jni folder with .so files we get them from the build server and include them in your .aab/.apk file. In .aab files the .so files are stored in /base/lib/PLATFORM/foobar.so:

> **[The Android App Bundle format  |  Other Play guides  |  Android Developers](https://developer.android.com/guide/app-bundle/app-bundle-format)**
>
> This document details the structure of an Android App Bundle (.aab file), explaining its modules, asset packs, and metadata. It describes how Google Play uses the app bundle to generate optimized APKs, including split APKs, base APKs, configuration...

You should be able to manually include .so files like you did by putting them in your extension in /res/android …

Although this looks a bit odd:

 ![Screenshot 2024-08-22 at 23.13.00](https://forum-defold.b-cdn.net/uploads/default/original/3X/6/a/6a08eafc39359915780c46011cfd64849ceda957.png)

Shouldn’t it be:

/res/arm64-android/lib/arm64-v8a/libgadsme.so

---

<div class="post-metadata">

**Author:** ![SalavatR](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/salavatr/32/43217_2.png) [@SalavatR](https://forum.defold.com/u/SalavatR)\
**Post date:** [August 27, 2024, 7:44am UTC](https://forum.defold.com/t/native-android-ext-and-so/77859/3 "2024-08-27T07:44:19Z")

</div>

![image](https://forum-defold.b-cdn.net/uploads/default/original/3X/2/c/2c8fd240de0eaeac2b6c51b016f58cf9082c67ce.png)

yes! now it builds, and i can operate with code from Java side, but if i try to call same methods from C++, i got error

```
2024-08-27 12:37:12.762 7166-7204 om.example.todo com.example.todo A java_vm_ext.cc:591] JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception java.lang.ClassNotFoundException: Didn't find class "gadsme.support.resource.Resource" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64, /system/lib64, /system_ext/lib64]]
                                                                                                    java_vm_ext.cc:591] at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:259)
                                                                                                    java_vm_ext.cc:591] at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)
                                                                                                    java_vm_ext.cc:591] at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
                                                                                                    java_vm_ext.cc:591] 
                                                                                                    java_vm_ext.cc:591] in call to NewGlobalRef

```

but class gadsme.support.resource.Resource is already in dex file

 ![image](https://forum-defold.b-cdn.net/uploads/default/original/3X/e/b/ebd10941ae34696f8444d0e389105ae1fe193391.png)

also i don’t understand why i got two files \_test.so and .so of lib

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 27, 2024, 9:16am UTC](https://forum.defold.com/t/native-android-ext-and-so/77859/4 "2024-08-27T09:16:28Z")

</div>

> [@SalavatR](#):
>
> also i don’t understand why i got two files \_test.so and .so of lib

Me neither, that looks very strange

> [@SalavatR](#):
>
> yes! now it builds, and i can operate with code from Java side, but if i try to call same methods from C++, i got error

Can you share a bit of code how you set this up?

---

<div class="post-metadata">

**Author:** ![SalavatR](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/salavatr/32/43217_2.png) [@SalavatR](https://forum.defold.com/u/SalavatR)\
**Post date:** [August 27, 2024, 10:52am UTC](https://forum.defold.com/t/native-android-ext-and-so/77859/5 "2024-08-27T10:52:29Z")

</div>

My cpp file:

```
#define EXTENSION_NAME DefGadsmeSDK
#define LIB_NAME "DefGadsmeSDK"
#define MODULE_NAME "def_gadsme_sdk"
#define DLIB_LOG_DOMAIN LIB_NAME

#include <dmsdk/sdk.h>
#include "def_gadsme_sdk.h"

#include <cmath>
#include <iostream>

// Import the SDK header on other platforms

#include "GadsmeSDK.h"

using namespace gadsme;

namespace dmGadsmeSDK {
static std::string str = "my_key";

static void set_sandbox(bool value) {
    // Gadsme_setForceSandbox(true);
}

static const luaL_reg Module_methods[] =
{
    {0, 0}
};

static void LuaInit(lua_State* L)
{
    int top = lua_gettop(L);
    luaL_register(L, MODULE_NAME, Module_methods);
    lua_pop(L, 1);
    assert(top == lua_gettop(L));
}

dmExtension::Result InitializeDefGadsmeSDK(dmExtension::Params* params)
{
    LuaInit(params->m_L);
    dmLogInfo("Registered extension DefGadsmeSDK");
    Initialize_Ext();
    ////Delayed CRASH after calling Gadsme_setGameId        
    Gadsme_setGameId(Gadsme_str(str));
    set_sandbox(true);
    // Gadsme_init();

   
    return dmExtension::RESULT_OK;
}

DM_DECLARE_EXTENSION(EXTENSION_NAME, LIB_NAME, 0, 0, InitializeDefGadsmeSDK, 0, 0, 0)
    
} //namespace

```

Part of GadsmeSDK.h

```
#define GADSME_SDK_H

#include <string>
#include <memory>
#include <functional>
#include <vector>
#include <map>
#include <utility>

#if defined( __ANDROID__ )
    #define GADSME_ANDROID 1
#elif defined(_WIN32)
    #define GADSME_WINDOWS 1
#elif defined( __APPLE__ )
    #include <TargetConditionals.h>
    #if TARGET_OS_IPHONE
        #define GADSME_IOS 1
    #else
        #define GADSME_MAC 1
    #endif
#endif

#if GADSME_ANDROID
#include <jni.h>
#endif

#if defined _WIN32 || defined __CYGWIN__
  #ifdef BUILDING_GADSME
    #ifdef __GNUC__
      #define GADSME_PUBLIC __attribute__ ((dllexport))
    #else
      #define GADSME_PUBLIC __declspec(dllexport)
    #endif
  #else
    #ifdef __GNUC__
      #define GADSME_PUBLIC __attribute__ ((dllimport))
    #else
      #define GADSME_PUBLIC __declspec(dllimport)
    #endif
  #endif
  #define GADSME_HIDDEN
#else
  #if __GNUC__ >= 4
    #define GADSME_PUBLIC __attribute__ ((visibility ("default")))
    #define GADSME_HIDDEN __attribute__ ((visibility ("hidden")))
  #else
    #define GADSME_PUBLIC
    #define GADSME_HIDDEN
  #endif
#endif

/**
 * Gadsme SDK namespace.
 *
 * The SDK can be initialized from any thread, but any following call
 * should be done from this same thread. Calling from another thread will
 * cause a crash, unless using a method which is explicitly documented as
 * being thread safe, and thus can be called from any thread without issues.
 */
namespace gadsme {

/// Type aliases

    #if defined(_MSC_VER) && _MSC_VER < 1900

    typedef std::shared_ptr< std::vector<unsigned char> > Gadsme_Bytes;

    typedef std::shared_ptr<std::string> Gadsme_String;

    typedef std::shared_ptr< std::map< std::string, gadsme::Gadsme_String> > Gadsme_StringStringMap;

    typedef std::shared_ptr< std::vector<int> > Gadsme_IntArray;

    #else

    using Gadsme_Bytes = std::shared_ptr< std::vector<unsigned char> >;

    using Gadsme_String = std::shared_ptr<std::string>;

    using Gadsme_StringStringMap = std::shared_ptr< std::map< std::string, gadsme::Gadsme_String> >;

    using Gadsme_IntArray = std::shared_ptr< std::vector<int> >;

    #endif

    ...............................................

    GADSME_PUBLIC Gadsme_String Gadsme_str(std::string str);

    ...............................................
   
    GADSME_PUBLIC void Gadsme_setGameId(Gadsme_String gameId);

    ...............................................
}

```

ext.manifest

```
name: "DefGadsmeSDK"

platforms:
    armv7-android:
      context:
        defines: [' __ANDROID__']
    arm64-android:
      context:
        defines: [' __ANDROID__']

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 27, 2024, 11:38am UTC](https://forum.defold.com/t/native-android-ext-and-so/77859/6 "2024-08-27T11:38:21Z")

</div>

> [@SalavatR](#):
>
> `defines: [' __ANDROID__']`

I’m pretty sure this is unnecessary.

> [@SalavatR](#):
>
> ```lua
> ////Delayed CRASH after calling Gadsme_setGameId        
> Gadsme_setGameId(Gadsme_str(str));
> 
> ```

So you are able to call the function in the library? But it crashes after a while? I’m curious to learn what the Android device log says. Have you looked for clues as to why it crashes?

---

<div class="post-metadata">

**Author:** ![SalavatR](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/salavatr/32/43217_2.png) [@SalavatR](https://forum.defold.com/u/SalavatR)\
**Post date:** [August 27, 2024, 11:56am UTC](https://forum.defold.com/t/native-android-ext-and-so/77859/7 "2024-08-27T11:56:31Z")

</div>

got this error:

```
textava_vm_ext.cc:591] JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception java.lang.ClassNotFoundException: Didn't find class "gadsme.support.resource.Resource" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64, /system/lib64, /system_ext/lib64]]
java_vm_ext.cc:591] at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:259)
java_vm_ext.cc:591] at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)    
java_vm_ext.cc:591] at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
java_vm_ext.cc:591] 
java_vm_ext.cc:591] in call to NewGlobalRef

```
