2017-11-25 121 views
0

我在本地方法執行SensorEventListener如下調用實例方法(使計算速度更快):無法從SensorEventListener本地JNI方法

public class GPSLogger extends Service 
    implements SensorEventListener { 

    public native void onSensorChanged(SensorEvent event); 
    public void startLocationUpdates() { // some code } 
} 

相對應的JNI實現被調用。但是我想從調用本地C代碼startLocationUpdates()如下:

jclass objcls = (*env).GetObjectClass(obj); 
jmethodID methid = (*env).GetMethodID(objcls, "startLocationUpdates", "()V"); 
if (methid == NULL) 
    __android_log_write(ANDROID_LOG_ERROR, "[IFL]", "startLocationUpdates not found"); 
else 
    (*env).CallVoidMethod(obj, methid); 

在這種情況下methid爲NULL。並且在亞行日誌中有一個例外說

java.lang.NoSuchMethodError: no non-static method "Lmy/package/GPSLogger;.startLocationUpdates()V" 

什麼可能是錯的?我甚至嘗試過將物品作爲

jclass objcls = (*env).FindClass("my/package/GPSLogger"); 

但同樣的問題。

回答

0

問題加入proguard的規則後解決(防止方法名的模糊處理):

-keep class my.package.GPSLogger { void startLocationUpdates(); }