# Android NDK native activity and gnustl

**URL:** <https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782>\
**Category:** FMOD Engine\
**Created:** [January 6, 2015, 2:10am UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782 "2015-01-06T02:10:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![robotrovsky](https://avatars.discourse-cdn.com/v4/letter/r/ee59a6/32.png) [@robotrovsky](https://qa.fmod.com/u/robotrovsky)\
**Post date:** [January 6, 2015, 2:10am UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/1 "2015-01-06T02:10:51Z")

</div>

Hello!

I have a crossplatform application, that works already perfect with fmod on windows, linux, osx and ios. Just android is not working. I tried everything, but when i call

```
fmod_system->init(32, FMOD_INIT_NORMAL, 0);

```

i get a segmentation fault. I presume the problem is, that my application is build against the gnustl as i make heavy use of C++11 features and fmod is build against the standard or stlport. The call to

```
FMOD::System_Create(&fmod_system);

```

before the init works fine.

Is it possible to get a native activity with gnustl running with fmod?

**EDIT:**

A minimal modification to the examples would just look like this:

For 3D example:

Replace in examples/eclipse/3d/jni/Application.mk  
APP\_STL := stlport\_shared  
with  
APP\_STL := gnustl\_shared

as the common\_platform.cpp and common.cpp and example.cpp (3d.cpp) will get compiled against gnustl and call fmod code which was precompiled with the standard stl from the .so, a segmentation fault will be rised.

The 3d example works perfectly with stlport\_shared, but as i already wrote, this is not an option for me, as i need the full gnustl to get the full C++11 feature set. And this works fine for every other platform.

I really wish you could help me with this. I dont want a special (clumsy) code way with OpenSL or whatever just for android as i really enjoy working with fmod.

[http://www.kandroid.org/ndk/docs/CPLUSPLUS-SUPPORT.html](http://www.kandroid.org/ndk/docs/CPLUSPLUS-SUPPORT.html)

> You can only select a single C++ runtime that all your code will  
> depend on. It is not possible to mix shared libraries compiled against  
> different C++ runtimes.

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [January 12, 2015, 12:51am UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/2 "2015-01-12T00:51:18Z")

</div>

I’ve run some tests and I haven’t had any trouble changing the example over to using gnustl\_shared in the manner you describe.

One possibility that comes to mind is you aren’t loading the gnustl\_shared lib in Java code and you have a device that doesn’t perform automatic dependency loading. Can you ensure that you have updated the System.loadlibrary entry in the main java file?

---

<div class="post-metadata">

**Author:** ![robotrovsky](https://avatars.discourse-cdn.com/v4/letter/r/ee59a6/32.png) [@robotrovsky](https://qa.fmod.com/u/robotrovsky)\
**Post date:** [January 17, 2015, 10:41pm UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/3 "2015-01-17T22:41:10Z")

</div>

Hi Mathew!

Thank you very much for looking into this issue. You are right, the example works with LoadLibrary gnustl\_shared in the java file. It must be something else that i am doing wrong. Because i still get this error in my application. I will have to review the error more and have a deeper look into the fmod android example. I think it is something i am doing wrong.

It is weird, because in the Java App i have this code:

```
	// FMOD init
	org.fmod.FMOD.init(this);
	if (!org.fmod.FMOD.checkInit())
		Log.e(TAG, " **************FMOD INIT FAILED*************");
	else
		Log.i(TAG, " **************FMOD INIT OK*************");

```

and it says it is initialized ok

I always get this segmentation fault:

```
   I/EMWE ( 179): **************FMOD INIT OK*************
    V/EMWE ( 179): Post create
    I/EMWE ( 179): Initialize fmod
    I/EMWE ( 179): FMOD System created
    I/EMWE ( 179): FMOD init
    F/libc ( 179): Fatal signal 11 (SIGSEGV) at 0x00000018 (code=1), thread 13040 (oft.livyngrinth)
    I/DEBUG ( 179): ******  ******  ******  ******  ******  ******  ******  ******
    I/DEBUG ( 179): Build fingerprint: 'google/hammerhead/hammerhead:4.4.4/KTU84P/1227136:user/release-keys'
    I/DEBUG ( 179): Revision: '11'
    I/DEBUG ( 179): pid: 13017, tid: 13040, name: oft.livyngrinth >>> com.emwesoft.livyngrinth <<<
    I/DEBUG ( 179): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000018
    I/DEBUG ( 179): r0 77059010 r1 00000000 r2 00000000 r3 000158e4
    I/DEBUG ( 179): r4 00000036 r5 77059010 r6 754a7e68 r7 00000000
    I/DEBUG ( 179): r8 00000000 r9 00005dc0 sl 00000000 fp 00000000
    I/DEBUG ( 179): ip 00000006 sp 75ddaaf0 lr 753c8fa4 pc 753db810 cpsr 600f0010
    I/DEBUG ( 179): d0 0000000000000000 d1 0000000000000000
    I/DEBUG ( 179): d2 0000000000000000 d3 0000000000000000
    I/DEBUG ( 179): d4 000000007545b140 d5 0000000000000000
    I/DEBUG ( 179): d6 0000000400000000 d7 3f80000000000158
    I/DEBUG ( 179): d8 0000000000000000 d9 0000000000000000
    I/DEBUG ( 179): d10 0000000000000000 d11 0000000000000000
    I/DEBUG ( 179): d12 0000000000000000 d13 0000000000000000
    I/DEBUG ( 179): d14 0000000000000000 d15 0000000000000000
    I/DEBUG ( 179): d16 000000e0000000e9 d17 000000e0000000e9
    I/DEBUG ( 179): d18 000000eb00000078 d19 000000ca00000036
    I/DEBUG ( 179): d20 0000005d0000000a d21 0000000a00000015
    I/DEBUG ( 179): d22 000000f30000002f d23 00000003000000ff
    I/DEBUG ( 179): d24 0000000000000000 d25 0000000000000000
    I/DEBUG ( 179): d26 ff000000ff000000 d27 ff000000ff000000
    I/DEBUG ( 179): d28 000000ff000000ff d29 000000ff000000ff
    I/DEBUG ( 179): d30 0000000000000000 d31 0000000000000000
    I/DEBUG ( 179): scr 20000012
    I/DEBUG ( 179): 
    I/DEBUG ( 179): backtrace:
    I/DEBUG ( 179): #00 pc 0004d810 /data/app-lib/com.emwesoft.livyngrinth-2/libfmod.so
    I/DEBUG ( 179): #01 pc 0003afa0 /data/app-lib/com.emwesoft.livyngrinth-2/libfmod.so
    I/DEBUG ( 179): 
    I/DEBUG ( 179): stack:
    I/DEBUG ( 179): 75ddaab0 76e408e0 [anon:libc_malloc]
    I/DEBUG ( 179): 75ddaab4 75ddaaec [stack:13040]
    I/DEBUG ( 179): 75ddaab8 00000000  
    I/DEBUG ( 179): 75ddaabc 754a7e68 /data/app-lib/com.emwesoft.livyngrinth-2/libfmod.so
    I/DEBUG ( 179): 75ddaac0 00000020  
    I/DEBUG ( 179): 75ddaac4 7545df10 /data/app-lib/com.emwesoft.livyngrinth-2/libfmod.so
    I/DEBUG ( 179): 75ddaac8 00000000  
    I/DEBUG ( 179): 75ddaacc 00000000  
    I/DEBUG ( 179): 75ddaad0 77059010 [anon:libc_malloc]
    I/DEBUG ( 179): 75ddaad4 00000000  
    I/DEBUG ( 179): 75ddaad8 754a7e68 /data/app-lib/com.emwesoft.livyngrinth-2/libfmod.so
    I/DEBUG ( 179): 75ddaadc 00000020  
    I/DEBUG ( 179): 75ddaae0 00000000  
    I/DEBUG ( 179): 75ddaae4 753d6ac0 /data/app-lib/com.emwesoft.livyngrinth-2/libfmod.so
    I/DEBUG ( 179): 75ddaae8 00000020  
    I/DEBUG ( 179): 75ddaaec 00000000  
    I/DEBUG ( 179): #00 75ddaaf0 754b265c /data/app-lib/com.emwesoft.livyngrinth-2/libfmod.so
    I/DEBUG ( 179): ........ ........
    I/DEBUG ( 179): #01 75ddaaf0 754b265c /data/app-lib/com.emwesoft.livyngrinth-2/libfmod.so
    I/DEBUG ( 179): 75ddaaf4 76e40e00 [anon:libc_malloc]
    I/DEBUG ( 179): 75ddaaf8 00000006  
    I/DEBUG ( 179): 75ddaafc 00000036  
    I/DEBUG ( 179): 75ddab00 77059010 [anon:libc_malloc]
    I/DEBUG ( 179): 75ddab04 00000000  
    I/DEBUG ( 179): 75ddab08 00000000  
    I/DEBUG ( 179): 75ddab0c 00000000  
    I/DEBUG ( 179): 75ddab10 00005dc0  
    I/DEBUG ( 179): 75ddab14 753c8fa4 /data/app-lib/com.emwesoft.livyngrinth-2/libfmod.so
    I/DEBUG ( 179): 75ddab18 00015ed8  
    I/DEBUG ( 179): 75ddab1c 00808000  
    I/DEBUG ( 179): 75ddab20 77059008 [anon:libc_malloc]
    I/DEBUG ( 179): 75ddab24 40056e6b /system/lib/libc.so (dlmalloc+4254)
    I/DEBUG ( 179): 75ddab28 00015ed0  
    I/DEBUG ( 179): 75ddab2c 4008fe00 /system/lib/libc.so

```

I will try to make a minimal example of my build. If that doesn’t solve the problem i will post it. And if it solves the problem i will tell you what i did wrong.

---

<div class="post-metadata">

**Author:** ![tido](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@tido](https://qa.fmod.com/u/tido)\
**Post date:** [February 14, 2015, 9:08am UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/4 "2015-02-14T09:08:23Z")

</div>

Hi,

Did you ever fix this crash? I got the same stack as yours on my devices as well. When I upgraded to 1.05.12, I got a different stack:

```
F/libc ( 7013): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x18 in tid 7032 (ainment.rainbow)
W/libc ( 7013): Security Level: (1), Debug inforamtion is controlled by the DUMPABLE flag.
I/ActivityManager( 912): Displayed com.bifrostentertainment.rainbow/.RainbowActivity: +331ms
D/PMS ( 912): releaseWL(13bc90f5): PARTIAL_WAKE_LOCK ActivityManager-Launch 0x1 null
I/WindowManager( 912): Screen frozen for +270ms due to Window{1d6b9118 u0 Starting com.bifrostentertainment.rainbow}
D/PMS ( 912): releaseWL(b575dd7): PARTIAL_WAKE_LOCK SCREEN_FROZEN 0x1 null
I/DEBUG ( 458): ******  ******  ******  ******  ******  ******  ******  ******
I/DEBUG ( 458): Build fingerprint: 'htc/htc_europe/htc_m8:5.0.1/LRX22C/448934.10:user/release-keys'
I/DEBUG ( 458): Revision: '0'
I/DEBUG ( 458): ABI: 'arm'
I/DEBUG ( 458): pid: 7013, tid: 7032, name: ainment.rainbow >>> com.bifrostentertainment.rainbow <<<
I/DEBUG ( 458): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x18
I/DEBUG ( 458): r0 ae680010 r1 00000000 r2 00000000 r3 000158e4
I/DEBUG ( 458): r4 00000036 r5 ae680010 r6 b0028e68 r7 00000000
I/DEBUG ( 458): r8 00000000 r9 00005dc0 sl 00000000 fp 00000000
I/DEBUG ( 458): ip 00000006 sp a1f3d6e8 lr aff4939c pc aff5bc4c cpsr 600f0010
I/DEBUG ( 458):
I/DEBUG ( 458): backtrace:
I/DEBUG ( 458): #00 pc 0004dc4c /data/app/com.bifrostentertainment.rainbow-2/lib/arm/libfmod.so
I/DEBUG ( 458): #01 pc 0003b398 /data/app/com.bifrostentertainment.rainbow-2/lib/arm/libfmod.so

```

It still crashes at the same place. I tried both with `gnustl_shared` and `c++_shared`.

---

<div class="post-metadata">

**Author:** ![tido](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@tido](https://qa.fmod.com/u/tido)\
**Post date:** [February 14, 2015, 9:23am UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/5 "2015-02-14T09:23:09Z")

</div>

Oh, I seem to have gotten a Java stack as well:

```
W/ActivityManager( 912): Force finishing activity com.bifrostentertainment.rainbow/.RainbowActivity
E/JavaBinder( 912): !!! FAILED BINDER TRANSACTION !!!
W/ActivityManager( 912): Exception thrown during pause
W/ActivityManager( 912): android.os.TransactionTooLargeException
W/ActivityManager( 912): at android.os.BinderProxy.transactNative(Native Method)
W/ActivityManager( 912): at android.os.BinderProxy.transact(Binder.java:504)
W/ActivityManager( 912): at android.app.ApplicationThreadProxy.schedulePauseActivity(ApplicationThreadNative.java:718)
W/ActivityManager( 912): at com.android.server.am.ActivityStack.startPausingLocked(ActivityStack.java:877)
W/ActivityManager( 912): at com.android.server.am.ActivityStack.finishActivityLocked(ActivityStack.java:2948)
W/ActivityManager( 912): at com.android.server.am.ActivityStack.finishTopRunningActivityLocked(ActivityStack.java:2803)
W/ActivityManager( 912): at com.android.server.am.ActivityStackSupervisor.finishTopRunningActivityLocked(ActivityStackSupervisor.java:2560)
W/ActivityManager( 912): at com.android.server.am.ActivityManagerService.handleAppCrashLocked(ActivityManagerService.java:12117)
W/ActivityManager( 912): at com.android.server.am.ActivityManagerService.makeAppCrashingLocked(ActivityManagerService.java:12009)
W/ActivityManager( 912): at com.android.server.am.ActivityManagerService.crashApplication(ActivityManagerService.java:12717)
W/ActivityManager( 912): at com.android.server.am.ActivityManagerService.handleApplicationCrashInner(ActivityManagerService.java:12223)
W/ActivityManager( 912): at com.android.server.am.NativeCrashListener$NativeCrashReporter.run(NativeCrashListener.java:86)
D/PMS ( 912): acquireWL(10f38745): PARTIAL_WAKE_LOCK ActivityManager-Launch 0x1 912 1000 null
I/ActivityManager( 912): Recipient 11266
I/WindowState( 912): WIN DEATH: Window{3ede4d53 u0 com.bifrostentertainment.rainbow/com.bifrostentertainment.rainbow.RainbowActivity}
```

---

<div class="post-metadata">

**Author:** ![tido](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@tido](https://qa.fmod.com/u/tido)\
**Post date:** [February 14, 2015, 8:42pm UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/6 "2015-02-14T20:42:36Z")

</div>

Run with logging libraries:

```
I/fmod ( 8013): [LOG] System::create : Header version = 1.5.12. Current version = 1.5.12.
I/fmod ( 8013): [ERR] FMOD_OS_Output_GetDefault : JavaVM::GetEnv returned -2.
I/fmod ( 8013): [LOG] SystemI::init : FMOD Studio Version: 00010512 (61695)
I/fmod ( 8013): [LOG] SystemI::init : maxchannels = 16, flags = 00000000, extradriverdata = 0x0
I/fmod ( 8013): [LOG] SystemI::close :
I/fmod ( 8013): [LOG] SystemI::close : Stop all sounds
I/fmod ( 8013): [LOG] SystemI::close : Remove miscllaneous DSP stuff.
I/fmod ( 8013): [LOG] SystemI::close : done.
I/fmod ( 8013):
I/fmod ( 8013): [LOG] FMOD_OS_Init : Detected Neon instruction support, will use Neon optimized mixing and resampling.
I/fmod ( 8013): [ERR] AndroidAssetFile::registerLib : JavaVM::GetEnv returned -2.
I/fmod ( 8013): [ERR] FMOD_OS_Output_GetDefault : JavaVM::GetEnv returned -2.
I/fmod ( 8013): [ERR] assert : assertion: 'mOutput' failed
F/libc ( 8013): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x18 in tid 8033 (ainment.rainbow)
W/libc ( 8013): Security Level: (1), Debug inforamtion is controlled by the DUMPABLE flag.
```

---

<div class="post-metadata">

**Author:** ![robotrovsky](https://avatars.discourse-cdn.com/v4/letter/r/ee59a6/32.png) [@robotrovsky](https://qa.fmod.com/u/robotrovsky)\
**Post date:** [February 17, 2015, 11:38am UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/7 "2015-02-17T11:38:18Z")

</div>

Hi Tommy!

I still haven’t had the time to fix this on android. But i presume, that i mixed more libraries with different stl’s or/and maybe compiled against different fmod headers. Or maybe we are also mixing hard-float and soft-float. I use hardfloat for my other libraries. I think it is an error in our build-setup with multiple libraries. When i fixed this, i will let you know. Please tell me also, if you fixed this.

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [June 29, 2015, 11:14pm UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/8 "2015-06-29T23:14:08Z")

</div>

> [@](#):
>
> Hi Tommy!
> 
> I still haven’t had the time to fix this on android. But i presume, that i mixed more libraries with different stl’s or/and maybe compiled against different fmod headers. Or maybe we are also mixing hard-float and soft-float. I use hardfloat for my other libraries. I think it is an error in our build-setup with multiple libraries. When i fixed this, i will let you know. Please tell me also, if you fixed this.

FMOD does not use the hard floating point convention, all floats are passed using general purpose registers (not VFP registers).

---

<div class="post-metadata">

**Author:** ![bennherrera1](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@bennherrera1](https://qa.fmod.com/u/bennherrera1)\
**Post date:** [June 28, 2015, 5:30am UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/9 "2015-06-28T05:30:00Z")

</div>

The problem is not with gnustl.

```
I/fmod ( 8013): [LOG] System::create : Header version = 1.5.12. Current version = 1.5.12.
I/fmod ( 8013): [ERR] FMOD_OS_Output_GetDefault : **\*\*\* JavaVM::GetEnv returned -2 \*\*\***
```

I’ve been dealing with this as well. Here’s what I’ve got so far:

- If you are running from a NativeActivity your native code is not running on the main activity thread.
- The NativeActivity java class is instantiated and run on the main thread.
- When ANativeActivity\_onCreate() is called (from the main thread) a new thread is created that is the 'main' thread for the native code. 
- Error code -2 is JNI\_EDETACHED. This means that JavaVM::GetEnv() was called from a thread the JVM is currently not bound to.

JavaVM::AttachCurrentThread() does not appear to be getting called as a fallback when GetEnv fails.

---

<div class="post-metadata">

**Author:** ![bennherrera1](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@bennherrera1](https://qa.fmod.com/u/bennherrera1)\
**Post date:** [June 28, 2015, 6:59am UTC](https://qa.fmod.com/t/android-ndk-native-activity-and-gnustl/11782/10 "2015-06-28T06:59:26Z")

</div>

> [@](#):
>
> The problem is not with gnustl.
> 
> ```
> I/fmod ( 8013): [LOG] System::create : Header version = 1.5.12. Current version = 1.5.12.
> I/fmod ( 8013): [ERR] FMOD_OS_Output_GetDefault : **\*\*\* JavaVM::GetEnv returned -2 \*\*\***
> ```
> 
> I’ve been dealing with this as well. Here’s what I’ve got so far:
> 
> - If you are running from a NativeActivity your native code is not running on the main activity thread.
> - The NativeActivity java class is instantiated and run on the main thread.
> - When ANativeActivity\_onCreate() is called (from the main thread) a new thread is created that is the 'main' thread for the native code. 
> - Error code -2 is JNI\_EDETACHED. This means that JavaVM::GetEnv() was called from a thread the JVM is currently not bound to.
> 
> JavaVM::AttachCurrentThread() does not appear to be getting called as a fallback when GetEnv fails.

Surrounding the native code calls for setting up the FMOD system (FMOD::System\_Create(), system-\>getNumDrivers(), system-\>getDriverInfo(), system-\>init()) with a pair of AttachCurrentThread()/DetachCurrentThread() calls fixed the issue.
