首先下载NDK 然后在最外层的
local.properties文件夹中 写上 ndk的路径
ndk.dir=/Users/wyj/Desktop/androidStudioSdk/android-ndk-r10d
1.新建工程myapp
2.在MainActivity中写好
1 public static native String stringFromJNI();
2.生成对应的class文件
右键工程 Make Module 'myapp' 会生成 build文件夹 旗下很多 *.class
3.生成native对应的 .h 文件
命令格式
-classpath <路径> 用于装入类的路径
-d <目录> 输出目录
-jni 生成 JNI样式的头文件(默认)
注意MainActivity.class是个二进制文件,当前文件夹目录是myapp
终端下切换到myapp下 执行 要带上android.jar,因为 Activity中一些类需要android.jar中的类
mac下(:)
javah -classpath build/intermediates/classes/debug:/Users/wyj/Desktop/a/android.jar -d jni com.example.wyj.myapplication.MainActivity
windows下(;)
javah -classpath build\intermediates
\classes\debug;d:\Android\SDK\platforms\android-23\android.jar -d jni2 -jni com.
example.wyj.myapplication.MainActivity
其中Android.mk的内容是 其中:
LOCAL_MODULE := hello 生成的库名 生成 libhello.so
LOCAL_SRC_FILES := hello.c 指定哪个文件 都是.c文件
1 # Copyright (C) 2009 The Android Open Source Project 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 LOCAL_PATH := $(call my-dir) 16 17 include $(CLEAR_VARS) 18 19 LOCAL_MODULE := hello 20 LOCAL_SRC_FILES := hello.c 21 22 include $(BUILD_SHARED_LIBRARY)