13

I'm trying to create a .h header file for a simple NDK project. Using cygwin I browse to myprojectDIR\jni directory, then execute this command:

javah -o com_myproject_MyActivity.h -classpath  myprojectDIR\bin\classes com.myproject.MyActivity

then this error message appears:

Error: cannot access android.app.Activity
class file for android.app.Activity not found

I have a native method inside MyActivity class, so I tried to create a new class called NativeAccess (does not extend any class from the android SDK) and it worked fine, the .h file was created, ndk-build and test on device where successful!

So my problem is that I need my native methods inside android activities and services that I create, but I cant do that because the Javah command cannot access classes from inside the android-sdk itself. Please notice that I'm using (Windows-7 x64) and I have these environment variables:

ANDROID_NDK : C:\Android\android-ndk-r7b
ANDROID_SDK : C:\Android\android-sdk
ANT_HOME    : C:\ANT\apache-ant-1.8.3
JAVA_HOME   : C:\Program Files\Java\jdk1.7.0_02
PATH        : %JAVA_HOME%\bin;%ANDROID_SDK%\tools;%ANDROID_SDK%\platform-tools;%ANDROID_NDK%;%ANT_HOME%\bin; (other unrelated stuff)

Thanks in advance

WhiteKnight
  • 4,938
  • 5
  • 37
  • 41
Ayesh Qumhieh
  • 1,117
  • 2
  • 11
  • 26
  • You'll need to include the android platform jar(s) in the classpath you use with this tool. If your task is particularly simple you might just create the headers by hand, using the ndk samples as a guide. – Chris Stratton May 07 '12 at 18:38
  • Thanks Chris, I did thought about adding the classpth of the android jar(s) with the javah tool, but I couldn't find how to do that.. – Ayesh Qumhieh May 09 '12 at 22:26

5 Answers5

21

Try adding to your classpath:

-classpath <android-sdk-location>/platforms/android-8.jar
JonnyBoy
  • 1,555
  • 14
  • 24
14

I found it, this is how it goes:

javah -o com_myproject_MyActivity.h -classpath <android-sdk-location>/platforms/android-8.jar;myprojectDIR\bin\classes com.myproject.MyActivity
Ayesh Qumhieh
  • 1,117
  • 2
  • 11
  • 26
4

Try the following in Eclipse,

Go to > Run | External Tools| External Tool Configurations Under Program create new configuration by clicking small icon.

Name it.

Location would be : C:\Program Files\Java\jdk1.7.0_04\bin\javah.exe

Working Directory would be: ${workspace_loc:/My_First_NDK/bin/classes} and

Arguments would be: -classpath ${workspace_loc:/My_First_NDK/bin/classes} -bootclasspath "C:\adt-bundle-windows-x86-20140624\sdk\platforms\android-19\android.jar" -v -d ${workspace_loc:/My_First_NDK/jni} com.mypackage.ndk.HelloNDK

enter image description here

Punith K
  • 656
  • 1
  • 9
  • 26
0

In order to generate native header files javah needs:

  • your classes (usually under build/intermediates/classes)
  • Android classes (usually under $ANDROID_HOME/platforms/android-%ver%/android.jar)

So you simply pass them in -classpath argument

The main challenge for me was basically passing both of them - you have to use : as a separator and paths must be absolute.
See Oracle docs

Thus you may end up with something like this:
javah -classpath <full path to app>build/intermediates/classes/debug:<full path to sdk>/platforms/android-26/android.jar com.myproject.MyActivity

mister_potato
  • 120
  • 3
  • 6
-1

I found it, this is how it goes:

   E:\workspeaceResearch\DAMMADARJNI\src>javah -classpath "E:\Software\sdk\platform
   s\android-19/platforms/android-8.jar"; -jni com.dammadar.jni.Home_Screen
Emran Hamza
  • 3,829
  • 1
  • 24
  • 20