【NDK】NDK初入門

下載地址:java

http://blog.csdn.net/lanergaming/article/details/39855033android

http://www.androiddevtools.cn/git

配置ndk:github

 

jni頭文件須要用到javah命令:express

開始生成步驟:apache

一、app

能夠發現上面出現找不到類文件的問題less

二、解決方法:找到sdk裏的android.jar目錄ide

三、會發現項目裏多了jni的目錄和.h頭文件ui

四、配置下ndk的環境變量

五、驗證下是否配置成功

六、項目添加.mk文件,能夠參考google的hello-jni的demo,直接copy過來改下
地址:https://github.com/googlesamples/android-ndk/tree/android-mk

文件如圖,直接copy到本身的工做目錄jni文件夾下,修改下便可

修改說明:

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#.so文件的名字
LOCAL_MODULE    := hello
#對哪一個c文件進行編譯
LOCAL_SRC_FILES := hello.c

include $(BUILD_SHARED_LIBRARY)

七、最後到本身的項目目錄裏編譯下

再到項目裏刷新下就能夠看到神奇的.so文件了

如圖會生成obj>local>armeabi>文件目錄,即是編譯成功了。

八、好了,進展到最後了,咱們開始小試牛刀了,開啓「Hello World」裝逼模式

在Activity裏使用:

/**
 * jni頭文件 經過javah的命令
 * @author haoxi
 *
 */
public class MainActivity extends Activity {
	
	static{
		System.loadLibrary("hello");
	}
	
	private TextView tv_test;
	
	public static native String getStringFromC();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tv_test = (TextView)findViewById(R.id.tv_test);
		tv_test.setText(getStringFromC());
	}

	
}

如圖所示:改了下逼格,Hello from JNI!看到沒,就這麼搞定!

相關文章
相關標籤/搜索