Search code examples
androidchromiumgn

How to use android library with native library as dependency for Chromium?


I'm working on integrating some library (aar that contains native library (.so)) into Chromium (Snapdragon-optimized fork to be more detailed).

In order to use it in Android code i had to modify chrome/android/BUILD.gn and add 2 dependencies:

deps = [
  ...
  "//third_party/libxyz_android:libxyz_android",
  "//third_party/libxyz_android:libxyz_android_settings",
  ...
]

So i had to create third_party/libxyz directory, put aar files there and create BUILD.gn:

import("//build/config/android/rules.gni")

android_aar_prebuilt("libxyz_android") {
  aar_path = "libxyz-android-release.aar"
#  proguard_configs = [ "src/proguard-gvr.txt" ]
  ignore_manifest = true  # Ignored because manifest merging is not supported (http://crbug.com/643967)
  ignore_native_libraries = false
}

android_aar_prebuilt("libxyz_android_settings") {
  aar_path = "libxyz-android-settings-release.aar"
  ignore_manifest = true  # Ignored because manifest merging is not supported (http://crbug.com/643967)
}

Note ignore_native_libraries = false as libxyz has native libaries.

While building of Chromium i got error:

android_aar_prebuilt() with .so files is not supported. Use ignore_native_libraries = true to silence this error.

It comes from here: https://chromium.googlesource.com/chromium/src/build/config/+/master/android/rules.gni#2817

assert(_ignore_native_libraries || !_scanned_files.has_native_libraries, ...

Is there any possibility to use aar with native libraries, some workaround or modified rules.gni? If i have to add the support myself - should i edit rules.gni and add native libraries copying from unzipped aar to target directory (it seems to be pretty simple)?

Update 1: i had to change target names and replace "-" to "_" because of target name error ("-" is not supported).


Solution

  • You need to modify android_aar_prebuilt in //build/config/android/rules.gni to copy the appropriate .so files into the build directory (root_build_dir).

    Then you need to add a dependency on the AAR target to your android_apk target directly. Finally, also add the .so files in your build directory to the loadable_modules directive of android_apk (this is critical).

    The GN Reference is your friend.