Android NDK on WindowsXP

Preparing :

jdk-6u32-windows-i586.exe

eclipse-java-indigo-SR2-win32.zip

installer_r18-windows.exe

ADT-18.0.0.zip

 

Before you start install Android NDk, you need make sure Android SDK set up correctly. (I installed cygwin on my computer, but the make process still works even I removed the cygwin. Maybe the current version of Android NDK already include some make tools)

android-ndk-r8b-windows.zip

 

Install NDK

unzip android-ndk-r8b-windows.zip to a directory, and add it’s ndk full path to PATH env variable.

 

Create an Android project in eclipse

Create an Android project in Eclipse, and supply the code as following:

package com.bruce.test;

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

public class TestJniActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String strResult = unimplementStringFromJNI();
        TextView tv = (TextView)this.findViewById(R.id.strView);
        tv.setText(strResult);
    }
    
    // The following are Jni calls
    public native String stringFromJNI();
    
    public native String unimplementStringFromJNI();
    
    static
    {
        System.loadLibrary("testJni");
    }
}

 

Create the Jni c/c++ header file according to the java .class files

Enter the command line, create new folder named “jni”under the android project directory, and input the command line as following:

$ javah –classpath ./bin/classes –d jni com.bruce.test.TestJniActivity

Note, please provide a correct value to option “classpath”, otherwise you will encounter error that could not find the specified .class file. And also make sure that this .class file already generated.

 

Implement the c/c++ function

After the above process, you will find a xxx.h generated located under $PROJECT_DIR\jni. What you need to do now is to write a xxx.c file to give a implementation for all interfaces. Here header file “com_bruce_test_TestJniActivity.h”generated as following:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_bruce_test_TestJniActivity */

#ifndef _Included_com_bruce_test_TestJniActivity
#define _Included_com_bruce_test_TestJniActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_bruce_test_TestJniActivity
 * Method:    stringFromJNI
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_bruce_test_TestJniActivity_stringFromJNI
  (JNIEnv *, jobject);

/*
 * Class:     com_bruce_test_TestJniActivity
 * Method:    unimplementStringFromJNI
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_bruce_test_TestJniActivity_unimplementStringFromJNI
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Here is the testJni.c:

#include "com_bruce_test_TestJniActivity.h"

JNIEXPORT jstring JNICALL Java_com_bruce_test_TestJniActivity_stringFromJNI
  (JNIEnv *env, jobject obj)
{
    return (*env)->NewStringUTF( env, (const char*)"String From JNI" );
}  

JNIEXPORT jstring JNICALL Java_com_bruce_test_TestJniActivity_unimplementStringFromJNI
  (JNIEnv *env, jobject obj)
{
    return (*env)->NewStringUTF( env, (const char*)"unimplemented String From JNI" );   
}

 

Compile them into a shared library

To compile those c/c++ file into a *.so shared library file, we need to provide a “Android.mk”file fist:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := testJni
LOCAL_SRC_FILES := TestJni.c

include $(BUILD_SHARED_LIBRARY)

 

Then compile it under the command line, switch to the project folder first,

$ ndk-build.cmd

compile

 

Now, you could run the Android application now. Right click the project, and select Run –> Run As Android Application:

android_ndk

 

The full source code could be found here.

转载于:https://www.cnblogs.com/open-coder/archive/2012/09/13/2682665.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值