Steps for calling local methods through JNI in android: first, generate class files in eclipse
package Pag;public class JNI {static{System.loadLibrary("hello");}public native void printHello();public static void main(String[] args) {new JNI().printHello();}}
The above example is a simple call of the native method in java. You can use javac to generate a. class file.
The first method is to enter the generated. class package directory (note that the directory containing. class is not the. class directory ).
For example:
C: \ Users \ Administrator> d:
D: \> cd workspace \ JNIText \ bin \ Pag
D: \ workspace \ JNIText \ bin \ Pag> javah JNI
Error: unable to access JNI
Incorrect class file:. \ JNI. class
Class file contains the wrong class: Pag. JNI
Delete the file or make sure the file is in the correct class path subdirectory.
Com. sun. tools. javac. util. Abort
At com. sun. tools. javac. comp. Check. completionError (Check. java: 164)
At com. sun. tools. javadoc. DocEnv. loadClass (DocEnv. java: 149)
At com. sun. tools. javadoc. RootDocImpl. (RootDocImpl. java: 77)
At com. sun. tools. javadoc. JavadocTool. getRootDocImpl (JavadocTool. java: 159
)
At com. sun. tools. javadoc. Start. parseAndExecute (Start. java: 330)
At com. sun. tools. javadoc. Start. begin (Start. java: 128)
At com.sun.tools.javadoc.Main.exe cute (Main. java: 66)
At com. sun. tools. javah. Main. main (Main. java: 147)
Javadoc: Error-fatal error
2 error
The above prompt is to enter the. class directory, not the package directory. Next, return to the package directory (D: \ workspace \ JNIText \ bin) in the previous directory. Compile the. h header file through javah Pag. JNI and generate the. h header file in the bin.
D: \ workspace \ JNIText \ bin \ Pag>
D: \ workspace \ JNIText \ bin \ Pag> cd ..
D: \ workspace \ JNIText \ bin> javah Pag. JNI
The bin contains the class package Pag, that is, the package path. Implemented using the javah command
The command for the second method is javah-classpath D: \ workspace \ JNIText \ bin-jni Pag. JNI (directly go to cmd) javah-classpath <包路径> -Jni <包含.class的包路径> .
Test C: \ Users \ Administrator> javah-classpath D: \ workspace \ JNIText \ bin-jni Pag. in JNI,-jni can be omitted without writing, but-classpath cannot be omitted; otherwise, an error is returned.
Javadoc: Error-invalid package name: "D: \ workspace \ JNIText \ bin" error.
Then, use the C \ C ++ programming tool to implement the method of its header file and generate the dll.
Reprinted with indicated source http://write.blog.csdn.net/postedit