参考链接: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干货请关注“千里马学框架”