Difference between revisions of "Android"

From NixOS Wiki
Jump to: navigation, search
(Flutter developement environment instructions)
Line 1: Line 1:
== adb setup ==
+
== Developement enviroment ==
  
To enable <code>adb</code> in NixOS for unprivileged users
+
To be able to build flutter apps, you need to add the platform-specific dependencies to your environment. This may be done by running <code>nix-shell</code> using this shell.nix configuration:
add these lines to your <code>configuration.nix</code>.
 
This is mandatory for all further interactions with your android device.
 
  
 
<syntaxhighlight lang=nix>
 
<syntaxhighlight lang=nix>
{
+
{ pkgs ? import <nixpkgs> {} }:
  programs.adb.enable = true;
+
let android-nixpkgs = (pkgs.callPackage (import (builtins.fetchGit {url = "https://github.com/tadfisher/android-nixpkgs.git";})) {channel = "stable";});
  users.users.<your-user>.extraGroups = ["adbusers"];
 
}
 
</syntaxhighlight>
 
 
 
This will add [https://github.com/M0Rf30/android-udev-rules additional udev rules] for unprivileged access as well as add adb to your <code>$PATH</code>.
 
 
 
Alternatively, if you don't want to install adb globally but do want to configure the udev rules, you can:
 
 
 
<syntaxhighlight lang=nix>
 
{
 
  services.udev.packages = [
 
    pkgs.android-udev-rules
 
  ];
 
}
 
</syntaxhighlight>
 
 
 
=== Use Older Platform Version ===
 
If you would like to get older platform version, you can write the following.
 
 
 
<syntaxhighlight lang=nix>
 
{ pkgs ? import <nixpkgs> {
 
  config.android_sdk.accept_license = true;
 
  overlays = [
 
    (self: super: {
 
      androidPkgs_8_0 = super.androidenv.composeAndroidPackages {
 
        platformVersions = [ "26" ];
 
        abiVersions = [ "x86" "x86_64"];
 
      };
 
    })
 
  ];
 
} }:
 
 
 
(pkgs.buildFHSUserEnv {
 
  name = "android-sdk-env";
 
  targetPkgs = pkgs: (with pkgs;
 
    [
 
      androidPkgs_8_0.androidsdk
 
      glibc
 
    ]);
 
  runScript = "bash";
 
}).env
 
 
 
</syntaxhighlight>
 
 
 
== Interaction with your Android device ==
 
 
 
=== adb shell on device ===
 
 
 
First open a nix-shell with the platform tools and connect your device:
 
 
 
<syntaxhighlight lang=console>
 
$ # For nixos < 19.03
 
$ # nix-shell -p androidenv.platformTools
 
$ nix-shell -p androidenv.androidPkgs_9_0.platform-tools
 
% adb devices
 
List of devices attached
 
* daemon not running; starting now at tcp:5037
 
* daemon started successfully
 
BH90272JCU unauthorized
 
</syntaxhighlight>
 
 
 
A popup appears on your phone to allow your computer access to it. After allowing, you can:
 
 
 
<syntaxhighlight lang=console>
 
% adb devices
 
List of devices attached
 
BH90272JCU device
 
% adb shell
 
</syntaxhighlight>
 
 
 
You can also connect to an already-running adb server:
 
 
 
<syntaxhighlight lang=console>
 
$ # For nixos < 19.03
 
$ # nix-shell -p androidenv.platformTools
 
$ nix-shell -p androidenv.androidPkgs_9_0.platform-tools
 
% adb connect 192.168.1.10
 
% adb shell
 
</syntaxhighlight>
 
 
 
=== Transferring files from/to an Android device ===
 
 
 
There are two main methods for newer devices:
 
* <code>adb push</code> and <code>adb pull</code>: see above.
 
* via [[MTP]], see [[MTP|the corresponding page]]
 
 
 
== Android Development ==
 
 
 
=== Android Studio ===
 
 
 
To develop apps using [https://developer.android.com/studio/ Android Studio]
 
 
 
<syntaxhighlight lang=console>
 
$ nix-shell -p android-studio --run android-studio
 
</syntaxhighlight>
 
 
 
=== gradlew ===
 
 
 
It's possible to create a build environment (shell.nix) to use with gradlew as a FHS environment:
 
  
<syntaxhighlight lang=nix>
+
in pkgs.mkShell {
{ pkgs ? import <nixpkgs> {config.android_sdk.accept_license = true;} }:
 
 
 
(pkgs.buildFHSUserEnv {
 
  name = "android-sdk-env";
 
  targetPkgs = pkgs: (with pkgs;
 
    [
 
      androidenv.androidPkgs_9_0.androidsdk
 
      glibc
 
    ]);
 
  runScript = "bash";
 
}).env
 
</syntaxhighlight>
 
 
 
As an alternative, it's often enough to override just the aapt2 binary for the gradle build process:
 
 
 
<syntaxhighlight lang=nix>
 
{ pkgs ? import <nixpkgs> {config.android_sdk.accept_license = true;} }:
 
 
 
let
 
  androidSdk = pkgs.androidenv.androidPkgs_9_0.androidsdk;
 
in
 
pkgs.mkShell {
 
 
   buildInputs = with pkgs; [
 
   buildInputs = with pkgs; [
     androidSdk
+
     flutter
     glibc
+
     #linux build
 +
    cmake clang ninja pkg-config pkgconfig gtk3 libepoxy
 +
    #android build
 +
    (android-nixpkgs.sdk (sdkPkgs: with sdkPkgs; [cmdline-tools-latest build-tools-32-0-0 platform-tools platforms-android-31 system-images-android-31-default-x86-64 emulator])) jdk
 +
    #web build
 +
    chromium
 
   ];
 
   ];
   # override the aapt2 that gradle uses with the nix-shipped version
+
   CHROME_EXECUTABLE = "${pkgs.chromium}";
  GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidSdk}/libexec/android-sdk/build-tools/28.0.3/aapt2";
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
See the [https://nixos.org/manual/nixpkgs/unstable/#android androidenv documentation] for full examples.
+
This uses the tool android-nixpkgs made by tadfisher to get all the android dependencies (be sure to change the package versions/branch to your needs; they can be found here: https://github.com/tadfisher/android-nixpkgs/tree/main/channels )
 
 
=== Building Android on NixOS ===
 
 
 
It's possible to use nix-shell with buildFHSUserEnv to set up an environment in which it's viable to build Android without huge amounts of editing. This is an example shell.nix file.
 
<syntaxhighlight lang="nix">
 
{ pkgs ? import <nixpkgs> {} }:
 
 
let fhs = pkgs.buildFHSUserEnv {
 
  name = "android-env";
 
  targetPkgs = pkgs: with pkgs;
 
    [ git
 
      gitRepo
 
      gnupg
 
      python2
 
      curl
 
      procps
 
      openssl
 
      gnumake
 
      nettools
 
      # For nixos < 19.03, use `androidenv.platformTools`
 
      androidenv.androidPkgs_9_0.platform-tools
 
      jdk
 
      schedtool
 
      util-linux
 
      m4
 
      gperf
 
      perl
 
      libxml2
 
      zip
 
      unzip
 
      bison
 
      flex
 
      lzop
 
      python3
 
    ];
 
  multiPkgs = pkgs: with pkgs;
 
    [ zlib
 
      ncurses5
 
    ];
 
  runScript = "bash";
 
  profile = ''
 
    export ALLOW_NINJA_ENV=true
 
    export USE_CCACHE=1
 
    export ANDROID_JAVA_HOME=${pkgs.jdk.home}
 
    export LD_LIBRARY_PATH=/usr/lib:/usr/lib32
 
  '';
 
};
 
in pkgs.stdenv.mkDerivation {
 
  name = "android-env-shell";
 
  nativeBuildInputs = [ fhs ];
 
  shellHook = "exec android-env";
 
 
 
}
 
</syntaxhighlight>
 
 
 
# [https://nixos.org/nix-dev/2015-April/016881.html more information on that snippet]
 
# [https://gist.github.com/Nadrieril/d006c0d9784ba7eff0b092796d78eb2a A shell.nix to build LineageOS]
 
# [https://github.com/danielfullmer/robotnix robotnix], building aosp roms (e.g. LineageOS) with nix.
 
# [https://github.com/mrVanDalo/LineagoOS-build LineageOS build setup using terranix and hcloud], based on the [https://gist.github.com/Nadrieril/d006c0d9784ba7eff0b092796d78eb2a shell.nix to build LineageOS]. Useful if you are in a rush and don't have to much CPU power on your hand.
 
 
 
== Using the nixpkgs androidenv ==
 
 
 
User Sander van der Burg created two awesome blog posts as well as a number of test cases usable as examples on how to use the nixpkgs androidenv and the emulator provided:
 
 
 
# [https://nixos.org/manual/nixpkgs/unstable/#android androidenv documentation]
 
# [https://sandervanderburg.blogspot.de/2012/11/building-android-applications-with-nix.html  Building Android applications with the Nix package manager ]
 
# [https://sandervanderburg.blogspot.de/2014/02/reproducing-android-app-deployments-or.html  Reproducing Android app deployments (or playing Angry Birds on NixOS)]
 
# [https://github.com/svanderburg/nix-androidenvtests A set of androidenv test cases]
 

Revision as of 20:41, 28 January 2023

Developement enviroment

To be able to build flutter apps, you need to add the platform-specific dependencies to your environment. This may be done by running nix-shell using this shell.nix configuration:

{ pkgs ? import <nixpkgs> {} }:
let android-nixpkgs = (pkgs.callPackage (import (builtins.fetchGit {url = "https://github.com/tadfisher/android-nixpkgs.git";})) {channel = "stable";});

in pkgs.mkShell {
  buildInputs = with pkgs; [
    flutter
    #linux build
    cmake clang ninja pkg-config pkgconfig gtk3 libepoxy
    #android build
    (android-nixpkgs.sdk (sdkPkgs: with sdkPkgs; [cmdline-tools-latest build-tools-32-0-0 platform-tools platforms-android-31 system-images-android-31-default-x86-64 emulator])) jdk
    #web build
    chromium
  ];
  CHROME_EXECUTABLE = "${pkgs.chromium}";
}

This uses the tool android-nixpkgs made by tadfisher to get all the android dependencies (be sure to change the package versions/branch to your needs; they can be found here: https://github.com/tadfisher/android-nixpkgs/tree/main/channels )