libusb: android上集成libusb库

时间:2021-07-26 06:54:13

1. 下载libusb库。

可以到libusb库的官网(https://libusb.info/)或者是其官方的github仓库(https://github.com/libusb/libusb/releases)上下载最新的libusb库源码。

2. 添加libusb库到android studio项目中。

这里以源码编译的方式添加,使用的仍然是ndk-build的方式,而非cmake,使用源码编译的好处在于,可以随时调试libusb库的源码,方便加log。

2.1 在app/src/main目录下新建一个jni目录,用于存放libusb库的源代码。
libusb: android上集成libusb库

2.2  打开app目录下的build.gradle文件,指定JNI目录:

libusb: android上集成libusb库

2.3  在jni目录下新建libusb-support目录和makefile文件,libusb-support目录用于存放libusb库的源代码。

这里单独新建一个libusb-support目录的目的在于,将来可能会有很多个第三方模块,比如libusb,libyuv, sdl,分目录存放可以划分得更清楚,可以直接把libusb的代码放到jni目录下,只要makefile能找到就行。
Android.mk

#
# Copyright (c) Realsil.Inc. All rights reserved.
#
include $(call all-subdir-makefiles)

Application.mk

#
# Copyright (c) SEP.Inc. All rights reserved.
# APP_ABI := all APP_LDFLAGS := -llog

最终目录结构如下:
libusb: android上集成libusb库

2.3 拷贝libusb库的源代码到libusb-support目录

打开下载的libusb库源代码,找到libusb文件夹,将整个文件拷贝到libusb-support目录下,我下载的是1.0.23版本:
libusb: android上集成libusb库

打开android文件夹,拷贝config.h文件到libusb-support目录下
libusb: android上集成libusb库

打开jni文件夹,拷贝Android.mk、libusb.mk文件到libusb-support目录下:
libusb: android上集成libusb库

最终目录结构如下:

libusb: android上集成libusb库

接下来修改Android.mk和libusb.mk文件,默认编译出来的库会包含一些测试用例和示例代码,我们不需要它。
修改Android.mk, 注释掉example.mk和tests.mk

# Android build config for libusb, examples and tests
# Copyright © - RealVNC Ltd. <toby.gray@realvnc.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., Franklin Street, Fifth Floor, Boston, MA - USA
# LOCAL_PATH:= $(call my-dir) include $(LOCAL_PATH)/libusb.mk # include $(LOCAL_PATH)/examples.mk
# include $(LOCAL_PATH)/tests.mk

修改libusb.mk, 更新当前源代码的位置:

# Android build config for libusb
# Copyright © - RealVNC Ltd. <toby.gray@realvnc.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., Franklin Street, Fifth Floor, Boston, MA - USA
# LOCAL_PATH:= $(call my-dir)
LIBUSB_ROOT_REL:= .
LIBUSB_ROOT_ABS:= $(LOCAL_PATH) # libusb include $(CLEAR_VARS) LIBUSB_ROOT_REL:= .
LIBUSB_ROOT_ABS:= $(LOCAL_PATH) LOCAL_SRC_FILES := \
$(LIBUSB_ROOT_REL)/libusb/core.c \
$(LIBUSB_ROOT_REL)/libusb/descriptor.c \
$(LIBUSB_ROOT_REL)/libusb/hotplug.c \
$(LIBUSB_ROOT_REL)/libusb/io.c \
$(LIBUSB_ROOT_REL)/libusb/sync.c \
$(LIBUSB_ROOT_REL)/libusb/strerror.c \
$(LIBUSB_ROOT_REL)/libusb/os/linux_usbfs.c \
$(LIBUSB_ROOT_REL)/libusb/os/poll_posix.c \
$(LIBUSB_ROOT_REL)/libusb/os/threads_posix.c \
$(LIBUSB_ROOT_REL)/libusb/os/linux_netlink.c \
$(LIBUSB_ROOT_REL)/libusb/realsil_usb_audio.c \
$(LIBUSB_ROOT_REL)/libusb/jnihelp.cpp LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/ \
$(LIBUSB_ROOT_ABS)/libusb \
$(LIBUSB_ROOT_ABS)/libusb/os LOCAL_EXPORT_C_INCLUDES := \
$(LIBUSB_ROOT_ABS)/libusb LOCAL_LDLIBS := -llog LOCAL_MODULE := usb include $(BUILD_SHARED_LIBRARY)

其中的 realsil_usb_audio.c 和 jnihelp.cpp是我自己添加的调用libusb库的code, 可不用在意。
3.  编译链接
在任意文件夹下右击,选择“Link C++ Project with Gradle”
libusb: android上集成libusb库

在Build System的下拉列表中选择ndk-build的编译方式,并指定所有模块的makefile文件位置:
libusb: android上集成libusb库

libusb: android上集成libusb库

点OK, 此时编译系统就会自动开始构建,注意到此时文件夹的目录颜色已修改,且app目录下的build.gradle文件也进行了更新。

libusb: android上集成libusb库

接下来就可以在相应的C代码里编写调用libusb库的相应逻辑了。

参考链接:

1. Android从USB声卡录制高质量音频-----USB API测试

2. Android从USB声卡录制高质量音频-----使用libusb读取USB声卡数据

3. Android如何对libusb进行编译和使用

4. Libusb在Android平台上的环境以及原理

5. Android无驱usb音频实现