How To Compile Native WebRTC Library from Source for Android

 

 

 

source: webrtc.org

In this article I assume you use Linux and understand about webrtc, and android development, please consider reading those before continuing if you feel not comfortable.

abdularis/libwebrtc-android

Contribute to abdularis/libwebrtc-android development by creating an account on GitHub.

github.com

Install Prerequisite (Chromium depot_tools)

First we need to setup all tools we need, which is depot_tools, it includes custom git binary, ninja build tools, gclientgnfetch etc.

It is quite easy, clone the repository from https://chromium.googlesource.com/chromium/tools/depot_tools.git then add the local cloned repository into the PATH environment variables.

git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

add into PATH environment variables, so the binary included in the repository would be available in the terminal (command line).

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

head over to this link for more information.

Getting The Source Code

Use fetch command to get the webrtc source code for android, before done this you may want to go over another or new directory to place the source code in.

fetch --nohooks webrtc_android

Then.

gclient sync

After that go to webrtc_android/src/ directory, which is just created by the command above.

The process may take long because it require the android build chain tools, it is around 16GB checkout.

Then you need to install all the dependency needed to build the source code.

./build/install-build-deps.sh

Also make sure you have OpenJDK installed and configured properly on your machine (I used version OpenJDK 1.8)

Before compiling the code you can checkout to particular git branch to go into different available webrtc version, to list all the branch use this git command.

git branch -r

Output is something like below

 

uploading.4e448015.gif正在上传…重新上传取消

 

 

output: git branch -r

Like the above result you can checkout to the latest version which is m74 by.

git checkout branch-heads/m74

To check what is your current branch type in.

git branch

uploading.4e448015.gif正在上传…重新上传取消

uploading.4e448015.gif正在上传…重新上传取消

output: git branch

Compilation

There are two ways I have tried out to compile webrtc for android, here are those two starting with the most easy way (Thanks to great developers & contributors of webrtc).

Using AAR build tools

tools_webrtc/android/build_aar.py

It would compile the source code for all supported cpu type such as arm64-v8aarmeabi-v7ax86, x86_64 and then package these native libraries and .jar library into .aar file.

uploading.4e448015.gif转存失败重新上传取消

uploading.4e448015.gif转存失败重新上传取消

output: compilation

.AAR file resides in your current working directory or src/ directory with default name libwebrtc.aar

Finally you can use the aar library in your project, or publish it into maven repository.

Manual Compilation

Manually compile the source code for each particular cpu type.

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

for release

gn gen out/Release --args='is_debug=false is_component_build=false rtc_include_tests=false target_os="android" target_cpu="arm"'

the output is in the out/Debug or out/Release (directory name is arbitrary), target_cpu can be arm64x86 or x64.

begin compiling with ninja (output directory out/Debug or out/Release as explain above).

ninja -C out/Debug

compilation may take long depending on how powerful your machine. output of compilation is in the out/Debug or out/Release directory, native .sofile is in the lib.unstripped/, and the java .jar library is lib.java/ directory.

native .so library is unstripped you can strip to minimize file size using strip tools for particular cpu type such tool is located in webrtc_android/src/third_party/android_ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip (for x64 binary .so).

With manual compilation you also need to package all these library into .aar manually.

Manually compiling from the source code is quite challenging and tedious in my opinion and fortunately thanks to webrtc developers/contributors who made the build script for packaging into .aar library much more easier, for more information visit http://webrtc.github.io/webrtc-org/native-code/android/.

 

 

google原文:

Android

Show Contents

Prebuilt libraries

The easiest way to get started is using the official prebuilt libraries available at JCenter. These libraries are compiled from the tip-of-tree and are meant for development purposes only.

On Android Studio 3 add to your dependencies:

implementation 'org.webrtc:google-webrtc:1.0.+'

On Android Studio 2 add to your dependencies:

compile 'org.webrtc:google-webrtc:1.0.+'

The version of the library is 1.0.. The hash of the commit can be found in the .pom-file. The third party licenses can be found in the THIRD_PARTY_LICENSES.md file next to the .aar-file.

Getting the Code

Android development is only supported on Linux.

  1. Install prerequisite software

  2. Create a working directory, enter it, and run:

    fetch --nohooks webrtc_androidgclient sync

This will fetch a regular WebRTC checkout with the Android-specific parts added. Notice that the Android specific parts like the Android SDK and NDK are quite large (~8 GB), so the total checkout size will be about 16 GB. The same checkout can be used for both Linux and Android development since you can generate your Ninja project files in different directories for each build config.

See Development for instructions on how to update the code, building etc.

Compiling

  1. Generate projects using GN.

    Make sure your current working directory is src/ of your workspace. Then run:

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

    You can specify a directory of your own choice instead of out/Debug, to enable managing multiple configurations in parallel.

    • To build for ARM64: use target_cpu="arm64"
    • To build for 32-bit x86: use target_cpu="x86"
    • To build for 64-bit x64: use target_cpu="x64"
  2. Compile using:

    ninja -C out/Debug

Using the Bundled Android SDK/NDK

In order to use the Android SDK and NDK that is bundled in third_party/android_tools, run this to get it included in your PATH (from src/):

. build/android/envsetup.sh

Then you’ll have adb and all the other Android tools in your PATH.

Running the AppRTCMobile App

AppRTCMobile is an Android application using WebRTC Native APIs via JNI (JNI wrapper is documented here).

For instructions on how to build and run, see examples/androidapp/README.

Using Android Studio

Note: This is known to be broken at the moment. See bug: https://bugs.webrtc.org/9282

  1. Build the project normally (out/Debug should be the directory you used when generating the build files using GN):

    ninja -C out/Debug AppRTCMobile
  2. Generate the project files:

    build/android/gradle/generate_gradle.py --output-directory $PWD/out/Debug \--target "//examples:AppRTCMobile" --use-gradle-process-resources \--split-projects --canary
  3. Import the project in Android Studio. (Do not just open it.) The project is located in out/Debug/gradle. If asked which SDK to use, choose to use Android Studio’s SDK. When asked whether to use the Gradle wrapper, press “OK”.

  4. Ensure target webrtc > examples > AppRTCMobile is selected and press Run. AppRTCMobile should now start on the device.

If you do any changes to the C++ code, you have to compile the project using ninja after the changes (see step 1).

Note: Only “arm” is supported as the target_cpu when using Android Studio. This still allows you to run the application on 64-bit ARM devices. x86-based devices are not supported right now.

Running WebRTC Native Tests on an Android Device

To build APKs with the WebRTC native tests, follow these instructions.

  1. Ensure you have an Android device set in Developer mode connected via USB.

  2. Compile as described in the section above.

  3. To see which tests are available: look in out/Debug/bin.

  4. Run a test on your device:

    out/Debug/bin/run_modules_unittests
  5. If you want to limit to a subset of tests, use the --gtest_filter flag, e.g.

    out/Debug/bin/run_modules_unittests \--gtest_filter=RtpRtcpAPITest.SSRC:RtpRtcpRtcpTest.*
  6. NOTICE: The first time you run a test, you must accept a dialog on the device!

If want to run Release builds instead; pass is_debug=false to GN (and preferably generate the projects files into a directory like out/Release). Then use the scripts generated in out/Release/bin instead.

Running WebRTC Instrumentation Tests on an Android Device

The instrumentation tests (like AppRTCMobileTest and libjingle_peerconnection_android_unittest) gets scripts generated in the same location as the native tests described in the previous section.

更多相关文章

  1. Android 使用AsyncHttpClient文件上传与下载
  2. Android使用ksoap2调用C#中的webservice实现图像上传
  3. android之调用webservice 实现图片上传
  4. 如何上传应用软件到 Android Market集市网站
  5. android之调用webservice 实现图片上传下载
  6. Android上传文件,客户端+服务器源码

随机推荐

  1. Android(安卓)5.0 模拟器 emulator无法启
  2. 基于ubuntu16.04多用户编译android N(and
  3. Appium的前世今生、工作原理等
  4. Android优化adapter及SparseArray介绍
  5. android 中getBaseContext getApplicatio
  6. Android(安卓)ListView item 焦点被抢无
  7. Mac OS启动 Android(安卓)Device Monitor
  8. Android中Java服务过程
  9. Android(安卓)4.0以后正确的获取外部sd卡
  10. android传送照片到FTP服务器的实现代码