Android: Most efficient way to pass some read-only bytes to native C++

I have an Android project (targeting Android 1.6 and up) which includes native code written in C/C++, accessed via NDK. I’m wondering what the most efficient way is to pass an array of bytes from Java through NDK to my JNI glue layer. My concern is around whether or not NDK for Android will copy the array of bytes, or just give me a direct reference. I need read-only access to the bytes at the C++ level, so any copying behind the scenes would be a waste of time from my perspective.

It’s easy to find info about this on the web, but I’m not sure what is the most pertinent info. Examples:

Get the pointer of a Java ByteBuffer though JNI

So does anyone know what is the best (most efficient, least copying) way to do this in the current NDK? GetByteArrayRegion? GetByteArrayElements? Something else?


According to the documentation, GetDirectBufferAddress will give you the reference without copying the array.

However, to call this function you need to allocate a direct buffer with ByteBuffer.allocateDirect() instead of a simple byte array. It has a counterpart as explained here :

A direct byte buffer may be created by invoking the allocateDirect
factory method of this class. The buffers returned by this method
typically have somewhat higher allocation and deallocation costs than
non-direct buffers. The contents of direct buffers may reside outside
of the normal garbage-collected heap, and so their impact upon the
memory footprint of an application might not be obvious. It is
therefore recommended that direct buffers be allocated primarily for
large, long-lived buffers that are subject to the underlying system’s
native I/O operations. In general it is best to allocate direct
buffers only when they yield a measureable gain in program


OAuth integration with Vimeo using Scribe

I have been able to successfully get an access token from Vimeo using the Scribe API.

However, when I try to access a protected resource, I get an invalid signature error. My OAuthService that I use to try an access a protected resource, looks like:

OAuthService service = new ServiceBuilder()

Then, I make a request doing the following:

  OAuthRequest orequest = new OAuthRequest(Verb.GET, "");
  orequest.addBodyParameter("method", "vimeo.videos.upload.getQuota");

This fails and tell me that the signature is invalid.


The problem is,

  orequest.addBodyParameter("method", "vimeo.videos.upload.getQuota");

Scribe then added this parameter to the base string used to form the signature. Vimeo saw that I was doing a GET and that the method parameter was in the request body and not query string, so it did not include it in the base string. Hence, the signature Vimeo expected was different than the one Scribe generated.

I am doing a GET however so I should be passing this parameter on the query string,

  orequest.addQuerystringParameter("method", "vimeo.videos.upload.getQuota");

This works, as would:

  OAuthRequest orequest = new OAuthRequest(Verb.POST, "");
  orequest.addBodyParameter("method", "vimeo.videos.upload.getQuota");

How to know which view inside a specific ListView item that was clicked

I’m having a ListView with my own custom adapter derived from a BaseAdapter. Each item in the ListView has sub items such as ImageView and TextView.

How can I know which one of these sub items the user clicked? Is it possible to attach a listener in the getView function for example, or could that be a problem?

/ Henrik

Edit: Currently I have a onItemClick in the Activity which contains the ListView. Is there any good way to know which sub item in a specific item in the ListView which has been pressed by checking the params in the onItemClick.

public void onItemClick(AdapterView<?> a, View v, int pos, long id) {


You can do it. You need to modify your getView method:

public View getView(final int position, View row, final ViewGroup parent) {
    YourWrapper wrapper = null;
    if (row == null) {
        row = getLayoutInflater().inflate(R.layout.your_row, parent, false);
        wrapper = new YourWrapper(row);
    } else {
        wrapper = (YourWrapper) row.getTag();

    wrapper.yourSubView.setOnClickListener(new View.OnClickListener()   
    public void onClick(View v) {
        // do something

Is Java’s System.arraycopy() efficient for small arrays?

Is Java’s System.arraycopy() efficient for small arrays, or does the fact that it’s a native method make it likely to be substantially less efficient than a simple loop and a function call?

Do native methods incur additional performance overhead for crossing some kind of Java-system bridge?


Expanding a little on what Sid has written, it’s very likely that System.arraycopy is just a JIT intrinsic; meaning that when code calls System.arraycopy, it will most probably be calling a JIT-specific implementation (once the JIT tags System.arraycopy as being “hot”) that is not executed through the JNI interface, so it doesn’t incur the normal overhead of native methods.

In general, executing native methods does have some overhead (going through the JNI interface, also some internal JVM operations cannot happen when native methods are being executed). But it’s not because a method is marked as “native” that you’re actually executing it using JNI. The JIT can do some crazy things.

Easiest way to check is, as has been suggested, writing a small benchmark, being careful with the normal caveats of Java microbenchmarks (warm up the code first, avoid code with no side-effects since the JIT just optimizes it as a no-op, etc).


NoClassDefFoundError when running Scala jar file

I have a small application with RemoteActors, and I want to make a jar file from it. When I try to execute it it gets this exception:

Exception in thread "main" java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(
  at  sun.reflect.DelegatingMethodAccessorImpl.invoke(
  at java.lang.reflect.Method.invoke(
  at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(
Caused by: java.lang.NoClassDefFoundError: scala/actors/Combinators
  at pingpong.PingApp$.main(PingApp.scala:5)
  at pingpong.PingApp.main(PingApp.scala)
  at pingpong.ScalaEntryPoint.main(
  ... 5 more
Caused by: java.lang.ClassNotFoundException: scala.actors.Combinators
  at Method)
  at java.lang.ClassLoader.loadClass(
  at java.lang.ClassLoader.loadClass(
  ... 8 more

I have included scala-library.jar in my jar file, and it is in the classpath. Otherwise java would stop at finding the class ScalaObject.


The problem was a version mismatch between the Scala plugin on Eclipse and the installed Scala version. actors/Combinators.scala appeared in 2.8, and the Eclipse plugin was 2.8 however I included 2.7.7 scala-library.jar

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..