title: Android KitKat emulator native library JNI ERROR (app bug)
body: <p>While testing my app with Android 4.4.2 API level 19 emulator, first call to native code causes a crash. This is the logcat:</p>
  
  <pre><code>01-29 12:12:32.639: E/ALLOC(1232): Requiring 1228800bytes
  01-29 12:12:32.639: D/dalvikvm(1232): Trying to load lib /data/app-lib/it.jcsoftmobile.snailcamera.snailcamerapro-1/liblowlevelpixelworks.so 0xb4cd9cc8
  01-29 12:12:32.639: D/dalvikvm(1232): Added shared lib /data/app-lib/it.jcsoftmobile.snailcamera.snailcamerapro-1/liblowlevelpixelworks.so 0xb4cd9cc8
  01-29 12:12:32.639: D/dalvikvm(1232): No JNI_OnLoad found in /data/app-lib/it.jcsoftmobile.snailcamera.snailcamerapro-1/liblowlevelpixelworks.so 0xb4cd9cc8, skipping init
  01-29 12:12:32.679: E/dalvikvm(1232): JNI ERROR (app bug): negative buffer capacity: -5457750921690562560
  01-29 12:12:32.679: I/dalvikvm(1232): "main" prio=5 tid=1 RUNNABLE
  01-29 12:12:32.679: I/dalvikvm(1232):   | group="main" sCount=0 dsCount=0 obj=0xb4a2aca8 self=0xb70c4380
  01-29 12:12:32.689: I/dalvikvm(1232):   | sysTid=1232 nice=0 sched=0/0 cgrp=apps handle=-1225698988
  01-29 12:12:32.689: I/dalvikvm(1232):   | state=R schedstat=( 1140000000 1520000000 1339 ) utm=83 stm=31 core=0
  01-29 12:12:32.729: I/dalvikvm(1232):   at it.jcsoftmobile.snailcamera.ImageLab.NativeAlloc(Native Method)
  </code></pre>
  
  <p>This is the logcat of the same piece of code, same app, same screen-size emulator, ... the only thing that differs is Android version (4.3):</p>
  
  <pre><code>01-28 19:52:51.543: E/ALLOC(12661): Requiring 1228800bytes
  01-28 19:52:51.563: D/dalvikvm(12661): Trying to load lib /data/app-lib/it.jcsoftmobile.snailcamera.snailcamerapro-1/liblowlevelpixelworks.so 0x416e2fd0
  01-28 19:52:51.613: D/dalvikvm(12661): Added shared lib /data/app-lib/it.jcsoftmobile.snailcamera.snailcamerapro-1/liblowlevelpixelworks.so 0x416e2fd0
  01-28 19:52:51.613: D/dalvikvm(12661): No JNI_OnLoad found in /data/app-lib/it.jcsoftmobile.snailcamera.snailcamerapro-1/liblowlevelpixelworks.so 0x416e2fd0, skipping init
  01-28 19:52:51.922: D/dalvikvm(12661): GC_FOR_ALLOC freed 68K, 4% free 3447K/3584K, paused 302ms, total 303ms
  01-28 19:52:51.922: I/dalvikvm-heap(12661): Grow heap (frag case) to 3.865MB for 460816-byte allocation
  </code></pre>
  
  <p>I read something about ART vm, but i think this is not the case, because logcat explicitly tags both outputs as "dalvikvm".</p>
  
  <p>My app was running ok on every tested devices and emulators, starting from GB 2.3.3 to JB 4.3, KitKat is the first version that gave me problems.</p>
  
  <p>Maybe KitKat has a different loadLibrary management? Should i implement (i dunno how!) JNI_OnLoad?</p>
  
  <p>Any idea?</p>
  
  <p><br>
  <strong>EDIT</strong></p>
  
  <p>I tested my app on a Nexus 7 with KK 4.4.2, and it's working fine.</p>
  
  <p>Here's the C code that generates the exception:</p>
  
  <pre><code>jobject Java_my_package_NativeAlloc(JNIEnv* env, jlong numBytes) {
      void *ptr = (char*)malloc(numBytes);
      return (*env)-&gt;NewDirectByteBuffer(env, ptr, numBytes);
  }
  </code></pre>
  
  <p>and here's the Java code that calls it:</p>
  
  <pre><code>public native static ByteBuffer NativeAlloc(int size);
  </code></pre>
  
  <p>There is of course an issue, as Java <em>int size</em> (32-bit) becomes a JNI <em>jlong numBytes</em> (64-bit), my fault.
  But why this code worked fine until 4.4.2 emulator?</p>