Search code examples
javaandroidbazel

How to import external module/dependencies like Flic in my Android project which is using Bazel as build system?


For importing module dependency in my Android project, for ex. For making my app compatible with Flic I have to import the whole Flic project as module dependency in my existing Android project. It works fine when built with gradle but with Bazel it shows error while importing any class of Flic.


Solution

  • I'm not familiar with Flic, but looking at the instructions here, it looks like flic can be depended upon using http_archive and writing a small android_library rule for it:

    WORKSPACE:

    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
    
    http_archive(
      name = "flic-android",
      # latest commit as of writing this answer
      url = "https://github.com/50ButtonsEach/fliclib-android/archive/ddfbfebfd5090bb2cc80a7e66c613134ffc4071a.zip",
      build_file = "@//:BUILD.flic-android",
      strip_prefix = "fliclib-android-ddfbfebfd5090bb2cc80a7e66c613134ffc4071a"
    )
    

    BUILD.flic-android (put this next to the WORKSPACE file):

    package(default_visibility = ["//visibility:public"])
    
    android_library(
      name = "flic",
      manifest = "fliclib/src/main/AndroidManifest.xml",
      srcs = glob(["fliclib/src/main/java/io/flic/lib/*.java"]),
    
      idl_srcs =        glob(["fliclib/src/main/aidl/io/flic/lib/*.aidl"]),
      # "external/flic-android" must be included in the import root because this
      # rule is being evaluated in an external repository
      idl_import_root = "external/flic-android/fliclib/src/main/aidl",
    
      custom_package = "io.flic.lib",
    )
    

    Finally, add "@flic-android//:flic" to the deps of any android_binary or android_library rules that use Flic.

    (Side note: flic's build.gradle file listed com.android.support:appcompat-v7:22.1.1 as a compile-time dependency, but it doesn't appear to be needed to compile the library, so I didn't include that here)