Profile cover photo
Profile photo
Christopher Blay

Christopher's interests
Christopher's posts

Android + Bazel + OpenCV

I've been tinkering with the Guillemot Sensor app lately, trying to get OpenCV for Android SDK [0] integrated, after having switched to Bazel [1] because Gradle is annoying.

Overall I'm liking Bazel a lot! I can reference my other repos directly which means no more Maven packaging nightmares. It's fast. It makes sense. The only thing I'm still working out is how to make Android Studio just be an editor and stop bothering me about Gradle all the time.

I used the OpenCV for Android SDK with Eclipse way back while working on my masters thesis and I remember it took awhile to get all the settings just right, but then it was pretty nice. The nice thing about Bazel is once you figure out how to get OpenCV pulled in then it's one of those things you can just commit and never worry about ever again. I thought I'd share my approach and BUILD files so that everyone else can hopefully never worry about it ever again.


Basic SDK Setup

I searched for "android opencv bazel" and found a thing on Stack Overflow [2]. It's not specific to Android but kind of gives a lead for compiling from source which sounds hard. I tinkered with it for a bit and decided I'd rather try something more like the old Eclipse setup using the provided SDK download.

The SDK approach worked great! I made an android_library target called "sdk" depending on a cc_library target called "jni". It's easy enough to point those to their respective source files. The only tricky part was configuring the .aidl stuff.

I could `bazel build @org_opencv//:sdk and everything Just Worked. Yay!


Sample Projects

The SDK also comes with a handful of example Android apps, some of which use NDK. All the pure-Java ones were really easy to setup; I even made a "sample" macro for them. But the apps using NDK were a bit more tricky.

For some reason, the .apk artifacts didn't include any .so files? Maybe I was doing something wrong? After searching a bit more I found out there's a secret argument for android_binary called "legacy_native_support" which, if set to 0, does cause a .so file to get packaged.

Next hurdle was the .so file was named wrong. Apparently it's impossible to control its name except for changing the name of the android_binary target so that's what I did. Only other option would be to somehow modify the source of the SDK samples which is out of the question.

Last hurdle was something to do with how the name mangling works between Bazel and the OpenCV Manager thing meant that none of the NDK code worked. If you include the correct .so files as part of the "jni" cc_library then those work. Only downside is each app includes its own copy of the OpenCV runtime; this was maybe a problem in the early days of Android but it's definitely not a problem for me. It's actually kind of nice to not have to deal with the separate Manager app.


Ha OK so I told my long boring story but where are the files!? I've hosted them on github in a new "bazel" repo. I suppose I'll use it to share other Bazel tricks if/when I figure them out. Enjoy!

Post has attachment
Drinking beer in Zürich!
3 Photos - View album

Post has attachment
Delicious jägerschnitzel, spätzle, and German beers!

Post has attachment
I went to Robosub again this year! I had a great time chatting with all the teams and it was nice to run into a lot of people from the old USU-still-goes-to-Robosub glory days.

Lots of good looking subs and there was big upset this year since both Cornell and University of Florida were having problems. Other things to note:
- 3D printing propeller blades is a big thing U of F was looking into. I was actually looking into it a bit on my own already so got some good tips.
- Submarines can separate after they dive, so long as all parts stay underwater, and do different tasks simultaneously. Cornell was looking into this but their mini-sub didn't attend this year.
- Maybe this was already really common but I only just noticed many teams using epoxy to do sealed cable joins. Just like a huge cylinder of it with the cables going in on the sides. Seems to work pretty well.
- There's a new less-expensive hardware supplier called Blue Robotics [0]. They have pretty decent thrusters for $100 and a lot of teams were using them. They're looking into doing pressure vessels, and other things as well!?

Robosub 2015
10 Photos - View album

Post has attachment
I ran Bay to Breakers[0] this year as Nyan Cat[1] and it was pretty great! I didn't run too hard - my knee has been getting mad at me when I do that - but still finished in 1:04:24 which comes out to ~5m22s per km. It'd be nice to get down below 1h next year!?

Anyhow I just wanted to give some context to these pictures I forgot about on my computer. Please excuse the poor quality, they're the "free" versions instead of the $40 versions... that's an entirely different story.

Bay to Breakers 2015
3 Photos - View album

Guillemot Update

Yes that's right! I've started working on this thing again. Going at it from another direction...

Guillemot Core

I've forked the old Gilligan ROS nodes at into and I've been cleaning it up a bit and trying to get most everything working on the Raspberry Pi. Unfortunately, getting ROS to work properly on Raspberry Pi is basically a nightmare.


I've been a pretty strong defender of ROS in the past, and I think that it's definitely worth using on a pure x86 Ubuntu setup (i.e., Gilligan), but I'm getting to the point where I'm tired of fighting ROS just to build a motherboard temperature publisher on a Raspberry Pi... With all the trouble ROS has already given me, I've basically done nothing productive as far as software goes, and that's incredibly troubling.

So what is it that ROS actually does that I like so much?
∙ Pub/sub and request/response communication paradigms. Transparently using IPC or sockets as needed.
∙ Marshalling - not great - but Good Enough™.
∙ Registry of publishers, subscribers, and severs for discovery.
∙ Tools to save messages published to specific channels and inspect/replay them later.
∙ Node startup/monitoring stuff. I'm looking forward to getting rid of the hacky Bash scripts.

I'm thinking ØMQ for communication, MessagePack for marshalling, a custom discovery registry, a custom persistence/replay tool, and Supervisor for startup/monitoring. This isn't something I'm going to do lightly though. I'll prototype out a few things before going all in. And this means Catastrophic Change in Guillemot Core, rather than a smooth transition, but I think there's potential for a much better future so it's definitely worth the effort.

Guillemot Sensor

This is still on the way! I'm having trouble with using library dependencies in Android Studio but once I have that figured out I'll publish to GitHub and this will be another big thing to work on. I've been planning on using MessagePack over Android Open Accessory Bridge so using it in Guillemot Core means no translation between marshalling formats!


If you've ever thought to yourself "Wow this looks cool I wish I could help out" then you're in luck! I'm making sure to do as much of my development in the open on GitHub as possible going forward. Useful contributions are more than welcome - open bugs or feature requests, edit wikis, send pull requests, whatever!

I wonder why everyone outside is screaming and yelling? I guess SF either won or lost... At least the multitude of fireworks are making me think we won. 😺

Post has attachment
Hmm which flavor of Blue Moon should I have tonight? Just write a C program for it!

Post has attachment
Guillemot Update

Meh still absolutely no progress but perhaps I will be inspired to resume work because:

RoboSub Update

I went to the 2014 RoboSub (semi-)finals and spent a ton of time just wandering around talking to teams about their subs! Quite a few brand new submarines and even a couple first-year teams in the finals. Finals start later today so be sure to check out the live stream at if you have any interest in this kind of stuff.
RoboSub 2014
13 Photos - View album

Post has attachment
Oh My Heck: SF Pride is hella crazy!
2 Photos - View album
Wait while more posts are being loaded