android studio 生成本地方法实现 JNI 的 C 头文件(*.h)

Generate C header file (*.h) for native method implementation JNI in android studio

如何在Android studio 中自动构建时生成包含函数原型的C 头文件(*.h)?Androidstudio 实现JNI?

谢谢

假设您有一个项目 HelloNative

HelloNative
   |-build/intermediate/classes/debug  (you get this directory upon project build)
   |-app/src/main/jni
        /src/main/java/com/planets/mercury/HelloNativeActivity.java

并且您想为 HelloNativeActivity.java 生成头文件 --> 执行此操作

cd HelloNative

javah -d app/src/main/jni -classpath /Users/john-smith/Android/sdk/platforms/android-19/android.jar:app/build/intermediates/classes/debug com.planets.mercury.HelloNativeActivity

将 john-smith 和 android sdk 路径替换为您自己机器上的路径。 对于 android jar 文件。您可以使用任何您选择的。

例如。 android-19,或android-21

要在 android 工作室中生成头文件,请按照给定的几个步骤进行操作。

1) Open Android Studio Terminal
   (At Left Bottom Corner Given Terminal Tab)

2) type this command first
   (cd app/src/main)
   it is located to the your main source directory.

3) now type this command in the termial for the generation of the header file
   (javah -d cpp -classpath ../../build/intermediates/classes/debug <your_packagename>.<class_name>)

我建议创建一个外部工具。

转到文件->设置->工具->外部工具 并添加一个新工具。填写对话框(使用 "Insert Macro" 按钮)

  • 名称:javah
  • 说明:生成jni头文件
  • 程序:$JDKPath$\bin\javah
  • 参数: -d $SourcepathEntry$..\cpp\ -v $FileClass$
  • 工作目录。 $SourcepathEntry$

保留其他选项不变。

现在您可以在 Project Explorer 中 select 您的 class,右键单击并从上下文菜单中启动外部工具->javah .

头文件将在项目的 cpp 文件夹中创建(如 Android Studio Native 开发所建议),但您需要浏览该文件夹下单看看。

致谢 http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/ (修改为与更新的 SDK 和 Android Studio 更加一致) 和