Android编译WebRTC(M116版本)

1,151 阅读1分钟

Android编译WebRTC(M116版本)

当前教程是编译webrtc m116版本的。网上找了好多教程都是m84m85这类的版本,比较老。

目前看到的声网的版本也是基于m84来做的。

声网地址: webrtc.org.cn/mirror/

有些教程是基础老版本的,所以步骤和流程会有不一样。

官方最新的教程:webrtc官方编译教程

也可以看我这边的步骤

  • 执行命令,拉取 depot_tools 代码:

    git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
    
  • 接着将如下命令加入到 ~/.bashrc 中,并输入 source ~/.bashrc 重载 bash。其中的 /path/to/ 需要你替换为 depot_tools 的所在路径:

    export PATH=/path/to/depot_tools:$PATH
    

    例如:export /root/chrome/depot_tools:$PATH

  • 创建并切换到 ~/webrtc

    mkdir ~/webrtc && cd "$_"
    
  • 进入 webrtc 目录执行下面,拉取并同步 WebRTC 的最新代码:

    fetch —nohooks webrtc_android
    gclient sync
    

    网络不好的时候,可以重复使用 gclient sync 进行同步

  • 进行到下载的代码页面 ~/webrtc/src ,切换到最新的 m116 分支。

    git checkout -b m116 branch-heads/5845
    gclient sync
    

    所有版本号可以在这里查,目前编译时最新的正式版本就是m116

    chromiumdash.appspot.com/branches

  • 确保当前工作目录是你的工作区的 src/ 目录。然后运行以下命令:

    gn gen out/Debug --args='target_os="android" target_cpu="arm"'
    

    你可以指定自己选择的目录,而不是 out/Debug,以便同时管理多个配置。

    要构建ARM64版本:使用 target_cpu="arm64"

    要构建32位x86版本:使用 target_cpu="x86"

    要构建64位x64版本:使用 target_cpu="x64"</br>

  • 使用以下命令进行编译:

    autoninja -C out/Debug
    

    (要列出所有可用的目标,请运行 autoninja -C out/Debug -t targets all

    我是使用 autoninja -C out/Debug -t targets all ,第一种的时候报错了

  • 运行下面命令:

    . build/android/envsetup.sh
    
  • 构建项目:

    autoninja -C out/Debug AppRTCMobile
    
  • 生成项目文件:

    build/android/gradle/generate_gradle.py --output-directory $PWD/out/Debug \
      --target "//examples:AppRTCMobile" --use-gradle-process-resources \
      --split-projects --canary
    

    如果执行的过程中,报下面的报错:

    >   --target "//examples:AppRTCMobile" --use-gradle-process-resources \
    >   --split-projects --canary
    usage: generate_gradle.py [-h] [--output-directory OUTPUT_DIRECTORY] [-v] [--target TARGETS]
                              [--extra-target EXTRA_TARGETS] [--project-dir PROJECT_DIR] [--all]
                              [--use-gradle-process-resources] [--split-projects] [--native-target NATIVE_TARGETS]
                              [--sdk-path SDK_PATH]
    generate_gradle.py: error: unrecognized arguments: --canary
    

    去掉 --canary 执行命令如下:

    build/android/gradle/generate_gradle.py --output-directory $PWD/out/Debug \
      --target "//examples:AppRTCMobile" --use-gradle-process-resources \
      --split-projects
    
  • 生成APK文件:

    autoninja -C out/Debug android_instrumentation_test_apk
    autoninja -C out/Debug rtc_unittests
    

    可以在 out/Debug/bin 目录中找到生成的apk文件

我把 out/Debug/bin 目录编译下的,原生编译出来的东西。可以直接用