![]() Every piece is separated by an underscore instead of a dot. It follows the specific pattern of “Java,” followed by the package name, followed by the class name, followed by the method name, as defined from Java. The name of the function, though, is important. It takes in a Java object String parameter, converts it in to a C-string, and then writes it out to LogCat. This function is actually fairly straightforward. (*env)->ReleaseStringUTFChars(env, logThis, szLogThis) _android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: ", szLogThis) Void Java_com_mamlambo_sample_ndk1_AndroidNDK1SampleActivity_helloLog(JNIEnv * env, jobject this, jstring logThis)Ĭonst char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy) #define DEBUG_TAG "NDK_AndroidNDK1SampleActivity" Place the following C code in this file to start we’ll add another function later: Now, within the jni folder, create a file called native.c. If you’re familiar with JNI, the Android NDK is heavily based on JNI concepts – it is, essentially, JNI with a limited set of headers for C compilation. Our project has a package name of “1” with a default Activity name of “AndroidNDK1SampleActivity” – you’ll see these appear again soon.Īt the top level of this project, create a directory called “jni” – this is where you’ll put your native code. To avoid problems later, your project must reside in a path that contains no spaces. Step 2: Creating the ProjectĬreate a regular Android project. If you’re on Windows using Cygwin, you need to configure the Cygwin path setting. If you’re on Mac or Linux, you can do this with your native path setting. Remember where you put it.Īt this point, you may want to add the NDK tools to your path. We put ours in the same directory that we put the Android SDK. Do so and place it in an appropriate directory. Now that the NDK is downloaded (it is, right?), you need to unzip it. If any of these versions are too old, please update them before continuing. GNU Make 3.81 or later (We're using 3.81).Update awk to the most recent version (We're using 20070501).Now, check the versions of your tools against these: We’ll do this first, as while it’s downloading you can check to make sure you have the right versions of the rest of the tools you need.ĭownload the NDK for your operating system from the Android site. Step 0: Downloading the ToolsĪlright, let’s get started. The JavaNative C exchanges add some overhead, so it’s only really worthwhile if you’ve got some intensive processing to do. ![]() This can be particularly true of OpenGL ES applications written for other platforms.ĭon’t assume you’ll increase your application’s performance just because you’re using native code. If you’ve got loads of C code for your existing application, using the NDK could speed up your project’s development process as well as help keep changes synchronized between your Android and non-Android projects. ![]() Also, don’t forget that in Android 2.2, a JIT compiler will improve the performance of such code as well.Īnother reason to use the NDK is for ease of porting. That is, you have algorithms that are using all of the processor within the DalvikVM and would benefit from running natively. Generally speaking, you only need to use the NDK if your application is truly processor bound. However, we’d like to take a moment to talk about why the NDK is important, when it should be used, and-just as importantly, when it should not be used. So, if you’re reading this tutorial, you may already be considering the NDK for your Android projects. The complete sample project for this tutorial can be downloaded open source code. Be aware that you may have to do some troubleshooting of your own before you get everything working smoothly on your development system. That said, using the NDK is still a process that is prone to problems and issues even if you consider yourself a mobile development veteran. If you aren’t comfortable with these, you’re welcome to read this tutorial, of course, but you may have difficulties at certain steps that would be resolved by being comfortable with the above.
0 Comments
Leave a Reply. |