Android: Difference between revisions

imported>Jmarmstrong1207
No edit summary
Pigs (talk | contribs)
m Add category development
 
(26 intermediate revisions by 17 users not shown)
Line 1: Line 1:
== Using the Android SDK and Creating Emulators with Nix ==
== Using the Android SDK ==
NixOS uses the androidenv package for building android SDKs and manually creating emulators without the use of Android Studio.


The first link provides a guide for creating a custom android SDK, using a predefined SDK, and how to manually create an emulator. The second link is an extra guide that might have some helpful tips for improving your workflow.
NixOS uses the androidenv package for building android SDKs and manually creating emulators without the use of Android Studio. Example android sdk is <code>androidenv.androidPkgs.androidsdk</code>. They also include all of the SDK tools such as sdkmanager and avdmanager needed to create emulators.


{{Note|<code><small>androidenv.androidPkgs_9_0</small></code> has been replaced with <code><u><small>androidenv.androidPkgs</small></u></code> in nixos 24.11, see [https://github.com/NixOS/nixpkgs/blob/master/nixos/doc/manual/release-notes/rl-2411.section.md#backward-incompatibilities-sec-release-2411-incompatibilities backward-incompatibilities-sec-release-2411-incompatibilities], so all the <code><small>androidPkgs</small></code> references below will be androidPkgs_9_0 if you are still using 24.05 or below.}}
The first link provides a guide for creating a custom android SDK, using a predefined SDK, and how to nixify an emulator. The second link is an extra guide that might have some helpful tips for improving your workflow.
# [https://nixos.org/manual/nixpkgs/unstable/#android Official Android SDK guide from NixOS.org]  
# [https://nixos.org/manual/nixpkgs/unstable/#android Official Android SDK guide from NixOS.org]  
# [https://sandervanderburg.blogspot.de/2014/02/reproducing-android-app-deployments-or.html  Reproducing Android app deployments]
# [https://sandervanderburg.blogspot.de/2014/02/reproducing-android-app-deployments-or.html  Reproducing Android app deployments]


== Emulators without Nix ==
When creating emulators with Nix's emulateApp function as mentioned in the first link, your IDE should now be able to recognize the emulator but you won't be able to run the code. To run it, view the first link on how to run the apk file in the emulator.
 
To run emulateApp, build it with <code>nix-build fileName.nix</code>. It'll build in the folder <code>result</code>. run it with <code>./result/bin/run-test-emulator</code>
 
== Creating emulators without Nix ==
If you don't want to nixify your emulators, you can use Android Studio and set up emulators there like a regular system.
If you don't want to nixify your emulators, you can use Android Studio and set up emulators there like a regular system.
Using <code>sdkmanager</code> and <code>avdmanager</code> from the Android SDK may not work given how Nix stores its files. You can use them from the Android Studio GUI.
When using machine images from the SDK, you will need to run them with <code>steam-run</code>, and possibly pass extra flags, e.g.:
<code>steam-run ~/Android/Sdk/emulator/emulator -feature -Vulkan @Pixel_5_API_33</code>
=== hardware acceleration ===
NOTE: Whether this here is effective needs more research and confirmation. My colleague and I have seen the emulator using around 800% CPU. So far, the following has improved that on my side.
See also the [https://github.com/NixOS/nixpkgs/issues/41703 nixpkgs issue] where people tried to trace issues.
Add your user to the <code>kvm</code> group:
<syntaxhighlight lang=nix>
{
  users.users.<your-user>.extraGroups = [ "kvm" ];
}
</syntaxhighlight>


== adb setup ==
== adb setup ==
Line 19: Line 45:
{
{
   programs.adb.enable = true;
   programs.adb.enable = true;
   users.users.<your-user>.extraGroups = ["adbusers"];
   users.users.<your-user>.extraGroups = [ "adbusers" ];
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 72: Line 98:
$ # For nixos < 19.03
$ # For nixos < 19.03
$ # nix-shell -p androidenv.platformTools
$ # nix-shell -p androidenv.platformTools
$ # for nixos <= 24.05
$ nix-shell -p androidenv.androidPkgs_9_0.platform-tools
$ nix-shell -p androidenv.androidPkgs_9_0.platform-tools
$ # For nixos >= 24.11
$ nix-shell -p androidenv.androidPkgs.platform-tools
% adb devices
% adb devices
List of devices attached
List of devices attached
Line 79: Line 108:
BH90272JCU unauthorized
BH90272JCU unauthorized
</syntaxhighlight>
</syntaxhighlight>
Troubleshooting: [https://stackoverflow.com/a/28211161 no device is listed]


A popup appears on your phone to allow your computer access to it. After allowing, you can:
A popup appears on your phone to allow your computer access to it. After allowing, you can:
Line 92: Line 123:


<syntaxhighlight lang=console>
<syntaxhighlight lang=console>
$ # For nixos < 19.03
$ nix-shell -p androidenv.androidPkgs.platform-tools
$ # nix-shell -p androidenv.platformTools
$ nix-shell -p androidenv.androidPkgs_9_0.platform-tools
% adb connect 192.168.1.10
% adb connect 192.168.1.10
% adb shell
% adb shell
Line 111: Line 140:
To develop apps using [https://developer.android.com/studio/ Android Studio], install it to your system.
To develop apps using [https://developer.android.com/studio/ Android Studio], install it to your system.


<syntaxhighlight lang=nix>
<syntaxhighlight lang="nix">
{
environment.systemPackages = [
   pkgs.androidstudio
   pkgs.android-studio
}
]
</syntaxhighlight>
 
By default, Android Studio has a FHS environment and by using <code>pkgs.android-studio-full</code> you get the predefined Android SDK composition including (as of nixos-unstable on 2024-11-02) platforms 28-34, an emulator, many system images and the NDK.
 
Notice: to install Android Studio, you have to indicate accepting the EULA. If you don't, the rebuild fails and prints the EULA. The simplest way to acknowledge it is to add this line to your config: <syntaxhighlight lang="nix">
nixpkgs.config.android_sdk.accept_license = true;
</syntaxhighlight>
</syntaxhighlight>
To use the Android Emulator, you need to enable KVM virtualization (in your BIOS) and make sure your user has permission to use KVM (add yourself to the <code>kvm</code> group).


=== gradlew ===
=== gradlew ===
Line 128: Line 165:
   targetPkgs = pkgs: (with pkgs;
   targetPkgs = pkgs: (with pkgs;
     [
     [
       androidenv.androidPkgs_9_0.androidsdk
       androidenv.androidPkgs.androidsdk
       glibc
       glibc
     ]);
     ]);
Line 141: Line 178:


let
let
   androidSdk = pkgs.androidenv.androidPkgs_9_0.androidsdk;
   androidSdk = pkgs.androidenv.androidPkgs.androidsdk;
in
in
pkgs.mkShell {
pkgs.mkShell {
Line 157: Line 194:
=== Building Android on NixOS ===
=== 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.
It's possible to use nix-shell with buildFHSEnv 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">
<syntaxhighlight lang="nix">
{ pkgs ? import <nixpkgs> {} }:
{ pkgs ? import <nixpkgs> {} }:
   
   
let fhs = pkgs.buildFHSUserEnv {
let fhs = pkgs.buildFHSEnv {
   name = "android-env";
   name = "android-env";
   targetPkgs = pkgs: with pkgs;
   targetPkgs = pkgs: with pkgs;
     [ git
     [
      git
       gitRepo
       gitRepo
       gnupg
       gnupg
Line 173: Line 211:
       gnumake
       gnumake
       nettools
       nettools
      # For nixos < 19.03, use `androidenv.platformTools`
       androidenv.androidPkgs.platform-tools
       androidenv.androidPkgs_9_0.platform-tools
       jdk
       jdk
       schedtool
       schedtool
Line 206: Line 243:
   shellHook = "exec android-env";
   shellHook = "exec android-env";


}
</syntaxhighlight>
=== Android Debug Bridge ===
Run <code>nix-shell -p usbutils --run "lsusb"</code> on your terminal to get the list of USB devices connected to your computer. Sample output:<pre>
...
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 002 Device 007: ID 0fce:320d Sony Ericsson Mobile Communications AB Xperia 5 III
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
...
</pre>
<code>ID 0fce:320d</code> can be seen as: <code>idVendor = 0fce</code> and <code>idProduct = 320d</code>.
<syntaxhighlight lang="nix">
{
  programs.adb.enable = true;
  services.udev.extraRules =
    let
      # nix-shell -p usbutils --run "lsusb"
      idVendor = "0fce"; # Change according to the guide above
      idProduct = "320d"; # Change according to the guide above
    in
    ''
      SUBSYSTEM=="usb", ATTR{idVendor}=="${idVendor}", MODE="[]", GROUP="adbusers", TAG+="uaccess"
      SUBSYSTEM=="usb", ATTR{idVendor}=="${idVendor}", ATTR{idProduct}=="${idProduct}", SYMLINK+="android_adb"
      SUBSYSTEM=="usb", ATTR{idVendor}=="${idVendor}", ATTR{idProduct}=="${idProduct}", SYMLINK+="android_fastboot"
    '';
  # add user to adbusers group
  users.users.myUser = {
  isNormalUser = true;
  extraGroups = [ "adbusers" ];
  };
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 213: Line 285:
# [https://github.com/danielfullmer/robotnix robotnix], building aosp roms (e.g. LineageOS) with nix.
# [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.
# [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.
# [https://wiki.archlinux.org/title/Android_Debug_Bridge Archlinux Wiki to Android_Debug_Bridge]
[[Category:Development]]