從零開始-邊學邊做-塔防遊戲-七彩三國(三)--JNI集成

第三章:JNI集成


上一章,我們完成了,從無到有的開發環境搭建,並且成功在模擬器上運行了,helloword的測試程序。爲追求更高的性能,個人覺得還是使用c++語言開發遊戲更合適。

android程序調用c++編寫的.so,需要藉助JNI。


1.先將項目文本編碼改成UTF-8


2.打開

...\workspace\HelloWorld\src\com\example\helloworld\MainActivity.java

將原來的代碼修改爲:

package com.example.helloworld;

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  tv = new TextView(this);
		 tv.setText( stringFromJNI() );
		 setContentView(tv);
	}
	public native String  stringFromJNI();
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	static {
        System.loadLibrary("helloword-jni");
    }
}


3.工程目錄下新建一個目錄 jni

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := helloword-jni
LOCAL_SRC_FILES := helloword-jni.c

include $(BUILD_SHARED_LIBRARY)


Application.mk:

APP_ABI := all


helloword-jni.c:

#include <string.h>
#include <jni.h>

jstring Java_com_example_helloworld_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz) {
	return (*env)->NewStringUTF(env, "Hello World, JNI 我來了。");
}




4.添加builders


5.選擇需要編譯的目錄


6.測試運行




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章