【NDK】NDK初入门

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 13:15:43

下载地址:

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

http://www.androiddevtools.cn/

配置ndk:

 

jni头文件需要用到javah命令:

开始生成步骤:

1、

可以发现上面出现找不到类文件的问题

2、解决方法:找到sdk里的android.jar目录

3、会发现项目里多了jni的目录和.h头文件

4、配置下ndk的环境变量

5、验证下是否配置成功

6、项目添加.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)

7、最后到自己的项目目录里编译下

再到项目里刷新下就可以看到神奇的.so文件了

如图会生成obj>local>armeabi>文件目录,便是编译成功了。

8、好了,进展到最后了,我们开始小试牛刀了,开启“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!看到没,就这么搞定!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!