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
Now, you could run the Android application now. Right click the project, and select Run –> Run As Android Application:
The full source code could be found here.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了