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

参考链接:https://github.com/Genymobile/scrcpy
meson : https://mesonbuild.com/
ninja : https://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: 30~120fps, depending on the device
quality: 1920×1080 or above
low latency: 35~70ms
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

参考链接:
https://github.com/Genymobile/scrcpy/blob/master/doc/build.md
https://github.com/Genymobile/scrcpy/blob/master/doc/linux.md

环境: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官网下载好包https://github.com/Genymobile/scrcpy/releases/download/v2.4/scrcpy-server-v2.4)
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进行重命名既可以

更多framework干货请关注“千里马学框架”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里马学框架

帮助你了,就请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值