1.Android NDK Jni å¼åCåC++çåºå«
2.Android JNI/NDK环境的源码配置与Demo编译
3.å¦ä½å¨Androidä¸ä½¿ç¨JNI
4.å®åjniå¼åï¼
Android NDK Jni å¼åCåC++çåºå«
Android NDK Jni å¼åCåC++çåºå«
ç±äºAndroidå®æ¹NDKä¾å大å¤ä½¿ç¨Cè¯è¨ç¼åï¼æ以å½æ们æ³ç¨C++è¿è¡å¼åæ¶ï¼ä¼åºç°ç§ç§é误ãä¸é¢ç®å说说å ç¹ä¸åä¹å¤ï¼ä»£ç ä¸ç¨çº¢è²æ åºäºä¸åä¹å¤ï¼
1.å æ¥ä¸ä¸ªç¨Cåçä¾åï¼
ï¼1ï¼hello.cæ件ãå¨Cä¸æ²¡æå¼ç¨ï¼ä¼ éçenvæ¯ä¸ªä¸¤çº§æéï¼ç¨ï¼*envï¼->è°ç¨æ¹æ³ä¸æ¹æ³ä¸è¦ä¼ å ¥env.
1 #include <jni.h>
2
3 jstring Java_com_example_Hello_hello(JNIEnv* env, jobject thiz) {
4 return (*env)->NewStringUTF(env,"Hello Jni---->C!");
5 }
ï¼2ï¼Android.mkæ件,æ´æ¹åç¼å为.c
å¤å¶ä»£ç
1 LOCAL_PATH := $(call my-dir)
2
3 include $(CLEAR_VARS)
4
5 LOCAL_MODULE := hello
6 LOCAL_SRC_FILES := hello.c
7
8 include $(BUILD_SHARED_LIBRARY)
å¤å¶ä»£ç
2.åæ¥ä¸ä¸ªç¨C++åçä¾åï¼
ï¼1ï¼hello.cppæ件ãC++ä¸env为ä¸çº§æéï¼ç¨env->è°ç¨æ¹æ³ï¼æ éä¼ å ¥envï¼C++è¯è¨å¨ç¼è¯çæ¶å为äºè§£å³å½æ°çå¤æé®é¢ï¼ä¼å°å½æ°åååæ°èåèµ·æ¥çæä¸ä¸ªä¸é´çå½æ°å称ï¼èCè¯è¨åä¸ä¼ï¼å æ¤ä¼é æé¾æ¥æ¶æ¾ä¸å°å¯¹åºå½æ°çæ åµï¼æ¤æ¶Cå½æ°å°±éè¦ç¨extern "C"è¿è¡é¾æ¥æå®ï¼è¿åè¯ç¼è¯å¨ï¼è¯·ä¿ææçå称ï¼ä¸è¦ç»æçæç¨äºé¾æ¥çä¸é´å½æ°åï¼exter "C"{ jni代ç }ã
å¤å¶ä»£ç
å¤å¶ä»£ç
1 #include <jni.h>
2
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 jstring Java_com_example_Hello_hello(JNIEnv* env, jobject thiz) {
7 return env->NewStringUTF("Hello Jni---->C++!");
8 }
9 #ifdef __cplusplus
}
#endif
å¤å¶ä»£ç
å¤å¶ä»£ç
ï¼2ï¼Android.mkæ件ï¼æ´æ¹åç¼å为.cpp
å¤å¶ä»£ç
1 LOCAL_PATH := $(call my-dir)
2
3 include $(CLEAR_VARS)
4
5 LOCAL_MODULE := hello
6 LOCAL_SRC_FILES := hello.cpp
7
8 include $(BUILD_SHARED_LIBRARY)
Android JNI/NDK环境的配置与Demo编译
在Android开发中,JNI(Java Native Interface)与NDK(Native Development Kit)起着关键作用,源码它们旨在提升性能、源码代码复用和访问底层资源。源码
NDK,源码外卖侠3.6.0源码作为开发工具包,源码高额刷单源码允许开发者用C和C++编写应用组件,源码提供了必要的源码工具和库来编译本地代码并生成.so文件。它适用于优化性能、源码利用现有C/C++库和直接操作系统底层API。源码
JNI则连接Java和本地代码,源码使得Java能调用C/C++实现的源码函数,反之亦然。源码货车抢单源码对于计算密集型任务或对实时性要求高的源码场景,JNI能显著提高性能。源码例如,游戏开发中,ubuntu看源码工具JNI和NDK能提供流畅的游戏体验,通过C/C++直接操作图像和硬件。
要使用这些工具,首先,补码源码是什么你需要配置NDK环境。接着,从Java类生成JNI头文件,编写并实现C/C++代码,用CMake或ndk-build进行编译。在Android项目中,确保build.gradle配置了NDK,通过Gradle编译项目,并在Java代码中加载.so库,调用本地方法。
总的来说,通过正确的配置和实践,JNI和NDK为Android开发者提供了强大的性能优化和扩展能力。要体验它们的效益,开发者需要遵循特定的步骤和配置,以确保代码的顺利编译和运行。
å¦ä½å¨Androidä¸ä½¿ç¨JNI
ãã1.å¼è¨
ããæ们ç¥éï¼Androidç³»ç»çåºå±åºç±c/c++ç¼åï¼ä¸å±Androidåºç¨ç¨åºéè¿Javaèææºè°ç¨åºå±æ¥å£ï¼è¡æ¥åºå±c/c++åºä¸Javaåºç¨ç¨åºé´çæ¥å£æ£æ¯JNIï¼JavaNative Interface)ãæ¬ææè¿°äºå¦ä½å¨ubuntuä¸é ç½®AndroidJNIçå¼åç¯å¢ï¼ä»¥åå¦ä½ç¼åä¸ä¸ªç®åçcå½æ°åºåJNIæ¥å£ï¼å¹¶éè¿ç¼åJavaç¨åºè°ç¨è¿äºæ¥å£ï¼æç»è¿è¡å¨æ¨¡æå¨ä¸çè¿ç¨ã
ãã2.ç¯å¢é ç½®
ãã2.1.å®è£ jdk1.6
ããï¼1ï¼ä»jdkå®æ¹ç½ç«ä¸è½½jdk-6u-linux-i.binæ件ã
ããï¼2ï¼æ§è¡jdkå®è£ æ件
ãã[html] view plaincopyprint?
ãã.$chmod a+x jdk-6u-linux-i.bin
ãã.$jdk-6u-linux-i.bin
ãã$chmod a+x jdk-6u-linux-i.bin
ãã$jdk-6u-linux-i.bin
ãã(3)é ç½®jdkç¯å¢åé
ãã[html] view plaincopyprint?
ãã.$sudo vim /etc/profile
ãã.#JAVAEVIRENMENT
ãã.exportJAVA_HOME=/usr/lib/java/jdk1.6.0_
ãã.exportJRE_HOME=$JAVA_HOME/jre
ãã.exportCLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
ãã.exportPATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
ãã$sudo vim /etc/profile
ãã#JAVAEVIRENMENT
ããexportJAVA_HOME=/usr/lib/java/jdk1.6.0_
ããexportJRE_HOME=$JAVA_HOME/jre
ããexportCLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
ããexportPATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
ããä¿ååéåºç¼è¾ï¼å¹¶éå¯ç³»ç»ã
ããï¼4ï¼éªè¯å®è£
ãã[html] view plaincopyprint?
ãã.$java -version
ãã.javaversion "1.6.0_"
ãã.Java(TM)SE Runtime Environment (build 1.6.0_-b)
ãã.JavaHotSpot(TM) Server VM (build .4-b, mixed mode)
ãã.$javah
ãã.ç¨æ³ï¼javah[é项]<ç±»>
ãã.å ¶ä¸[é项]å æ¬ï¼
ãã.-helpè¾åºæ¤å¸®å©æ¶æ¯å¹¶éåº
ãã.-classpath<è·¯å¾>ç¨äºè£ å ¥ç±»çè·¯å¾
ãã.-bootclasspath<è·¯å¾>ç¨äºè£ å ¥å¼å¯¼ç±»çè·¯å¾
ãã.-d<ç®å½>è¾åºç®å½
ãã.-o<æ件>è¾åºæ件ï¼åªè½ä½¿ç¨-dæ-oä¸çä¸ä¸ªï¼
ãã.-jniçæJNIæ ·å¼ç头æ件ï¼é»è®¤ï¼
ãã.-versionè¾åºçæ¬ä¿¡æ¯
ãã.-verboseå¯ç¨è¯¦ç»è¾åº
ãã.-forceå§ç»åå ¥è¾åºæ件
ãã.使ç¨å ¨éå®å称æå®<ç±»>ï¼ä¾
ãã.å¦ï¼java.lang.Objectï¼ã
ãã$java -version
ããjavaversion "1.6.0_"
ããJava(TM)SE Runtime Environment (build 1.6.0_-b)
ããJavaHotSpot(TM) Server VM (build .4-b, mixed mode)
ãã$javah
ããç¨æ³ï¼javah[é项]<ç±»>
ããå ¶ä¸[é项]å æ¬ï¼
ãã-helpè¾åºæ¤å¸®å©æ¶æ¯å¹¶éåº
ãã-classpath<è·¯å¾>ç¨äºè£ å ¥ç±»çè·¯å¾
ãã-bootclasspath<è·¯å¾>ç¨äºè£ å ¥å¼å¯¼ç±»çè·¯å¾
ãã-d<ç®å½>è¾åºç®å½
ãã-o<æ件>è¾åºæ件ï¼åªè½ä½¿ç¨-dæ-oä¸çä¸ä¸ªï¼
ãã-jniçæJNIæ ·å¼ç头æ件ï¼é»è®¤ï¼
ãã-versionè¾åºçæ¬ä¿¡æ¯
ãã-verboseå¯ç¨è¯¦ç»è¾åº
ãã-forceå§ç»åå ¥è¾åºæ件
ãã使ç¨å ¨éå®å称æå®<ç±»>ï¼ä¾
ããå¦ï¼java.lang.Objectï¼ã2.2.å®è£ androidåºç¨ç¨åºå¼åç¯å¢
ããubuntuä¸å®è£ androidåºç¨ç¨åºå¼åç¯å¢ä¸windows类似ï¼ä¾æ¬¡å®è£ 好以ä¸è½¯ä»¶å³å¯ï¼
ããï¼1ï¼Eclipse
ããï¼2ï¼ADT
ããï¼3ï¼AndroidSDK
ããä¸windowsä¸å®è£ å¯ä¸ä¸åçä¸ç¹æ¯ï¼ä¸è½½è¿äºè½¯ä»¶çæ¶åè¦ä¸è½½Linuxçæ¬çå®è£ å ã
ããå®è£ 好以ä¸androidåºç¨ç¨åºçå¼åç¯å¢åï¼è¿å¯ä»¥éæ©æ¯å¦éè¦é ç½®emulatoråadbå·¥å ·çç¯å¢åéï¼ä»¥æ¹ä¾¿å¨è¿è¡JNIå¼åçæ¶å使ç¨ãé ç½®æ¥éª¤å¦ä¸ï¼
ããæemulatoræå¨ç®å½android-sdk-linux/tools以åadbæå¨ç®å½android-sdk-linux/platform-toolsæ·»å å°ç¯å¢åéä¸ï¼android-sdk-linuxæandroidsdkå®è£ å android-sdk_rxx-linuxç解åç®å½ã
ãã[plain] view plaincopyprint?
ãã.$sudo vim /etc/profile
ãã.exportPATH=~/software/android/android-sdk-linux/tools:$PATH
ãã. exportPATH=~/software/android/android-sdk-linux/platform-tools:$PATH
ãã$sudo vim /etc/profile
ããexportPATH=~/software/android/android-sdk-linux/tools:$PATH
ããexportPATH=~/software/android/android-sdk-linux/platform-tools:$PATH
ããç¼è¾å®æ¯åéåºï¼å¹¶éå¯çæã
ãã2.3.å®è£ NDK
ããNDKæ¯ç±androidæä¾çç¼è¯androidæ¬å°ä»£ç çä¸ä¸ªå·¥å ·ã
ããï¼1ï¼ä»androidndkå®ç½/sdk/ndk/index.htmlä¸è½½ndkï¼ç®åææ°çæ¬ä¸ºandroid-ndk-r6b-linux-x.tar.bz2.
ããï¼2ï¼è§£åndkå°å·¥ä½ç®å½ï¼
ãã[plain] view plaincopyprint?
ãã.$tar -xvf android-ndk-r6b-linux-x.tar.bz2
ãã.$sudo mv android-ndk-r6b /usr/local/ndk
ãã$tar -xvf android-ndk-r6b-linux-x.tar.bz2
ãã$sudo mv android-ndk-r6b /usr/local/ndk
ããï¼3ï¼è®¾ç½®ndkç¯å¢åé
ãã[plain] view plaincopyprint?
ãã.$sudo vim /etc/profile
ãã.exportPATH=/usr/local/ndk:$PATH
ãã$sudo vim /etc/profile
ããexportPATH=/usr/local/ndk:$PATH
ããç¼è¾å®æ¯åä¿åéåºï¼å¹¶éå¯çæ
ããï¼4ï¼éªè¯å®è£
ãã[plain] view plaincopyprint?
ãã.$ cd/usr/local/ndk/samples/hello-jni/
ãã.$ ndk-build
ãã.Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
ãã.Gdbsetup : libs/armeabi/gdb.setup
ãã.Install : libhello-jni.so => libs/armeabi/libhello-jni.so
ãã$ cd/usr/local/ndk/samples/hello-jni/
ãã$ ndk-build
ããGdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
ããGdbsetup : libs/armeabi/gdb.setup
ããInstall : libhello-jni.so => libs/armeabi/libhello-jni.so
ãã3.JNIå®ç°
ããæ们éè¦å®ä¹ä¸ä¸ªç¬¦åJNIæ¥å£è§èçc/c++æ¥å£ï¼è¿ä¸ªæ¥å£ä¸ç¨å¤ªå¤æï¼ä¾å¦è¾åºä¸ä¸ªå符串ãæ¥ä¸æ¥ï¼åéè¦æc/c++æ¥å£ç代ç æ件ç¼è¯æå ±äº«åºï¼å¨æåºï¼.soæ件ï¼å¹¶æ¾å°æ¨¡æå¨çç¸å ³ç®å½ä¸ãæåï¼å¯å¨Javaåºç¨ç¨åºï¼å°±å¯ä»¥çå°æç»ææäºã
ãã3.1.ç¼åJavaåºç¨ç¨åºä»£ç
ããï¼1ï¼å¯å¨Eclipseï¼æ°å»ºandroidå·¥ç¨
ããProjectï¼JNITest
ããPackageï¼org.tonny.jni
ããActivityï¼JNITest
ããï¼2ï¼ç¼è¾èµæºæ件
ããç¼è¾res/values/strings.xmlæ件å¦ä¸ï¼
ãã
ããç¼è¾res/layout/main.xmlæ件
ããæ们å¨ä¸»çé¢ä¸æ·»å äºä¸ä¸ªEditTextæ§ä»¶åä¸ä¸ªButtonæ§ä»¶ã
ããï¼3ï¼ç¼è¾JNITest.javaæ件
ã
ããstatic表示å¨ç³»ç»ç¬¬ä¸æ¬¡å 载类çæ¶åï¼å æ§è¡è¿ä¸æ®µä»£ç ï¼å¨è¿é表示å è½½å¨æåºlibJNITest.soæ件ã
ããåçè¿ä¸æ®µï¼
ãã[java] view plaincopyprint?
ãã.privatenativeString GetReply();
ããprivatenativeString GetReply();
ããnative表示è¿ä¸ªæ¹æ³ç±æ¬å°ä»£ç å®ä¹ï¼éè¦éè¿jniæ¥å£è°ç¨æ¬å°c/c++代ç ã
ãã[java] view plaincopyprint?
ãã.publicvoidonClick(View arg0) {
ãã.edtName.setText(reply);
ãã.}
ããpublicvoidonClick(View arg0) {
ããedtName.setText(reply);
ãã}
ããè¿æ®µä»£ç 表示ç¹å»æé®åï¼ænativeæ¹æ³çè¿åçå符串æ¾ç¤ºå°EditTextæ§ä»¶ã
ããï¼4ï¼ç¼è¯å·¥ç¨ï¼çæ.classæ件ã
ãã3.2.ç¨javahå·¥å ·çæ符åJNIè§èçcè¯è¨å¤´æ件
ããå¨ç»ç«¯ä¸ï¼è¿å ¥androidå·¥ç¨æå¨çbinç®å½
ãã[plain] view plaincopyprint?
ãã.$cd ~/project/Android/JNITest/bin
ãã$cd ~/project/Android/JNITest/bin
ããæ们ç¨lså½ä»¤æ¥çï¼å¯ä»¥çå°binç®å½ä¸æ个classesç®å½ï¼å ¶ç®å½ç»æ为classes/org/tonny/jniï¼å³classesçåç®å½ç»ææ¯androidå·¥ç¨çå åorg.tonny.jniã请注æï¼ä¸é¢æ们åå¤æ§è¡javahå½ä»¤çæ¶åï¼å¿ é¡»è¿å ¥å°org/tonny/jniçä¸çº§ç®å½ï¼å³classesç®å½ï¼å¦åjavahä¼æ示æ¾ä¸å°ç¸å ³çjavaç±»ã
ããä¸é¢ç»§ç»ï¼
ãã[plain] view plaincopyprint?
ãã.$cd classes
ãã.$javah org.tonny.jni.JNITest
ãã.$ls
ãã.org org_tonny_jni_JNITest.h
ãã$cd classes
ãã$javah org.tonny.jni.JNITest
ãã$ls
ããorg org_tonny_jni_JNITest.h
ããæ§è¡javahorg.tonny.jni.JNITestå½ä»¤ï¼å¨classesç®å½ä¸ä¼çæorg_tonny_jni_JNITest.h头æ件ãå¦æä¸è¿å ¥å°classesç®å½ä¸çè¯ï¼ä¹å¯ä»¥è¿æ ·ï¼
ãã[plain] view plaincopyprint?
ãã.$javah -classpath ~/project/Android/JNITest/bin/classesorg.tonny.jni.JNITest
ãã$javah -classpath ~/project/Android/JNITest/bin/classesorg.tonny.jni.JNITest
ãã-classpath åæ°è¡¨ç¤ºè£ 载类çç®å½ã
ãã3.3.ç¼åc/c++代ç
ããçæorg_tonny_jni_JNITest.h头æ件åï¼æ们就å¯ä»¥ç¼åç¸åºçå½æ°ä»£ç äºãä¸é¢å¨androidå·¥ç¨ç®å½ä¸æ°å»ºjniç®å½ï¼å³~/project/Android/JNITest/jniï¼æorg_tonny_jni_JNITest.h头æ件æ·è´å°jniç®å½ä¸ï¼å¹¶å¨jniç®å½ä¸æ°å»ºorg_tonny_jni_JNITest.cæ件ï¼ç¼è¾ä»£ç å¦ä¸ï¼
ãã[cpp] view plaincopyprint?
ãã.#include<jni.h>
ãã.#include<string.h>
ãã.#include"org_tonny_jni_JNITest.h"
ãã.
ãã.
ãã.JNIEXPORTjstring JNICALLJava_org_tonny_jni_JNITest_GetReply
ãã.(JNIEnv *env, jobject obj){
ãã.return(*env)->NewStringUTF(env,(char*)"Hello,JNITest");
ãã.}
ãã#include<jni.h>
ãã#include<string.h>
ãã#include"org_tonny_jni_JNITest.h"
ããJNIEXPORTjstring JNICALLJava_org_tonny_jni_JNITest_GetReply
ãã(JNIEnv *env, jobject obj){
ããreturn(*env)->NewStringUTF(env,(char*)"Hello,JNITest");
ãã}
ããæ们å¯ä»¥çå°ï¼è¯¥å½æ°çå®ç°ç¸å½ç®åï¼è¿åä¸ä¸ªå符串为ï¼"Hello,JNITest"
ãã3.4.ç¼åAndroid.mkæ件
ããå¨~/project/Android/JNITest/jniç®å½ä¸æ°å»ºAndroid.mkæ件ï¼androidå¯ä»¥æ ¹æ®è¿ä¸ªæ件çç¼è¯åæ°ç¼è¯æ¨¡åãç¼è¾Android.mkæ件å¦ä¸ï¼
ãã[plain] view plaincopyprint?
ãã.LOCAL_PATH:= $(call my-dir)
ãã.include$(CLEAR_VARS)
ãã.LOCAL_MODULE := libJNITest
ãã.LOCAL_SRC_FILES:= org_tonny_jni_JNITest.c
ãã.include$(BUILD_SHARED_LIBRARY)
ããLOCAL_PATH:= $(call my-dir)
ããinclude$(CLEAR_VARS)
ããLOCAL_MODULE := libJNITest
ããLOCAL_SRC_FILES:= org_tonny_jni_JNITest.c
ããinclude$(BUILD_SHARED_LIBRARY)
ããLOCAL_MODULE表示ç¼è¯çå¨æåºå称
ããLOCAL_SRC_FILES 表示æºä»£ç æ件
ãã3.5.ç¨ndkå·¥å ·ç¼è¯å¹¶çæ.soæ件
ããè¿å ¥å°JNITestçå·¥ç¨ç®å½ï¼æ§è¡ndk-buildå½ä»¤å³å¯çælibJNITest.soæ件ã
ãã[plain] view plaincopyprint?
ãã.$cd ~/project/Android/JNITest/
ãã.$ndk-build
ãã.Invalidattribute name:
ãã.package
ãã.Install : libJNITest.so => libs/armeabi/libJNITest.so
ãã$cd ~/project/Android/JNITest/
ãã$ndk-build
ããInvalidattribute name:
ããpackage
ããInstall : libJNITest.so => libs/armeabi/libJNITest.so
ããå¯ä»¥çå°ï¼å¨å·¥ç¨ç®å½çlibs/armeabiç®å½ä¸çæäºlibJNITest.soæ件ã
ãã3.6.å¨æ¨¡æå¨ä¸è¿è¡
ããï¼1ï¼é¦å ï¼æ们æandroid模æå¨å¯å¨èµ·æ¥ãè¿å ¥å°emulatoræå¨ç®å½ï¼æ§è¡emulatorå½ä»¤ï¼
ãã[plain] view plaincopyprint?
ãã.$cd ~/software/android/android-sdk-linux/tools
ãã.$./emulator @AVD-2.3.3-V -partition-size
ãã$cd ~/software/android/android-sdk-linux/tools
ãã$./emulator @AVD-2.3.3-V -partition-size
ããAVD-2.3.3-Vè¡¨ç¤ºä½ ç模æå¨å称ï¼ä¸å¨Eclipse->AVDManagerä¸çAVDName对åºï¼-partition-size表示模æå¨çåå¨è®¾å¤å®¹éã
ããï¼2ï¼æ¥ä¸æ¥ï¼æ们éè¦ælibJNITest.soæ件æ·è´å°æ¨¡æå¨ç/system/libç®å½ä¸ï¼æ§è¡ä»¥ä¸å½ä»¤ï¼
ãã[plain] view plaincopyprint?
ãã.$cd ~/project/Android/JNITest/libs/armeabi/
ãã.$adb remount
ãã.$adb push libJNITest.so /system/lib
ãã. KB/s ( bytes in 0.s)
ãã$cd ~/project/Android/JNITest/libs/armeabi/
ãã$adb remount
ãã$adb push libJNITest.so /system/lib
ãã KB/s ( bytes in 0.s)
ããå½å¨ç»ç«¯ä¸çå°æ KB/s ( bytes in 0.s)ä¼ è¾é度çä¿¡æ¯çæ¶åï¼è¯´ææ·è´æåã
ããï¼3ï¼å¨ç»ç«¯ä¸æ§è¡JNITestç¨åºï¼è¿ä¸ªæ们å¯ä»¥å¨Eclipseä¸ï¼å³é®ç¹å»JNITestå·¥ç¨ï¼RunAs->Android Applicationï¼å³å¯å¨æ¨¡æå¨ä¸å¯å¨ç¨åº
å®åjniå¼åï¼
å¦ä½ç¼åå®å软件
1ãè§ææ2é1å®åçæ¬ï¼åå¼å§ä¹ä¸ç¥ééé¢æäºä»ä¹ææ¯é¾åº¦ï¼ä½æ¯è¦åçç®æ å·²ç»æç¡®äºï¼èä¸ä¹æ²¡æç°æçï¼ç¢°å°é®é¢å°±æ¥èµæï¼æ ¢æ ¢å°è§£å³ï¼è¿æ ·æçæ¾ç¢ï¼å¦ä¹ çææé常好ã
2ãä¸é¢æå°±å¼å§ä»ç»æ们å¼åéè¦ç¨ç软件ï¼Xcode(软件ä¸è½½å°åï¼developer.apple/xcode/)ï¼IOS模æå¨ã
3ãéæ±åæãç¡®å®è¦å¼åæä¸æ¬¾è½¯ä»¶çåæ¶è¦å¯¹éæ±è¿è¡åæï¼å¼åçappæå®é å®ç¨æä¹æå¯ä»¥ãå¼åè éè¦å¯¹è½¯ä»¶ç³»ç»è¿è¡æ¦è¦è®¾è®¡ï¼å³ç³»ç»è®¾è®¡ã
4ãè°·ææ¨åºçAppInventorAndroidAppå¼åå·¥å ·å¯ä»¥è®©ä½ ä» éè¿ææå¼çç®åæä½å°±å¯ä»¥å建èªå·±çAndroidAppã对äºé£äºä¸ºäºç¹å®ç®çæ³è¦å¨æå°è¯å¼åä¸ä¸ªç®ååºç¨çç¨æ·ã
5ãEclipseADTEclipseADTæ¯Eclipseå¹³å°ä¸ç¨æ¥å¼åAndroidåºç¨ç¨åºçæ件TheSDKandAVDManagerè¯¥å·¥å ·å å«å¾å¤éè¦çåè½ï¼å æ¬ç®¡çä¸åçAndroidSDKçæ¬ï¼æ建ç®æ ï¼ï¼Androidççæ¬ä¼å¤ï¼APIä¸æäºå ¼å®¹æ§é®é¢ã
6ãä¸é¢ï¼ä¸ç§è±æ就为大家æ®åä¸ä¸å®å软件å¼åå ¥é¨ç¥è¯ãå级é¶æ®µè¦å¦ä¹ çå 容é ç½®ç¯å¢ï¼å ¨é¨éç¨åæ°çæ¬SDKãADTãECLIPSEãJDKãç¼åæè è¿è¡æå¦ç¤ºä¾è¿æ¶é´ä¸»è¦è¿è¡ç示ä¾å¦HELLOWORLDï¼è¿æSDK带çä¾åã
Androidä¸æä¹éè¿JNIç¼ç¨å»åéadbæ令æ§å¶ææºç¼åä¸ä¸ªCç¨åºï¼ä½¿ç¨system(âcmdline)ï¼ç´æ¥è°ç¨å½ä»¤è¡ç¨åºå³å¯ã
(constchar*)(*env)-GetStringUTFChars(envï¼inputStrï¼JNI_FALSE)ï¼LOGI(dufresne---%sï¼(constchar*)str)ï¼//éç¥èææºæ¬å°ä»£ç ä¸åéè¦éè¿str访é®Javaå符串ã
$cd~/project/Android/JNITest/bin$cd~/project/Android/JNITest/binæ们ç¨lså½ä»¤æ¥çï¼å¯ä»¥çå°binç®å½ä¸æ个classesç®å½ï¼å ¶ç®å½ç»æ为classes/org/tonny/jniï¼å³classesçåç®å½ç»ææ¯androidå·¥ç¨çå åorg.tonny.jniã
å¦ä½ç¨javaè¿è¡å®ååºç¨çå¼åç´æ¥ç¾åº¦æç´¢å®åå¼åæç¨ï¼èµæºé常å¤ãå å»æJavaåCå¦å¥½å说ã
æ¯çï¼Javaå¯ä»¥ç¨äºå¼åå®ååºç¨ãå®ååºç¨çå¼åå¯ä»¥ä½¿ç¨Javaç¼ç¨è¯è¨åAndroidSDKï¼è½¯ä»¶å¼åå·¥å ·å ï¼ãè³äºâå®åæ¯å¦ä¼ä¸è½½å± æ°âï¼æä¸å¤ªæç½æ¨çææã
第ä¸æ¥ï¼å®è£ jdkï¼é ç½®jdkç¯å¢ãç¾åº¦æjdké 置第äºæ¥ï¼å®è£ AndroidStudio+SDKãæè Eclipse+ADT+AndroidSDKã第ä¸æ¥ï¼éè¦Androidæºåæè å®è£ èææºãå¦æçé®ï¼è¯·æåºã
ä¸ï¼JavaSEç¼ç¨Javaæ¯ä¸ç§é¢å对象çå¼åè¯è¨ï¼Androidæä½ç³»ç»çåºç¨å±ä½¿ç¨Javaè¯è¨æ¥å¼åï¼æ以è¦æ³è¿è¡Androidå¼åå¿ é¡»æè¯å¥½çJavaåºç¡ã
Androidåºç¨ç¨åºå¼åæ¯ä»¥Javaè¯è¨ä¸ºåºç¡çï¼æ以éè¦ææå®çJavaåºç¡ç¥è¯ãé¦å çæjavaåºæ¬è¯æ³ï¼ç¶åçæ设计模å¼çã
ç¨jniåçå®ååºç¨è½è¢«åç¼è¯å
å°ç¨åºè½¬æ¢ææ¬å°ä»£ç ä¹æ¯ä¸ç§é²æ¢åç¼è¯çæææ¹æ³ãå 为æ¬å°ä»£ç å¾å¾é¾ä»¥è¢«åç¼è¯ãå¼å人åå¯ä»¥éæ©å°æ´ä¸ªåºç¨ç¨åºè½¬æ¢ææ¬å°ä»£ç ï¼ä¹å¯ä»¥éæ©å ³é®æ¨¡å转æ¢ã
æå ç§æ¹å¼æ¥æé«è¢«åç¼è¯å代ç çé¾åº¦ï¼1å ³é®ä»£ç 使ç¨jniè°ç¨æ¬å°ä»£ç ï¼ç¨cæè c++ç¼åï¼å æ¤ç¸å¯¹æ¯è¾é¾äºåç¼è¯2æ··æ·java代ç ãæ··æ·æ¯ä¸æ¹å代ç é»è¾çæ åµä¸ï¼å¢å æ ç¨ä»£ç ï¼æè éå½åï¼ä½¿åç¼è¯åçæºä»£ç é¾äºçæã
ç±äºapkæ¯Androidèææºå è½½çï¼å®æä¸å®çè§èï¼å å¯apkåDalvikæ æ³è¯å«apkäºãå®å ¨é¿å æ¯ä¸å¯è½çï¼æ»æ人è½å¤ç ´è§£åç代ç ãä½æ¯æå ç§æ¹å¼æ¥æé«è¢«åç¼è¯å代ç çé¾åº¦ã
å¦ä½JAVA代ç è°ç¨ï¼jniå§ï¼å®åçSOæ件æ¯linuxä¸çæ件ï¼ç¨cæè c++åçã
ä¸é¢è¯´äºï¼è¿ç§æ¹å¼å ¶å®å¹¶ä¸æ¯çæ£å å¯ä»£ç ï¼å ¶å®ä»£ç è¿æ¯è½å¤è¢«äººåç¼è¯(æ人å¯è½è¯´ï¼ä½¿ç¨proguardä¸çoptimizeé项ï¼å¯ä»¥ä»åèæµå±é¢æ´æ¹ä»£ç ï¼çè³å¯ä»¥è®©JDè¿äºåç¼è¯è½¯ä»¶å¯ä»¥æ æ³å¾å°å 容ã
Androidä¸JNIæ¯ç¼è¯soåºçæºä»£ç ï¼ç¼è¯æååä¼çæSOåºï¼androidä¸æç»æ¯ä½¿ç¨SOåºçã
å®åå¼åè°ç¨åºå±ç¡¬ä»¶æä½æ¹æ³å¦ä¸ï¼ç¼è¯åå®è£ 该çæçapkå³å¯ä½¿ç¨è¯¥ç¨åºè°ç¨åºå±ç¡¬ä»¶é©±å¨ã大æ¦æ´ä¸ªè¿ç¨å°±è¿æ ·ï¼ä¸å±appè°ç¨æ¡æ¶å±çjavaæ¥å£ï¼javaæ¥å£éè¿jniè°ç¨ç¡¬ä»¶æ½è±¡å±å³å¯ã
ï¼Androidåºç¨ç¨åºå±ï¼2ï¼åºç¨ç¨åºæ¡æ¶å±ï¼3ï¼ç³»ç»è¿è¡åºå±ï¼4ï¼Linuxæ ¸å¿å±ã
å¯ä»¥åèä¸ä¸/luoshengyang/article/details/ãéé¢çåæä»ä¸å°å ï¼å®æ´çæè¿°çåºå±å°åºç¨å±çè°ç¨ï¼å¯¹äºç解HALæå¾å¤§ç帮å©ã
Androidåºå±é©±å¨ï¼ä»¥åæä½ç³»ç»å ¨é¨å æ ¸é½æ¯ä½¿ç¨Cè¯è¨åæ±ç¼è¯è¨ç¼ç¨å¼åãä½æ¯androidæä½ç³»ç»æ¬èº«æ¯ä½¿ç¨Javaå¼åï¼ä½¿ç¨JNIè°ç¨æ¬å°çCè¯è¨åºï¼ç±æä½ç³»ç»å®ç°å¯¹åºå±è°ç¨ãiOSç³»ç»æ¯è¹æèªå·±å¼åçï¼æ ¸å¿åéCè¯è¨ã
第åå±ï¼åºäºCçNDK(NativeDevelopmentKit)ï¼å¾å¤å¤§å游ææ¯ç¨è¿ç§æ¹æ³å¼åçï¼ç¸å¯¹äºJDKï¼è¿ç§æ¹æ³çé¨æ§å°±æ´é«äºï¼ç®å使ç¨çæ¯è¾å°ï¼æ¯ç«ç°å¨æ¯æºè½ææºçæ¶ä»£ï¼ç¡¬ä»¶åç½éé½å¤§å¹ æåï¼æ²¡å¿ è¦å¨ä¸å¨å°±Touchåºå±ã
AndroidNDKå¼åç®ä»NDKåSDK以åJNIæä»ä¹å ³ç³»_å®åndkæ¯å¹²åç_ç¾åº¦...对äºandroidç³»ç»çä¸äºå¼åçæ¬ç管ç以å模æå¨ç®¡çãèndkï¼NativeDevelopmentKitï¼è·sdkå·®ä¸å¤çæ¯ä»ä¹æ¯ä¸ä¸ªå¼åå·¥å ·å ãç¨ä»å¼åc/c++æ¯å¾æ¹ä¾¿çãä»æä¸ä¸ªå¼ºå¤§çç¼è¯éåãå ¶å®åè¯ç解éæä¹ä¸æ³å¤è¯´ã
NDKæ¯ç¨æ¥ç»å®åææºå¼å软件ç¨çï¼ä½æ¯åSDKä¸åçæ¯å®ç¨çæ¯Cè¯è¨ï¼èSDKç¨çæ¯Javaè¯è¨ãNDKæ¯ä¸ç³»åå·¥å ·çéåï¼NDKæä¾äºä¸ç³»åçå·¥å ·ï¼å¸®å©å¼åè å¿«éå¼åCï¼æC++ï¼çå¨æåºï¼å¹¶è½èªå¨å°soåjavaåºç¨ä¸èµ·æå æapkã
NDKæ¯ç¨æ¥ç»å®åææºå¼å软件ç¨çï¼ä½æ¯åSDKä¸åçæ¯å®ç¨çæ¯Cè¯è¨ï¼èSDKç¨çæ¯Javaè¯è¨ã
JNI表示JavaNativeIntefaceï¼æ¯Javaä¸å®ä¹çä¸ç§ç¨äºè¿æ¥JavaåC/C++æ¥å£çä¸ç§å®ç°æ¹å¼ãNDK表示NativeDevelopmentKitï¼æ¯Googleå¨Androidå¼åä¸æä¾çä¸å¥ç¨äºå¿«éå建nativeå·¥ç¨çä¸ä¸ªå·¥å ·ã