好的,所以...已经到了白手起家的尽头。
尝试学习使用本机开发工具包......当从 C 源代码返回字符串“Hello World”时,从 Hello World 开始。
开始:这是“主要活动”。
package com.example.hellojni;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView=(TextView) findViewById(R.id.text1);
textView.setText(sayHelloWorld());
}
private native String sayHelloWorld();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
static {
System.loadLibrary("HelloJni");
}
}
为了完整起见,这里是定义显示的 XML。我会向甜甜圈打赌,这不是问题所在,但如果我不提及它,我会在学术上疏忽大意。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
接下来,我们当然需要javah生成的头文件。
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_hellojni_MainActivity */
#ifndef _Included_com_example_hellojni_MainActivity
#define _Included_com_example_hellojni_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#undef com_example_hellojni_MainActivity_MODE_PRIVATE
#define com_example_hellojni_MainActivity_MODE_PRIVATE 0L
#undef com_example_hellojni_MainActivity_MODE_WORLD_READABLE
#define com_example_hellojni_MainActivity_MODE_WORLD_READABLE 1L
#undef com_example_hellojni_MainActivity_MODE_WORLD_WRITEABLE
#define com_example_hellojni_MainActivity_MODE_WORLD_WRITEABLE 2L
#undef com_example_hellojni_MainActivity_MODE_APPEND
#define com_example_hellojni_MainActivity_MODE_APPEND 32768L
#undef com_example_hellojni_MainActivity_MODE_MULTI_PROCESS
#define com_example_hellojni_MainActivity_MODE_MULTI_PROCESS 4L
#undef com_example_hellojni_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING
#define com_example_hellojni_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L
#undef com_example_hellojni_MainActivity_BIND_AUTO_CREATE
#define com_example_hellojni_MainActivity_BIND_AUTO_CREATE 1L
#undef com_example_hellojni_MainActivity_BIND_DEBUG_UNBIND
#define com_example_hellojni_MainActivity_BIND_DEBUG_UNBIND 2L
#undef com_example_hellojni_MainActivity_BIND_NOT_FOREGROUND
#define com_example_hellojni_MainActivity_BIND_NOT_FOREGROUND 4L
#undef com_example_hellojni_MainActivity_BIND_ABOVE_CLIENT
#define com_example_hellojni_MainActivity_BIND_ABOVE_CLIENT 8L
#undef com_example_hellojni_MainActivity_BIND_ALLOW_OOM_MANAGEMENT
#define com_example_hellojni_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
#undef com_example_hellojni_MainActivity_BIND_WAIVE_PRIORITY
#define com_example_hellojni_MainActivity_BIND_WAIVE_PRIORITY 32L
#undef com_example_hellojni_MainActivity_BIND_IMPORTANT
#define com_example_hellojni_MainActivity_BIND_IMPORTANT 64L
#undef com_example_hellojni_MainActivity_BIND_ADJUST_WITH_ACTIVITY
#define com_example_hellojni_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L
#undef com_example_hellojni_MainActivity_CONTEXT_INCLUDE_CODE
#define com_example_hellojni_MainActivity_CONTEXT_INCLUDE_CODE 1L
#undef com_example_hellojni_MainActivity_CONTEXT_IGNORE_SECURITY
#define com_example_hellojni_MainActivity_CONTEXT_IGNORE_SECURITY 2L
#undef com_example_hellojni_MainActivity_CONTEXT_RESTRICTED
#define com_example_hellojni_MainActivity_CONTEXT_RESTRICTED 4L
#undef com_example_hellojni_MainActivity_RESULT_CANCELED
#define com_example_hellojni_MainActivity_RESULT_CANCELED 0L
#undef com_example_hellojni_MainActivity_RESULT_OK
#define com_example_hellojni_MainActivity_RESULT_OK -1L
#undef com_example_hellojni_MainActivity_RESULT_FIRST_USER
#define com_example_hellojni_MainActivity_RESULT_FIRST_USER 1L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_DISABLE
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_DISABLE 0L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_DIALER
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_DIALER 1L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_SHORTCUT
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef com_example_hellojni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define com_example_hellojni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
/*
* Class: com_example_hellojni_MainActivity
* Method: sayHelloWorld
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_hellojni_MainActivity_sayHelloWorld (JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
下一个自然要看的是 C 源代码......
#include "com_example_hellojni_MainActivity.h"
jstring Java_com_example_HelloWorldActivity_sayHelloWorld(JNIEnv* pEnv, jobject thiz) {
return (pEnv->NewStringUTF("Hello World from C!"));
//return (*pEnv)->NewStringUTF(env,"Hello World from C!");
}
评论版本,我不记得我从哪里得到的......可能是从一本书中转录出来的。无论如何,它甚至没有编译。我使用的版本对我来说更有意义。也许这就是问题所在。
接下来,这里是 android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJni
LOCAL_SRC_FILES := HelloJni.cpp
include $(BUILD_SHARED_LIBRARY)
当然......当我把所有这些放在一起时,我得到了一个可以编译并尝试运行的 apk。我这几天一直在拔头发的笑话的妙语如下:
03-22 21:34:38.218: I/ActivityManager(322): Start proc com.example.hellojni for activity com.example.hellojni/.MainActivity: pid=20945 uid=10175 gids={}
03-22 21:34:38.278: D/dalvikvm(20945): Trying to load lib /data/data/com.example.hellojni/lib/libHelloJni.so 0x417260f0
03-22 21:34:38.278: D/dalvikvm(20945): Added shared lib /data/data/com.example.hellojni/lib/libHelloJni.so 0x417260f0
03-22 21:34:38.278: D/dalvikvm(20945): No JNI_OnLoad found in /data/data/com.example.hellojni/lib/libHelloJni.so 0x417260f0, skipping init
03-22 21:34:38.318: W/dalvikvm(20945): No implementation found for native Lcom/example/hellojni/MainActivity;.sayHelloWorld ()Ljava/lang/String;
03-22 21:34:38.328: E/AndroidRuntime(20945): at com.example.hellojni.MainActivity.sayHelloWorld(Native Method)
03-22 21:34:38.328: E/AndroidRuntime(20945): at com.example.hellojni.MainActivity.onCreate(MainActivity.java:17)
03-22 21:34:38.328: W/ActivityManager(322): Force finishing activity com.example.hellojni/.MainActivity
03-22 21:34:38.868: W/ActivityManager(322): Activity pause timeout for ActivityRecord{4235a518 com.example.hellojni/.MainActivity}
03-22 21:34:49.008: W/ActivityManager(322): Activity destroy timeout for ActivityRecord{4235a518 com.example.hellojni/.MainActivity}
03-22 21:34:56.208: I/ActivityManager(322): Process com.example.hellojni (pid 20945) has died.
请帮忙。沮丧是巨大的,理智正在消退。