Naming conventions to the methods written in native shared library

713 views
Skip to first unread message

Srinivas Ganji

unread,
Dec 2, 2013, 9:13:47 AM12/2/13
to andro...@googlegroups.com
Dear All,

I am new to this Android NDK development. I have gone through the OVERVIEW.html and ANDROID-MK.html files which are available in doc/ directory after extracting the NDK sources android-ndk-r9b-linux-x86.tar.bz2

After reading these html files, I have written my own Android.mk and myhwlib.c files in a new project folder and able to build the shared library file. The contents are as follows.

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := myhwlib
LOCAL_SRC_FILES := myhwlib.c

include $(BUILD_SHARED_LIBRARY)

myhwlib.c

#include <string.h>
#include <jni.h>

/* 
 * This is a trivial JNI example where we use a native method
 * to return a new VM String. 
 *
 * This source file implements a simple shared library that implements 
 * a native method that returns a string to the VM application.
 */
jstring
myhelloworld(JNIEnv* env, jobject thiz)
{
    return (*env)->NewStringUTF(env, "My Hello World String from JNI!");
}

Here are my doubts.

1. Is there any specific style of giving the names to the methods written C source file?
    As I am seeing every method starts with Java_com_
    
2. How can I write the application for using this newly build library file? Please provide a set of steps, if feasible.

If there any useful links, please forward to me. 

Thanks in advance.

Regards,
Srinivas

RichardC

unread,
Dec 2, 2013, 1:14:41 PM12/2/13
to andro...@googlegroups.com
The Android NDK makes use of Java Native Interface (JNI) specification you need to read about this and there plenty of online references and examples.  Search for JNI in Google and start reading, and/or http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html

With regard to your questions:
1) Yes the method names/signatures are specified by JNI and can be generated from your Java code by call javah on your Java files. http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javah.html

2) The Android NDK is not meant to be used to write your whole application but to enhance or enable some functionality of your Android Java program.  See the first paragraph http://developer.android.com/tools/sdk/ndk/index.html .  You should therefore start with a Java application and where you require it add native code.  

2a) There is an advanced topic regarding native applications which still require Java to invoke them although this can be provided by the platform.  The NDK documentation for native activities can be found in <NDK>/docs/NATIVE-ACTIVITY.html

Srinivas Ganji

unread,
Dec 3, 2013, 3:54:00 AM12/3/13
to andro...@googlegroups.com
Hi Richard,

Thank you very much for your answers and information with links. However, I have still do not understand it completely. As I am new to this NDK development, hence, these questions. Sorry, If these are silly. 

> With regard to your questions:
> 1) Yes the method names/signatures are specified by JNI and can be generated from your Java code by call javah on your Java files. http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javah.html
What I understand from your this sentence, write the Java code first and when we compile it we will get the method names. We need to use those method names in the C code which is going to be build as a Shared Library. I do not think, this is the right way of developing the shared library code. Generally, I will develop the library (APIs) first and then I will call them from my application using specified API name in the library. Due to this, I am expecting that there is a way to give the names to the methods which would be developed in the Shared Libraries of NDK framework.

> 2) The Android NDK is not meant to be used to write your whole application but to enhance or enable some functionality of your Android Java program.  
> See the first paragraph http://developer.android.com/tools/sdk/ndk/index.html .  You should therefore start with a Java application and where you require it add native code.  
Yes. I have gone through this link only. I have downloaded and installed by following this link only. This http://developer.android.com/tools/sdk/ndk/index.html#GetStarted says write the library code first, but, not application. That is the reason, I am asking about how to give the names to the methods and how to develop the application for using the developed library APIs.


> 2a) There is an advanced topic regarding native applications which still require Java to invoke them although this can be provided by the platform.  The NDK documentation for native activities can be found 
> in <NDK>/docs/NATIVE-ACTIVITY.html
I have gone through the <NDK>/docs/NATIVE-ACTIVITY.html file contents. What I understand after reading this html file is that this file explains about the developing a native application for using the developed shared library APIs. AM I CORRECT? I am bit confuse here. Please let me know whether my understand about this html file is correct or not.

I want to develop a shared library where I can use some of the OS APIs for example which are related to frame buffer. After developing this shared library, I want to develop a native application, in user space, which will call these shared library APIs using the JNI interface. This is briefly my requirement.

Thank you very much for your information.

Regards,
Srinivas.

RichardC

unread,
Dec 3, 2013, 3:32:09 PM12/3/13
to andro...@googlegroups.com
See inline


On Tuesday, December 3, 2013 8:54:00 AM UTC, Srinivas Ganji wrote:
Hi Richard,

Thank you very much for your answers and information with links. However, I have still do not understand it completely. As I am new to this NDK development, hence, these questions. Sorry, If these are silly. 

> With regard to your questions:
> 1) Yes the method names/signatures are specified by JNI and can be generated from your Java code by call javah on your Java files. http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javah.html
What I understand from your this sentence, write the Java code first and when we compile it we will get the method names. We need to use those method names in the C code which is going to be build as a Shared Library. I do not think, this is the right way of developing the shared library code. Generally, I will develop the library (APIs) first and then I will call them from my application using specified API name in the library. Due to this, I am expecting that there is a way to give the names to the methods which would be developed in the Shared Libraries of NDK framework.

To call native code from Java you need to declare one or more methods of your Java class(es) as native eg:

package xxx.yyy.zzzz

class MyClass
{
public:
  void DoSomthing()
  {
    ...;
    ANativeMethod();
  }

private:
  native void ANativeMethod();
}

The Java JNI calling mechanism expects that the C function name (of ANativeMethod) to be formed from the "Java package + class + method + parameters/return type" and you can either export a C function with this name or in your native dynamic library's on_load() function "fix up" the expected name (still the "manged" Java name)  to the C function name you have used. This is done using using the JNI C function RegisterNatives( ... ). 
 
 

> 2) The Android NDK is not meant to be used to write your whole application but to enhance or enable some functionality of your Android Java program.  
> See the first paragraph http://developer.android.com/tools/sdk/ndk/index.html .  You should therefore start with a Java application and where you require it add native code.  
Yes. I have gone through this link only. I have downloaded and installed by following this link only. This http://developer.android.com/tools/sdk/ndk/index.html#GetStarted says write the library code first, but, not application. That is the reason, I am asking about how to give the names to the methods and how to develop the application for using the developed library APIs.


> 2a) There is an advanced topic regarding native applications which still require Java to invoke them although this can be provided by the platform.  The NDK documentation for native activities can be found 
> in <NDK>/docs/NATIVE-ACTIVITY.html
I have gone through the <NDK>/docs/NATIVE-ACTIVITY.html file contents. What I understand after reading this html file is that this file explains about the developing a native application for using the developed shared library APIs. AM I CORRECT? I am bit confuse here. Please let me know whether my understand about this html file is correct or not.


NativeActivity provides one possible implementation of an Android Java SDK Activity forwarding most of the events (system and UI) you would normally process in your Java code to a native implementation stub. You basically have to implement and extend the C side of the application, see: <NDK>/samples/native-activity for an example implementation.  Even though you do not write any Java code yourself, Java is still the entry point(s) for your application and receives all the events forwarding them to the C layer.

 
I want to develop a shared library where I can use some of the OS APIs for example which are related to frame buffer. After developing this shared library, I want to develop a native application, in user space, which will call these shared library APIs using the JNI interface. This is briefly my requirement.


Just a point of clarification: both Java and native code (developed using the NDK) run in the same process in user space and obey most of the same constraints and security restrictions.

Srinivas Ganji

unread,
Dec 4, 2013, 3:04:32 AM12/4/13
to andro...@googlegroups.com
Hi Richard,

Thanks again, for your information. I understand how to give the proper names to the methods.Thanks.

So, in brief, whatever you are saying - You basically have to implement and extend the C side of the application - is 100% correct. For developing my C side code, shall I need to follow - <NDK>/samples/native-activity for an example implementation sources. AM I CORRECT?

Regarding my developed C code, it will be compiled as a shared library and the user space application access this shared library APIs through JNI interface. 

Please let me know, if you need more information.

Regards,
Srinivas.

Srinivas Ganji

unread,
Dec 5, 2013, 1:15:31 AM12/5/13
to andro...@googlegroups.com
Hi All,

I found a very good tutorial on YouTube for developing a NDK shared library. These tutorials clarified all my doubts about how to develop a shared library in NDK framework, how to give the names to the methods etc. Actually, the library would be developed using SDK tools, but can be compiled using NDK commands. The links for the tutorials are as follows.


Regards,

Srinivas

Reply all
Reply to author
Forward
0 new messages