Use Eclipse for Android NDK development (Javah for header file generation)

Source: Internet
Author: User

After the Android program is written in Java, it is easy to decompile after the release, so a way to protect the code or sensitive data (such as a communication key) is required, and the NDK is a good solution.

The NDK can generate a binary library (. So) file that is more difficult to decompile.

First we need to build the NDK environment, the eclipse is very simple to build, first add Android plug-ins in eclipse, it needs to download an NDK package, and then in Eclipse's settings, the NDK in the Android item to set the road strength:

The NDK development can then be started, followed by an example to develop the NDK.

First, build a new Android project, then create a new jar package, and write the interface classes that are ready for C + + development. The code for the class is as follows:

Public class Ndkhelloworld {

  Public static native String stringfromndk();

}

Then find the corresponding classes directory, use the Javah command to generate the corresponding header file, the command is:

Javah-jni Com.example.ndk.ndkHelloWorld

You will then be able to see a com_example_ndk_ndkhelloworld.h in the classes directory, which is the generated header file.

Then we are going to add the so library, right-click on the project properties, select Android Tool,add native support, such as:

After clicking, you will be asked to enter the name of so, and after you enter it, you can see that our list of items has a JNI folder, and the relevant data about the NDK is in this folder, and the header file that we previously generated through Javah is placed in the folder.

Android.mk is the JNI compilation file, and the syntax is not expanded.

Implement the methods in Com_example_ndk_ndkhelloworld.h in CPP and introduce the previously generated header files:

#include "Com_example_ndk_ndkhelloworld.h"

Jstring JAVA_COM_EXAMPLE_NDK_NDKHELLOWORLD_STRINGFROMNDK

(jnienv *env, jclass obj) {

  Return env->newstringutf("Hello ndk");

}

Then go back to the ndkhelloworld we had before, plus the link to so.

Static {

System. LoadLibrary("Ndkhelloworld");   

}

Then we can call the Ndk method of this class in other files.

Mtextview. SetText(ndkhelloworld. STRINGFROMNDK());

Code Location:

http://download.csdn.net/download/highkgao1988/8982487

Use Eclipse for Android NDK development (Javah for header file generation)

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.