4

How to generate C header file (*.h) containing the function prototype for the native method implementation JNI in Android studio while build automatically?

Thank You

ant
  • 53
  • 1
  • 6

3 Answers3

5

I suggest creating an External Tool.

Go to File->Settings->Tools->External Tools and add a new tool. Fill the dialog form (use "Insert Macro" button )

  • Name: javah
  • Description: Generates the jni header file
  • Program: $JDKPath$\bin\javah
  • Arguments: -d $SourcepathEntry$..\cpp\ -v $FileClass$
  • Working Directory. $SourcepathEntry$

Leave the other options unmodified.

enter image description here

Now you can select your class in the Project Explorer, right click and, from the context menu, launch the External Tool->javah.

The header file will be created in the cpp folder of your project (as suggested for Android Studio Native development) but you will need to browse the folder in order to see it.

Credits to http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/ (modified to be more consistent with more recent SDK and Android Studio) and to Javah: Error: cannot access android.support.v7.app.ActionBarActivity

Matteo
  • 482
  • 3
  • 11
  • It seems to be missing a backslash on the arguments. This worked for me: -d $SourcepathEntry$\..\cpp\ -v $FileClass$ – Bram Vaessen Dec 01 '20 at 15:30
4

Assume you have a project 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

And you want to generate the header file for HelloNativeActivity.java --> do this

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

Replace john-smith and android sdk path with what it is on your own machine. For the android jar file. You can use whatever is your choice.

Eg. android-19, or android-21

display name
  • 879
  • 11
  • 20
2

For the Generation of the header file in the android studio please follow the given few steps.

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>)

Kishan Donga
  • 2,851
  • 2
  • 23
  • 35
  • And in the meantime some intern broke compatibility and changed the path to the generated classes in Android Studio 4.1 to "build/intermediates/javac/debug/classes" – Lothar Nov 13 '20 at 04:32