本文以在Android APP使用speexdsp
中的回音消除为例来说明NDK开发流程;
编译native lib
- download
speexdsp
- 编写cmake
此部分的流程可以参见上一篇blog《CMake交叉编译》
写好Java Class
// EchoCanceller.java
package com.icatchtek.smarthome.echo;
public class EchoCanceller {
public native int echoInit( int frameSize, int filterLength );
public native void echoCancellation( byte[] input, byte[] echo, byte[] output );
public native void echoDestroy();
}
使用javac
和javah
生成JNI header file
- 在以上的java source file目录下使用
javac
来生成class文件
// -d class -> 指定目录,class文件生成在`class`目录下
javac -d class EchoCanceller.java
- 使用
javah
来生成JNI header file
cd class
javah -classpath . com.icatchtek.smarthome.echo.EchoCanceller
执行上述操作后,在class目录下会生成
com_icatchtek_smarthome_echo_EchoCanceller.h
编写对应的JNI实现
有了header file以后,进行对应的功能实现com_icatchtek_smarthome_echo_EchoCanceller.cpp
,对JNI编写不熟悉的可以参见《Pro Android C++ with the NDK》
添加JNI file到native lib编译
有了对应的功能实现以后,需要把com_icatchtek_smarthome_echo_EchoCanceller.cpp
加入native lib再次编译
编写Java测试程序
进行完上述步骤以后,native & JNI的准备就已经好了;我们把编译好的android lib放入对应架构的APP lib目录下;
然后在刚才的EchoCanceller.java
中添加进lib的load,就可以在APP中使用EchoCanceller
测试功能了
package com.icatchtek.smarthome.echo;
public class EchoCanceller {
public native int echoInit( int frameSize, int filterLength );
public native void echoCancellation( byte[] input, byte[] echo, byte[] output );
public native void echoDestroy();
static {
System.loadLibrary( "speexdsp" );
}
}