android ndk javah命令找不到Context或者Handler等sdk的class类

android ndk javah命令找不到Context或者Handler等sdk的class类

 

javah在ndk找不到Android相关的类

Android ndk开发的时候遇到了如果引用的class是Android的class,例如Context,Handler, Surface等Android sdk的类,javah就会找不到相关的类。

例如

package com.penny.demo;

import android.view.Surface;

public class MediaEntry {
    static {
        System.loadLibrary("native-lib");
    }

    public void setSurface(Surface surface) {
        native_setSurface(surface);
    }

    private native void native_setSurface(Surface surface);
}

如果我们运行javah生成jni文件时,就会面临找不到Surface的问题

$ cd build/intermediates/javac/debug/compileDebugJavaWithJavac/classes/

$ javah -jni com.penny.demo.MediaEntry
Error: Class android.view.Surface could not be found.

这是因为android.view.Surface是sdk里面的类,并没有在build/intermediates/javac/debug/compileDebugJavaWithJavac/classes/下面。

 

解决

解决这个问题很简单,把Android.jar的路径加入到javah classpath即可

1、 在macos和linux上运行

javah -jni -classpath $ANDROID_HOME/platforms/android-28/android.jar:. com.penny.demo.MediaEntry

于是命令运行成功

com_penny_demo_MediaEntry.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* Header for class com_penny_demo_MediaEntry */

#ifndef _Included_com_penny_demo_MediaEntry
#define _Included_com_penny_demo_MediaEntry
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_penny_demo_MediaEntry
 * Method:    native_setSurface
 * Signature: (Landroid/view/Surface;)V
 */
JNIEXPORT void JNICALL Java_com_penny_demo_MediaEntry_native_1setSurface
  (JNIEnv *, jobject, jobject);

#ifdef __cplusplus
}
#endif
#endif

2、在windows下命令

linux和macos运行命令毕竟和windows有所差别,不过不影响。在windows上需要修改环境变量和分隔符

cmd>  javah -jni -classpath %ANDROID_HOME%\platforms\android-28\android.jar;. com.penny.demo.MediaEntry

 

 

你可能感兴趣的:(javah包含android类,android开发,jni开发)