我在网上查了许多关于Android使用JNI的例子,可是都不好用。
希望那位能提供一个,不用太复杂的,简单而能说明原理的最好!我的环境 linux(ubuntu) eclipse(不支持c/c++) Android sdk1.5步骤要详细, 这个问题别死我了刚来论坛 没有什么分 辛苦了!
希望那位能提供一个,不用太复杂的,简单而能说明原理的最好!我的环境 linux(ubuntu) eclipse(不支持c/c++) Android sdk1.5步骤要详细, 这个问题别死我了刚来论坛 没有什么分 辛苦了!
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7. <TextView
8. android:id="@+id/JNITest"
9. android:layout_width="fill_parent"
10. android:layout_height="wrap_content"
11. android:text="@string/JNITest"
12. />
13. </LinearLayout>
14. 复制代码
(3)编辑java文件 1. package com.ura.test;
2. 3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.widget.TextView;
6. public class JNITest extends Activity {
7. /** Called when the activity is first created. */
8. static {
9. System.loadLibrary("JNITest");
10. }
11. public native String GetTest();
12. @Override
13. public void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.main);
16. String str =GetTest();
17. TextView JNITest = (TextView)findViewById(R.id.JNITest);
18. JNITest.setText(str);
19. }
20. } 复制代码 (4)生成head文件 编译上面工程声称class文件,然后用javah工具生成c/c++头文件。 1. javah -classpath bin -d jni com.ura.test.JNItest 复制代码
生成的头文件如下 1. /* DO NOT EDIT THIS FILE - it is machine generated */
2. #include <jni.h>
3. /* Header for class com_ura_test_JNITest */
4. 5. #ifndef _Included_com_ura_test_JNITest
6. #define _Included_com_ura_test_JNITest
7. #ifdef __cplusplus
8. extern "C" {
9. #endif
10. /*
11. * Class: com_ura_test_JNITest
12. * Method: GetTest
13. * Signature: ()Ljava/lang/String;
14. */
15. JNIEXPORT jstring JNICALL Java_com_ura_test_JNITest_GetTest
16. (JNIEnv *, jobject);
17. 18. #ifdef __cplusplus
19. }
20. #endif
21. #endif 复制代码 (5)编写c/c++文件如下 1. #include "com_ura_test_JNITest.h"
2. 3.
4. 5. #define LOG_TAG "JNITest"
6. 7. #undef LOG
8. 9. #include <utils/Log.h>
10. 11.
12. 13.
14. 15. JNIEXPORT jstring JNICALL Java_com_ura_test_JNITest_GetTest
16. (JNIEnv * env, jobject obj)
17. {
18. return (*env)->NewStringUTF(env, (char *)"JNITest Native String");
19. LOGD("Hello LIB!\n");
20. 21. } 复制代码 (6)编写android.mk文件 1. LOCAL_PATH:= $(call my-dir)
2. include $(CLEAR_VARS)
3. LOCAL_SRC_FILES:= \
4. com_ura_test_JNITest.c
5. LOCAL_C_INCLUDES := \
6. $(JNI_H_INCLUDE)
7. LOCAL_SHARED_LIBRARIES := libutils
8. LOCAL_PRELINK_MODULE := false
9. LOCAL_MODULE := libJNITest
10. include $(BUILD_SHARED_LIBRARY) 复制代码 (7)编译生成动态库 新建文件夹
~/mydroid/external/libJNITest
把上面编写好的头文件,c/c++源文件,make文件拷贝进上面目录中 * 需要注意的是把PRELINK_MOUDULE设置成false 否则需要重新做成img文件再烧入。 在ubuntu中执行 1. cd
2. cd mydroid/build/
3. envsetup.sh
4. cd ~/mydroid
5. cd external/libJNITest/
6. mm 复制代码 编译成功的后会在下面目录中生成libJNITest.so文件
~mydroid/out/target/product/generic/system/lib/
(8)在模拟器中执行程序 首先要把动态库拷进/system/lib中。 启动模拟器 1. adb shell
2. adb remount
3. adb push libJNITest.so /system/lib 复制代码 确认拷贝成功。 1. cd /system/lib
2. ls 复制代码
然后不要关闭模拟器(关掉再开动态库就没了,因为模拟器rom是只读) 执行java程序JNITest 会看到屏幕上打印出
貌似这个问题可以使用NDK解决……学习2楼的做法……
练习使用中……