AndroidNDK&JNI7---Why didn't FindClass find my class?
问题引入: (Most of this advice applies equally well to failures to find methods with GetMethodID or GetStaticMethodID, or fields with GetFieldID or GetStaticFieldID.)
Make sure that the class name string has the correct format. JNI class names start with the package name and are separated with slashes, such as java/lang/String. If you're looking up an array class, you need to start with the appropriate number of square brackets and must also wrap the class with 'L' and ';', so a one-dimensional array of String would be [Ljava/lang/String;. If you're looking up an inner class, use '$' rather than '.'. In general, using javap on the .class file is a good way to find out the internal name of your class.
If you're using ProGuard, make sure that ProGuard didn't strip out your class. This can happen if your class/method/field is only used from JNI.
If the class name looks right, you could be running into a class loader issue. FindClass wants to start the class search in the class loader associated with your code. It examines the call stack, which will look something like:
Foo.myfunc(Native Method)
Foo.main(Foo.java:10)
The topmost method is Foo.myfunc. FindClass finds the ClassLoader object associated with the Foo class and uses that.
This usually does what you want. You can get into trouble if you create a thread yourself (perhaps by calling pthread_create and then attaching it with AttachCurrentThread). Now there are no stack frames from your application. If you call FindClass from this thread, the JavaVM will start in the "system" class loader instead of the one associated with your application, so attempts to find app-specific classes will fail.
There are a few ways to work around this:
- Do your
FindClasslookups once, inJNI_OnLoad, and cache the class references for later use. AnyFindClasscalls made as part of executingJNI_OnLoadwill use the class loader associated with the function that calledSystem.loadLibrary(this is a special rule, provided to make library initialization more convenient). If your app code is loading the library,FindClasswill use the correct class loader. - Pass an instance of the class into the functions that need it, by declaring your native method to take a Class argument and then passing
Foo.classin. - Cache a reference to the
ClassLoaderobject somewhere handy, and issueloadClasscalls directly. This requires some effort.
解决办法:
因为使用到了ProGuard工具,
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
所以:
需要修改proguard.flags
keep public class com.goldsand.hardware.camera.livecore{ public *; }

浙公网安备 33010602011771号