AboutServicesProjectsBlog
Projects
Hanging Plotter
First Steps - Pinning an idea down and checking assumptions
Post Print Iterations
ESP32 development in NixOS using VSCode
Spinning a Stepper
Android development on NixOS
Connecting an ESP32 to android with bluetooth
Using an ESP32 as a logic analyzer
Using an Arduino as a logic analyzer
Driving steppers with the RMT module
Using Nix to write rust on the esp32
Using a smooth stepper driver on the esp32 in rust
Translating an esp32+esp-idf bluetooth example to rust
Musings on packaging build system via splitting independent libraries

Android development on NixOS

5th article in Hanging Plotter
SoftwareNixOSAndroid
2020-3-19

Here we go, time for PAIN

First step is to get code running on an android device. I'm interested in trying flutter/dart and there appears to be recent github movement

15 hours of work later! Note this works as of 2020-03-19, nixpkgs ddf87fb1baf8f5022281dad13fb318fa5c17a7c6 and flutter-testing e5bcc251c8c5f3c483a7e62385d3c5b96db3e2c2 and 19.09 stable

shell.nix

{ pkgs ? import <nixpkgs> {} }:

let
  flutterPkgs = (import (builtins.fetchTarball "https://github.com/babariviere/nixpkgs/archive/flutter-testing.tar.gz")  {});
  unstablePkgs    = (import (builtins.fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixpkgs-unstable.tar.gz")   {});
in
  pkgs.mkShell {
    buildInputs = with pkgs; [
      flutterPkgs.flutter
      unstablePkgs.android-studio
      jdk
      git
    ];

    shellHook=''
      export USE_CCACHE=1
      export ANDROID_JAVA_HOME=${pkgs.jdk.home}
      export ANDROID_HOME=~/.androidsdk
      export FLUTTER_SDK=${flutterPkgs.flutter.unwrapped}
    '';
  }

Unfortunately the method flutter was packaged with doesn't cooperate with the androidPkgs so we are stuck with android-studio which uses a fhs environment (chroot with a standard linux file layout) Thus we can only interact with the android-sdk through fhs environments as well since the binaries aren't wrapped.

Some system level configuration changes:

configuration.nix

  programs.adb.enable = true;

  boot.kernelModules = [ "kvm-amd" ];
  virtualisation.libvirtd.enable = true;

  users.users.username = {
      ....
      extraGroups = [
          ...
          "adbusers"
      ]
  }

  boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.linux_4_19.override {
    argsOverride = rec {
      src = pkgs.fetchurl {
            url = "mirror://kernel/linux/kernel/v4.x/linux-${version}.tar.xz";
            sha256 = "0h02pxzzwc5w2kfqw686bpxc13a93yq449lyzxxkxq1qilcsqjv5";
      };
      version = "4.19.107";
      modDirVersion = "4.19.107";
      };
  });

~/.config/nixpkgs/config.nix

{
  ...
  android_sdk.accept_license = true;
}

The kernel pin to 4.19.107 was an interesting one, whichever version I was on has a bug in kvm-amd which caused nixos-rebuild switch and modprobe kvm-amd to hang with some inscrutable errors. I discovered this with dmesg | grep kvm which had a line like kernel BUG at arch/x86/kvm/mmu.c:296!

At this point I can run android-studio inside the shell,and follow the setup instructions and install/configure the flutter plugin. After loading set the flutter sdk path to the $FLUTTER_SDK variable, and setup an avd and load an emulator

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Linux, locale en_US.UTF-8)
[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    ✗ Android license status unknown.
      Try re-installing or updating your Android SDK Manager.
      See https://developer.android.com/studio/#downloads or visit
      https://flutter.dev/setup/#android-setup for detailed instructions.
[✓] Android Studio (version 3.6)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.
$ flutter run
...

flutter hello world application running on an emulator
Never have I been so happy to see a demo application

Next step is the fun one, getting flutter to talk to the esp32

PreviousNext
Featured Projects
GeneratorHandwheel Repair
Company Info
About UsContactAffiliate DisclosurePrivacy Policy
Specific Solutions LLC
Portland, OR