抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

nix flake 的 devShell 完全可以配置安卓打包环境,就是资料不是很多

基本配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
description = "Nix Building Environment for Android APP";

inputs = {
flake-utils = {
url = "github:numtide/flake-utils";
};
};

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem ( system:
let
pkgs = import nixpkgs {
inherit system;
config = {
allowUnfree = true;
android_sdk = {
accept_license = true;
};
};
};
buildToolsVersion = "35.0.0";
ndkVersion = "27.0.12077973";
androidComposition = pkgs.androidenv.composeAndroidPackages {
buildToolsVersions = [ buildToolsVersion "34.0.0" ]; # 可以放多个版本
platformVersions = [ "35" "34" ];
abiVersions = [ "x86_64" "arm64-v8a" ];
includeNDK = true;
useGoogleAPIs = false;
useGoogleTVAddOns = false;
includeEmulator = false;
includeSystemImages = false;
includeSources = false;
};
pinnedJDK = pkgs.jdk21;
androidSdk = androidComposition.androidsdk;
in {
devShells = {
default = pkgs.mkShell {
name = "Android-Build-Shell";
buildInputs = with pkgs; [
# 这里你写
] ++ [
androidSdk
pinnedJDK
];
JAVA_HOME = pinnedJDK;
ANDROID_HOME = "${androidSdk}/libexec/android-sdk";
NDK_HOME = "${androidSdk}/libexec/android-sdk/ndk/${ndkVersion}";
GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidSdk}/libexec/android-sdk/build-tools/${buildToolsVersion}/aapt2";

};
};
}
);
}

然后使用 nix develop 进入开发环境

变量

变量要看你的 build.gradleflake.nix, 一般我们让 flake 适配原来的 gradle script

1
2
3
4
5
6
7
8
9
10
11
buildscript {
ext {
buildToolsVersion = findProperty('android.buildToolsVersion') ?: '35.0.0' // flake buildToolsVersion
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '24')
compileSdkVersion = Integer.parseInt(findProperty('android.compileSdkVersion') ?: '35')
targetSdkVersion = Integer.parseInt(findProperty('android.targetSdkVersion') ?: '34')
kotlinVersion = findProperty('android.kotlinVersion') ?: '1.9.25'

ndkVersion = "27.0.12077973" // flake ndkVersion
}
}

flake.nix 里面配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
buildToolsVersion = "35.0.0";
ndkVersion = "27.0.12077973";
androidComposition = pkgs.androidenv.composeAndroidPackages {
buildToolsVersions = [ buildToolsVersion ];
platformVersions = [ "35" ];
abiVersions = [ "x86_64" "arm64-v8a" ];
includeNDK = true;
useGoogleAPIs = false;
useGoogleTVAddOns = false;
includeEmulator = false;
includeSystemImages = false;
includeSources = false;
};
pinnedJDK = pkgs.jdk21;
androidSdk = androidComposition.androidsdk;

其他可配置项目

compose-android-packages

例如指定 cmake 版本

1
2
3
4
5
androidComposition = pkgs.androidenv.composeAndroidPackages {
...
cmakeVersions = [ "3.22.1" ];
...
};

React Native

添加 nodejs 和 yarn 即可

1
2
3
4
5
6
7
8
buildInputs =
with pkgs; [
nodejs_18
yarn
] ++ [
androidSdk
pinnedJDK
];

可能遇到的问题

Still waiting for package manifests to be fetched remotely

1
2
3
Errors during XML parse:
Additionally, the fallback loader failed to parse the XML.
Still waiting for package manifests to be fetched remotely.

这是因为 gradle daemon 无法复用

1
./gradlew --stop

关掉其他 daemon 即可