Eclipse NDK development of basic processes

 The basic process is the development of Eclipse for NDK:

Create an Android project ---> function to complete the preparation of java, jni add interface function (native function) is added ---> Add jni folder, add Android.mk and source files ---> implemented in the source file interface function ---> ndk-build with the source files compiled into lib library ---> start Android emulator, install and run the project.

 

 For example:

 1, open Eclipse, add a android project. Name of the project is IDETTest, and then complete the Activity name (IDETest) and java package (com.test.IDETest) of the prompts. Note java package name is best not underlined, calls may fail in the future jni call, because the format interface functions jni is the name of the package _Activity Java_java name _ function declaration

 2, to complete the preparation of java code introduced ndk cross compiler is called in the class of calls required jni:

    static {
        System.loadLibrary("IDETest");

    } 

 At the same time in the class declaration native function (in the C file is created, with the NDK cross compiler)

 public native String generateID(String label);

 

Complete java file looks like this:

package com.test.IDETest;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class IDETest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        TextView tv=new TextView(this);
        tv.setText(generateID("47766"));
        setContentView(tv);
    }
    
    /*native method implement*/
    public native String generateID(String label);
    /*******************************/
    /*import c lib*/
    static {
        System.loadLibrary("IDETest");
    }

 3, add a folder in IDETest project, named jni. C add modules to achieve in the jni folder files (.c and .h files engineering) and Android.mk file

  C content file module (IDETest.c) of

  #include <string.h>

  #include <jni.h>
  #include <stdio.h>

 

jstring
Java_com_test_IDETest_IDETest_generateID( JNIEnv* env,
jobject thiz,
jstring label)
{
char *str=(char *)malloc(1024*sizeof(char));
sprintf(str,"Hello everybody! I am from XXX, and my ID is %s",(*env)->GetStringUTFChars(env,label,0));
printf("%s",str);
return (*env)->NewStringUTF(env,str);
}

 Android.mk file

 

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := IDETest
LOCAL_SRC_FILES := IDETest.c
include $(BUILD_SHARED_LIBRARY)

 

 

 4, compile the source code for the library file.

 Into the project directory

 root@XXX-laptop:~/IDETest# ls

AndroidManifest.xml  bin                 gen  libs  proguard.cfg  src
assets               default.properties  jni  obj   res
root@XXX-laptop:~/IDETest# ndk-build
Compile thumb  : IDETest <= IDETest.c
SharedLibrary  : libIDETest.so
Install        : libIDETest.so => libs/armeabi/libIDETest.so
Obj is automatically created and compiled after the completion of the libs folder

Compiled library files in there.

 5, start the Android emulator to run the project.

 

 

Reproduced in: https: //www.cnblogs.com/imagelab/archive/2011/08/20/2147023.html

Guess you like

Origin blog.csdn.net/weixin_34217773/article/details/94177832