Android NDK environment construction and simple instances

Source: Internet
Author: User

Android NDK environment construction and simple instances
1. Introduction to NDK and JNI

 

NDK is called the native development kit local language (C & C ++) development kit. The Android SDK (software development kit) is a commonly used software development kit (only supports java development ).

Simply put, NDK can be used to develop pure C & C ++ code, and then compiled into a library for Java program calls developed using Android-SDK. NDK can be called underlying development or java native interface (jni) layer development. SDK development can be called Upper-layer development.

Why NDK is used in Android development:

1. As we all know, the APK generated using the code written in the SDK can be decompiled easily, and the security is extremely low. The library developed using the NDK is not easily decompiled and confidential, security is improved.

2. Many open source projects and large projects are C & C ++ Code. It is obviously impossible to convert them into pure java languages.

3. The code running speed and efficiency of C & C ++ are much faster than that of java.

 

JNI is called Java Native Interface (called locally by JAVA ). Since java, JNI has become a part of the Java platform. It allows java code to interact with code written in other languages (such as C & C ++. JNI is not introduced from Android release.

To put it simply, Android NDK provides some cross-compilation tool chains and libraries that come with Android. These Android libraries can be called by developers when programming programs in local languages. The cross-compilation tool chain provided by NDK compiles the compiled C & C ++ code to generate a library. Of course, you can also build your own cross-compilation environment without using NDK tools and libraries. Then generate the library. You can generate the library file that can be successfully called by the JAVA layer as long as the standard operation is performed.
2. Establish an NDK Environment

When using the latest NDK, cygwin is directly discarded. To use NDK for Android projects, you must download NDK and install Cygwin (used to simulate Linux environments ), download the CDT (Eclipse C/C ++ development plug-in) and configure the compiler and environment variables. This is especially troublesome and is not required for the new version.

Download Android SDK http://developer.android.com/sdk/index.html

Download Android NDK http://developer.android.com/tools/sdk/ndk/index.html (Note: NDK version after r7 and above integrated Cygwin, and still very Lite version .)

 

My SDK and NDK are

 

Eclipse Configuration

Open Eclipse, click Window-> Preferences-> Android-> NDK, and set the NDK path, for example, F: android-ndk-r10e. Create an Android project NDKTest, right-click the project and choose Android Tools> Add Native Support ..., and then give it to us. so file name (the package name NDKTest is started by default ). At this time, the project will have another jni folder, which contains the Android. mk and NDKTest. cpp files. Android. mk is the Makefile of the ndk project, and NDKTest. cpp is the source file of the NDK. Next, compile the NDKTest. cpp file.
#include 
 
  #include 
  
   extern Cjstring Java_com_example_ndktest_MainActivity_NDKTestFromJNI(JNIEnv* env,jobject thiz) {return env->NewStringUTF(Hello from JNI !);}
  
 
Continue to compile the MainActivity. java File
Package com. example. ndktest; import android. app. activity; import android. OS. bundle; import android. widget. textView; public class MainActivity extends Activity {protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); TextView TV = new TextView (this); TV. setText (NDKTestFromJNI (); setContentView (TV);} public native String NDKTestFromJNI (); // native declaration, indicating that this method comes from the Native layer. The implementation process has implemented static {System. loadLibrary (NDKTest); // load the library at the native layer. The previous lib and suffix names do not need to be written }}
Compile and run. Compile the cpp file first, right-click the Project NDKTest and Build Project, and generate the. so file in the libs directory.
Compile the Android Java project, right-click the project run application, and deploy it on the real machine. Result 3: Related Questions

Some problems encountered during the NDK construction and instance process are as follows:

 

Eclipse reports "Unresolved injection jni. h"

In the process of NDK development, sometimes in eclipse, it will encounter errors caused by the inability to handle the transition sion. There are many methods on the Internet to solve the problem similar to "Unresolved transition sion jni. h "indicates the incorrect method, including includepath and other methods, but it does not work for me.

The final solution is to initialize nativesupport for the project by eclipse:

1. Disable the specified Project in eclipse

2. Use other editing tools to open the. project file of the project and delete the following content:

......

Org. eclipse. cdt. managedbuilder. core. genmakebuilder
Clean, full, incremental,



......

Org. eclipse. cdt. managedbuilder. core. ScannerConfigBuilder
Full, incremental,

 

......

Org. eclipse. cdt. core. cnature
Org. eclipse. cdt. core. ccnature
Org. eclipse. cdt. managedbuilder. core. managedBuildNature
Org. eclipse. cdt. managedbuilder. core. ScannerConfigNature

3. Delete the. cproject File

4. Open the original project and refresh in eclipse, right-click and choose properties> Android Tools> Add Native Support.

5. Get it done


Method 'newstringutf' cocould not be resolved in Eclipse Ndk development on the Windows platform

 

Right-click a project and choose Properties> c/c ++ General> Code Analysis. If you select "Use project settings", the Method cannot be resolved cannot be parsed, application-> OK, and then refresh, clear, refresh, and build the project.

 

Jni/hellocpp/main. cpp: 16: 18: error: base operand of '-> 'has non-pointer type' JNIEnv {aka _ JNIEnv }'

 

 

The error is:

(* Env)-> NewStringUTF (env, Hello from JNI !);

This line is written in c, while my cpp program needs to be rewritten:

Env-> NewStringUTF (Hello from JNI !);

 

Java. lang. UnsatisfiedLinkError: Native method not found

 

 

1. In c ++, the Java initials in Java_xxx_xxx must be capitalized.

2. If it is A. cpp file, use extern C {Your method here}. The braces are your local method.

 

 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.