2013年8月14日星期三

Without cygwin, use the NDK to develop

 

NDK compiler comes from seven starts on windows without configuring cygwin environment.

 

configured in eclips NDK path

 

click on the menu bar in eclipse window-Perferences, configure NDK path.

 

 

in the works to add native lib

 Android

create a new blank project, right-click on the root directory Adnroid Tools - Add Native Support ...

 

 

In the dialog box, enter the name you want the new lib, click Finish, the project directory will be more of a jni folder, which has a blank cpp file and an Android.mk file

 

 

Android.mk contents of the file is

 

LOCAL_PATH: = $ (call my-dir)

 

include $ (CLEAR_VARS)

 

LOCAL_MODULE: = TestJni
LOCAL_SRC_FILES: = TestJni.cpp

 

include $ (BUILD_SHARED_LIBRARY)

 

TestJni is to load the lib name, LOCAL_SRC_FILES is the resource file.

 

hello jni

 


MainActivity.java code is as follows:

 
  
package com.example.testjni; 

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TextView textView
= new TextView(this);
textView.setText(helloFromJni());
setContentView(textView);
}

public native String helloFromJni();

static {
System.loadLibrary(
"TestJni");
}
}
 
 

TestJni.cpp code is as follows:

 
  
#include <jni.h> 

extern "C" jstring Java_com_example_testjni_MainActivity_helloFromJni(JNIEnv *env, jobject thiz) {
return env->NewStringUTF("Hello From Jni");
}
 
 

run the program directly, you can see the NDK build Console window output

 

 

One thing to note: Since I am using the x86 Android virtual machine to run, all using the x86 compiler. Compiler developed a method to create a new directory in the jni Application.mk file, enter the following

 

# APP_ABI: = armeabi
APP_ABI: = x86
# APP_ABI: = armeabi armeabi-v7a x86 mips mips-r2 mips-r2-sf
# APP_ABI: = all
APP_STL: = stlport_static

 

if an ordinary arm processor Android phone, use APP_ABI: = armeabi, if it is x86 processor, use APP_ABI: = x86, and so on.

 

If APP_ABI: = all, will compile all commands so.

 

runs as follows:

 

 

As for C / C + + compiler is how to achieve, we can look at the project Properties

 

 

inside the Builders how the two, one is CDT Builder, one Scanner Configuration Builder.

 

existing engineering add Builder

 

If the project which has jni directory, but not configured C / C + + compiler, experts who can add their own Builder, for me this love opportunistic little rookie, engineering context menu, add a new blank so, then delete, CDT Builder and Scanner Configuration Builder came out.

1 条评论:

  1. but i am getting runtime error as


    at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:44)
    at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:580)

    回复删除