scrcpy编译过程-手把手教你做车机手机投屏开发

896 阅读4分钟

参考链接:github.com/Genymobile/… meson : mesonbuild.com/ ninja : ninja-build.org/

什么是scrcpy:

pronounced "screen copy" This application mirrors Android devices (video and audio) connected via USB or over TCP/IP, and allows to control the device with the keyboard and the mouse of the computer. It does not require any root access. It works on Linux, Windows and macOS.

scrcpy的优点

It focuses on: lightness: native, displays only the device screen performance: 30120fps, depending on the device quality: 1920×1080 or above low latency: 3570ms low startup time: ~1 second to display the first image non-intrusiveness: nothing is left installed on the Android device user benefits: no account, no ads, no internet required freedom: free and open source software

Ubuntu上编译scrcpy

参考链接: github.com/Genymobile/… github.com/Genymobile/…

环境:Ubuntu20.04 64位 先进行clone代码

git clone https://github.com/Genymobile/scrcpy

这里默认是master分支,大家可以自己根据情况看看是否选着特定的release版本,比如要设置到1.21版本,需要如下操作:

test@test:~/demos/scrcpy-1.21/scrcpy$ git  log --oneline | grep 1.21
334f4699 Update README.zh-Hans.md to v1.21
86158130 Update README.sp.md to v1.21
cb8713eb Update links to v1.21 #选择这个既可以,也可以选其他,但是更新可能微弱差别
003e7381 Bump version to 1.21
1e215199 Remove unused struct port_range
test@test:~/demos/scrcpy-1.21/scrcpy$ git reset --hard cb8713eb #直接reset --hard
HEAD is now at cb8713eb Update links to v1.21

分析了解一下代码结构

在这里插入图片描述

安装好相关依赖库

Install the required packages from your package manager.

Debian/Ubuntu
# runtime dependencies
sudo apt install ffmpeg libsdl2-2.0-0 adb libusb-1.0-0

# client build dependencies
sudo apt install gcc git pkg-config meson ninja-build libsdl2-dev \
                 libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev \
                 libswresample-dev libusb-1.0-0-dev

# server build dependencies
sudo apt install openjdk-17-jdk #目的编译android部分的scrcpy-server

上面安装库已经有详细注释,主要分为运行时候依赖的库,电脑客户端编译依赖库,手机服务端的依赖库 注意:sudo apt install openjdk-17-jdk这里需要考虑 自己的scrcpy版本哈,如果我们编译为scrcpy 1.21版本那么就需要 sudo apt install openjdk-11-jdk即java11

编译所有输出,即包含电脑客户端和手机服务端:

Option 1: Build everything from sources
Install the Android SDK (Android Studio), and set ANDROID_SDK_ROOT to its directory. For example:
# Linux
export ANDROID_SDK_ROOT=~/Android/Sdk

即需要命令行eport一下ANDROID_SDK_ROOT路径,因为需要依赖android的sdk进行编译手机服务jar 开始使用meson配置构建:

meson setup x --buildtype=release --strip -Db_lto=true

在这里插入图片描述 会出现如上结果代表成功,接下来在进行ninja进行构建

ninja -Cx  # DO NOT RUN AS ROOT

构建成功结果如下: 在这里插入图片描述 一般会遇到问题主要还是android手机服务端编译,比如gradle下载,jdk版本等问题,如果不想要进行android手机服务端的scrcpy-server编译可以使用如下方式: 方法1: 使用下载好的scrcpy-server Option 2: Use prebuilt server scrcpy-server-v2.4(github官网下载好包github.com/Genymobile/… SHA-256: 93c272b7438605c055e127f7444064ed78fa9ca49f81156777fd201e79ce7ba3 Download the prebuilt server somewhere, and specify its path during the Meson configuration:



meson setup x --buildtype=release --strip -Db_lto=true \
    -Dprebuilt_server=/自己路径/scrcpy-server
ninja -Cx  # DO NOT RUN AS ROOT

方法2: 如果不想编译安卓服务端只编译电脑客户端scrcpy可以直接用如下方式:

First, you need to install the required packages:

# for Debian/Ubuntu
sudo apt install ffmpeg libsdl2-2.0-0 adb wget \
                 gcc git pkg-config meson ninja-build libsdl2-dev \
                 libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev \
                 libswresample-dev libusb-1.0-0 libusb-1.0-0-dev

如果不自己单独编译android端的scrcpy-server的jar,只编译Ubuntu电脑上scrcpy可执行程序

也可以使用如下方式:

Then clone the repo and execute the installation script (source):

git clone https://github.com/Genymobile/scrcpy
cd scrcpy
./install_release.sh

编译产物: 这里只需要关系客户端的scrcpy文件和手机服务端的可执行文件scrcpy-server 在scrcpy的根目录的x/app/下有对应 scrcpy的可执行文件: 在这里插入图片描述

另一个手机端的scrcpy-server在x/server目录下:

在这里插入图片描述

编译完成后运行: Run without installing:

./run x [options]

核心代码导入android studio

手机server部分的代码是android投屏最核心的,也是后续一直修改的部分,所以这块代码一定需要导入到android studio这个IDE中进行编译开发。 导入方式: File ---->open 打开对应scrcpy的目录既可以 把整个scrcpy项目导入到as,注意服务端代码都在server文件夹下,不是在app文件夹 在这里插入图片描述

导入没有问题后进行相关的编译: 编译方法 Build ----》 Make Module 'server'

编译的产物apk: 在这里插入图片描述

最后把server-debug.apk其实就是前面的scrcpy-server,直接可以对 apk进行重命名既可以

具体详情试看方式: 投屏专题部分: mp.weixin.qq.com/s/IGm6VHMiA…

更多framework详细代码和资料参考如下链接

hal+perfetto+surfaceflinger mp.weixin.qq.com/s/LbVLnu1ud… 在这里插入图片描述

其他课程七件套专题:在这里插入图片描述 点击这里 mp.weixin.qq.com/s/Qv8zjgQ0C…

视频试看: www.bilibili.com/video/BV1wc…