2019独角兽企业重金招聘Python工程师标准>>>
1.安装配置NDK
1). 解压NDK的zip包到非中文目录(最好英文目录不要带空格)
2). 配置path : 解压后NDK的根目录----->ndk-build
2.给AS关联NDK
1). local.properties中添加配置
ndk.dir=E\:\\Android\\sdk\\android-ndk
或者去项目工程中配置ndk的路径
2). gradle.properties中添加配置
android.useDeprecatedNdk=true(作用:兼容老版本的NDK)
3.编写native方法
public class JniTest {static {System.loadLibrary("Hello");}/*** 定义native方法* 调用C中对应的方法* @return*/public native String sayHello();
}
4.编写Android.mk文件
# 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) LOCAL_MODULE := Hello
LOCAL_SRC_FILES := Hello.c include $(BUILD_SHARED_LIBRARY)
1. LOCAL_PATH:设置工作目录,而 my-dir 则会返回 Android.mk 文件所在的目录。
2. CLEAR——VARS:清除几乎所有以 LOCAL——PATH 开头的变量(不包括 LOCAL_PATH)。
3. LOCAL_MODULE:用来设置模块的名称。
4. LOCAL_SRC_FILES:用来指定参与模块编译的 C/C++ 源文件名。
5. BUILD_SHARED_LIBRARY:作用是指定生成的静态库或者共享库在运行时依赖的共享库模块列表。
5.编写C文件
6.在你的Build.gradle中
buildTypes下
添加如下代码:
sourceSets {main {jni.srcDirs = ['src/main/jni', 'src/main/jni/']} }externalNativeBuild {ndkBuild {path file("src\\main\\jni\\Android.mk")} }
然后重新reBuilde工程
会在你的app\build\intermediates\ndkbuild下面会生成so文件
使用: