ndk-build2 1.3.14

Utilities for building Android binaries
Documentation
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use {
    crate::{error::NdkError, target::Target},
    dunce::canonicalize,
    std::{
        collections::HashMap,
        env::var,
        fs::{create_dir_all, read_dir, read_to_string, write},
        path::{Path, PathBuf},
        process::{Command, Stdio},
    },
};

/// 通过 [`Ndk::debug_key`] 创建默认 `debug.keystore` 时使用的默认密码
pub const DEFAULT_DEV_KEYSTORE_PASSWORD: &str = "android";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ndk {
    build_tools_path: PathBuf,
    user_home: PathBuf,
    ndk_path: PathBuf,
    sdk_path: PathBuf,
    build_tools_version: String,
    build_tag: u32,
    platforms: Vec<u32>,
}

impl Ndk {
    //noinspection SpellCheckingInspection
    pub fn from_env() -> Result<Self, NdkError> {
        let user_home = {
            let user_home = var("ANDROID_SDK_HOME")
                .map(PathBuf::from)
                // 与 ANDROID_USER_HOME 不同,ANDROID_SDK_HOME 指向 .android 的 _父_ 目录:
                // https://developer.android.com/studio/command-line/variables#envar
                .map(|home| home.join(".android"))
                .ok();

            if user_home.is_some() {
                eprintln!(
                    "Warning: Environment variable ANDROID_SDK_HOME is deprecated \
                    (https://developer.android.com/studio/command-line/variables#envar). \
                    It will be used until it is unset and replaced by ANDROID_USER_HOME."
                );
            }

            // 默认为 $HOME/.android
            user_home
                .or_else(|| var("ANDROID_USER_HOME").map(PathBuf::from).ok())
                .or_else(|| dirs::home_dir().map(|home| home.join(".android")))
                .ok_or_else(|| NdkError::PathNotFound(PathBuf::from("$HOME")))?
        };
        let sdk_path = android_build::android_sdk().ok_or(NdkError::SdkNotFound)?;

        let ndk_path = {
            let ndk_path = var("ANDROID_NDK_ROOT")
                .ok()
                .or_else(|| var("ANDROID_NDK_PATH").ok())
                .or_else(|| var("ANDROID_NDK_HOME").ok())
                .or_else(|| var("NDK_HOME").ok());

            // 默认 ndk 安装路径
            if ndk_path.is_none() && sdk_path.join("ndk-bundle").exists() {
                sdk_path.join("ndk-bundle")
            } else {
                PathBuf::from(ndk_path.ok_or(NdkError::NdkNotFound)?)
            }
        };

        let build_tools_path = sdk_path.join("build-tools");
        let build_tools_version = read_dir(&build_tools_path)
            .or(Err(NdkError::PathNotFound(build_tools_path.clone())))?
            .filter_map(|path| path.ok())
            .filter(|path| path.path().is_dir())
            .filter_map(|path| path.file_name().into_string().ok())
            .filter(|name| name.chars().next().unwrap().is_ascii_digit())
            .max()
            .ok_or(NdkError::BuildToolsNotFound)?;

        let build_tag = read_to_string(ndk_path.join("source.properties"))
            .expect("Failed to read source.properties");

        let build_tag = build_tag
            .split('\n')
            .find_map(|line| {
                let (key, value) = line
                    .split_once('=')
                    .expect("Failed to parse `key = value` from source.properties");
                if key.trim() == "Pkg.Revision" {
                    // AOSP 将不断增加的版本号写入补丁字段。此数字会随着 NDK 版本的推移而不断增加。
                    let mut parts = value.trim().split('.');
                    let _major = parts.next().unwrap();
                    let _minor = parts.next().unwrap();
                    let patch = parts.next().unwrap();
                    // 可以有一个可选的"XXX-beta1"
                    let patch = patch.split_once('-').map_or(patch, |(patch, _beta)| patch);
                    Some(patch.parse().expect("Failed to parse patch field"))
                } else {
                    None
                }
            })
            .expect("No `Pkg.Revision` in source.properties");

        let ndk_platforms = read_to_string(ndk_path.join("build/core/platforms.mk"))?;
        let ndk_platforms = ndk_platforms
            .split('\n')
            .map(|s| s.split_once(" := ").unwrap())
            .collect::<HashMap<_, _>>();

        let min_platform_level = ndk_platforms["NDK_MIN_PLATFORM_LEVEL"].parse::<u32>()?;
        let max_platform_level = ndk_platforms["NDK_MAX_PLATFORM_LEVEL"].parse::<u32>()?;

        let platforms_dir = sdk_path.join("platforms");
        let platforms: Vec<u32> = read_dir(&platforms_dir)
            .or(Err(NdkError::PathNotFound(platforms_dir)))?
            .filter_map(|path| path.ok())
            .filter(|path| path.path().is_dir())
            .filter_map(|path| path.file_name().into_string().ok())
            .filter_map(|name| {
                name.strip_prefix("android-")
                    .and_then(|api| api.split('.').next())
                    .and_then(|api| api.parse::<u32>().ok())
            })
            .filter(|level| (min_platform_level..=max_platform_level).contains(level))
            .collect();

        if platforms.is_empty() {
            return Err(NdkError::NoPlatformFound);
        }

        Ok(Self {
            build_tools_path,
            user_home,
            ndk_path,
            sdk_path,
            build_tools_version,
            build_tag,
            platforms,
        })
    }

    pub fn ndk(&self) -> &Path {
        &self.ndk_path
    }

    pub fn build_tools_version(&self) -> &str {
        &self.build_tools_version
    }

    pub fn build_tag(&self) -> u32 {
        self.build_tag
    }

    pub fn platforms(&self) -> &[u32] {
        &self.platforms
    }

    pub fn android_sdk(&self) -> &Path {
        &self.sdk_path
    }

    pub fn build_tools(&self) -> PathBuf {
        self.build_tools_path.join(&self.build_tools_version)
    }

    pub fn build_tool(&self, tool: &str) -> Result<Command, NdkError> {
        let path = self.build_tools().join(tool);
        if !path.exists() {
            return Err(NdkError::CmdNotFound(tool.to_string()));
        }

        let mut cmd = Command::new(canonicalize(path)?);
        cmd.stdin(Stdio::null());
        Ok(cmd)
    }

    pub fn platform_tool_path(&self, tool: &str) -> Result<PathBuf, NdkError> {
        let path = self.android_sdk().join("platform-tools").join(tool);
        if !path.exists() {
            return Err(NdkError::CmdNotFound(tool.to_string()));
        }

        Ok(canonicalize(path)?)
    }

    pub fn adb_path(&self) -> Result<PathBuf, NdkError> {
        self.platform_tool_path(bin!("adb"))
    }

    pub fn platform_tool(&self, tool: &str) -> Result<Command, NdkError> {
        let mut cmd = Command::new(self.platform_tool_path(tool)?);
        cmd.stdin(Stdio::null());
        Ok(cmd)
    }

    pub fn highest_supported_platform(&self) -> u32 {
        self.platforms().iter().max().cloned().unwrap()
    }

    /// 返回当前 [Google Play 所要求的] 平台"37"或更低版本(如果检测到的 SDK 尚不支持)。
    ///
    /// [Google Play 要求]: https://developer.android.com/distribute/best-practices/develop/target-sdk
    pub fn default_target_platform(&self) -> u32 {
        self.highest_supported_platform().min(37)
    }

    pub fn platform_dir(&self, platform: u32) -> Result<PathBuf, NdkError> {
        let platforms_dir = self.android_sdk().join("platforms");
        // Try exact match first (e.g. "android-36"), then versioned (e.g. "android-37.0")
        let exact = platforms_dir.join(format!("android-{}", platform));
        if exact.exists() {
            return Ok(exact);
        }
        if let Ok(entries) = std::fs::read_dir(&platforms_dir) {
            for entry in entries.flatten() {
                let name = entry.file_name();
                let name = name.to_string_lossy();
                if let Some(api) = name.strip_prefix("android-")
                    && api.split('.').next() == Some(&platform.to_string())
                {
                    return Ok(entry.path());
                }
            }
        }
        Err(NdkError::PlatformNotFound(platform))
    }

    pub fn android_jar(&self, api_level: u32) -> Result<PathBuf, NdkError> {
        // Try via android_build crate first, then fall back to platform_dir lookup
        if let Some(android_jar) =
            android_build::android_jar(Some(format!("android-{}", api_level).as_str()))
        {
            return Ok(android_jar);
        }
        // Handle new SDK naming like "android-37.0"
        if let Some(android_jar) = self
            .platform_dir(api_level)
            .ok()
            .map(|dir| dir.join("android.jar"))
            .filter(|path| path.exists())
        {
            return Ok(android_jar);
        }
        Err(NdkError::PlatformNotFound(api_level))
    }

    //noinspection SpellCheckingInspection
    fn host_arch() -> Result<&'static str, NdkError> {
        let host_os = var("HOST").ok();
        let host_contains = |s| host_os.as_ref().map(|h| h.contains(s)).unwrap_or(false);

        Ok(if host_contains("linux") {
            "linux"
        } else if host_contains("macos") {
            "darwin"
        } else if host_contains("windows") {
            "windows"
        } else if host_contains("android") {
            "android"
        } else if cfg!(target_os = "linux") {
            "linux"
        } else if cfg!(target_os = "macos") {
            "darwin"
        } else if cfg!(target_os = "windows") {
            "windows"
        } else if cfg!(target_os = "android") {
            "android"
        } else {
            return match host_os {
                Some(host_os) => Err(NdkError::UnsupportedHost(host_os)),
                _ => Err(NdkError::UnsupportedTarget),
            };
        })
    }

    pub fn toolchain_dir(&self) -> Result<PathBuf, NdkError> {
        let arch = Self::host_arch()?;
        let mut toolchain_dir = self
            .ndk_path
            .join("toolchains")
            .join("llvm")
            .join("prebuilt")
            .join(format!("{}-x86_64", arch));
        if !toolchain_dir.exists() {
            toolchain_dir.set_file_name(arch);
        }
        if !toolchain_dir.exists() {
            return Err(NdkError::PathNotFound(toolchain_dir));
        }

        Ok(toolchain_dir)
    }

    pub fn clang(&self) -> Result<(PathBuf, PathBuf), NdkError> {
        let ext = if cfg!(target_os = "windows") {
            "exe"
        } else {
            ""
        };

        let bin_path = self.toolchain_dir()?.join("bin");

        let clang = bin_path.join("clang").with_extension(ext);
        if !clang.exists() {
            return Err(NdkError::PathNotFound(clang));
        }

        let clang_pp = bin_path.join("clang++").with_extension(ext);
        if !clang_pp.exists() {
            return Err(NdkError::PathNotFound(clang_pp));
        }

        Ok((clang, clang_pp))
    }

    pub fn toolchain_bin(&self, name: &str, target: Target) -> Result<PathBuf, NdkError> {
        let ext = if cfg!(target_os = "windows") {
            ".exe"
        } else {
            ""
        };

        let toolchain_path = self.toolchain_dir()?.join("bin");

        // Since r21 (https://github.com/android/ndk/wiki/Changelog-r21) LLVM binutils are included _for testing_;
        // Since r22 (https://github.com/android/ndk/wiki/Changelog-r22) GNU binutils are deprecated in favor of LL-VM's;
        // Since r23 (https://github.com/android/ndk/wiki/Changelog-r23) GNU binutils have been removed.
        // To maintain stability with the current ndk-build crate release, prefer GNU binutils for
        // as long as it is provided by the NDK instead of trying to use llvm-* from r21 onwards.
        let gnu_bin = format!("{}-{}{}", target.ndk_triple(), name, ext);
        let gnu_path = toolchain_path.join(&gnu_bin);
        if gnu_path.exists() {
            Ok(gnu_path)
        } else {
            let llvm_bin = format!("llvm-{}{}", name, ext);
            let llvm_path = toolchain_path.join(&llvm_bin);
            if llvm_path.exists() {
                Ok(llvm_path)
            } else {
                Err(NdkError::ToolchainBinaryNotFound {
                    toolchain_path,
                    gnu_bin,
                    llvm_bin,
                })
            }
        }
    }

    pub fn prebuilt_dir(&self) -> Result<PathBuf, NdkError> {
        let arch = Self::host_arch()?;
        let prebuilt_dir = self
            .ndk_path
            .join("prebuilt")
            .join(format!("{}-x86_64", arch));
        if !prebuilt_dir.exists() {
            Err(NdkError::PathNotFound(prebuilt_dir))
        } else {
            Ok(prebuilt_dir)
        }
    }

    pub fn ndk_gdb(
        &self,
        launch_dir: impl AsRef<Path>,
        launch_activity: &str,
        device_serial: Option<&str>,
    ) -> Result<(), NdkError> {
        let abi = self.detect_abi(device_serial)?;
        let jni_dir = launch_dir.as_ref().join("jni");
        create_dir_all(&jni_dir)?;
        write(
            jni_dir.join("Android.mk"),
            format!("APP_ABI={}\nTARGET_OUT=\n", abi.android_abi()),
        )?;
        let mut ndk_gdb = Command::new(self.prebuilt_dir()?.join("bin").join(cmd!("ndk-gdb")));

        if let Some(device_serial) = &device_serial {
            ndk_gdb.arg("-s").arg(device_serial);
        }

        ndk_gdb
            .arg("--adb")
            .arg(self.adb_path()?)
            .arg("--launch")
            .arg(launch_activity)
            .current_dir(launch_dir)
            .status()?;

        Ok(())
    }

    pub fn android_user_home(&self) -> Result<PathBuf, NdkError> {
        let android_user_home = self.user_home.clone();
        create_dir_all(&android_user_home)?;

        Ok(android_user_home)
    }

    pub fn keytool(&self) -> Result<Command, NdkError> {
        if let Ok(keytool) = which::which(bin!("keytool")) {
            let mut cmd = Command::new(keytool);
            cmd.stdin(Stdio::null());
            return Ok(cmd);
        }
        if let Some(java) = android_build::java_home() {
            let keytool = java.join("bin").join(bin!("keytool"));
            if keytool.exists() {
                let mut cmd = Command::new(keytool);
                cmd.stdin(Stdio::null());
                return Ok(cmd);
            }
        }

        Err(NdkError::CmdNotFound("keytool".to_string()))
    }

    /// Returns a `Command` for the `java` executable, used to run Java-based tools directly.
    pub fn java_cmd(&self) -> Result<Command, NdkError> {
        if let Ok(java) = which::which(bin!("java")) {
            let mut cmd = Command::new(java);
            cmd.stdin(Stdio::null());
            return Ok(cmd);
        }
        if let Some(java_home) = android_build::java_home() {
            let java = java_home.join("bin").join(bin!("java"));
            if java.exists() {
                let mut cmd = Command::new(java);
                cmd.stdin(Stdio::null());
                return Ok(cmd);
            }
        }
        Err(NdkError::CmdNotFound("java".to_string()))
    }

    /// Returns the classpath for running d8 directly via Java, bypassing the d8 shell script.
    /// This avoids issues with shell script compatibility (e.g. CRLF line endings) on Linux/macOS.
    pub fn d8_classpath(&self) -> Result<String, NdkError> {
        let lib_dir = self.build_tools().join("lib");
        let mut classpath = String::new();
        if let Ok(entries) = std::fs::read_dir(&lib_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.extension().is_some_and(|ext| ext == "jar") {
                    if !classpath.is_empty() {
                        classpath.push(if cfg!(windows) { ';' } else { ':' });
                    }
                    classpath.push_str(&path.to_string_lossy());
                }
            }
        }
        if classpath.is_empty() {
            return Err(NdkError::PathNotFound(lib_dir));
        }
        Ok(classpath)
    }

    //noinspection SpellCheckingInspection
    pub fn debug_key(&self) -> Result<Key, NdkError> {
        let path = self.android_user_home()?.join("debug.keystore");
        let password = DEFAULT_DEV_KEYSTORE_PASSWORD.to_owned();

        if !path.exists() {
            let mut keytool = self.keytool()?;
            keytool
                .arg("-genkey")
                .arg("-v")
                .arg("-keystore")
                .arg(&path)
                .arg("-storepass")
                .arg(&password)
                .arg("-alias")
                .arg("androiddebugkey")
                .arg("-keypass")
                .arg(&password)
                .arg("-dname")
                .arg("CN=Android Debug,O=Android,C=US")
                .arg("-keyalg")
                .arg("RSA")
                .arg("-keysize")
                .arg("2048")
                .arg("-validity")
                .arg("10000");
            if !keytool.status()?.success() {
                return Err(NdkError::CmdFailed(Box::new(keytool)));
            }
        }

        Ok(Key { path, password })
    }

    pub fn sysroot_lib_dir(&self, target: Target) -> Result<PathBuf, NdkError> {
        let sysroot_lib_dir = self
            .toolchain_dir()?
            .join("sysroot")
            .join("usr")
            .join("lib")
            .join(target.ndk_triple());
        if !sysroot_lib_dir.exists() {
            return Err(NdkError::PathNotFound(sysroot_lib_dir));
        }

        Ok(sysroot_lib_dir)
    }

    pub fn sysroot_platform_lib_dir(
        &self,
        target: Target,
        min_sdk_version: u32,
    ) -> Result<PathBuf, NdkError> {
        let sysroot_lib_dir = self.sysroot_lib_dir(target)?;

        // Look for a platform <= min_sdk_version
        let mut tmp_platform = min_sdk_version;
        while tmp_platform > 1 {
            let path = sysroot_lib_dir.join(tmp_platform.to_string());
            if path.exists() {
                return Ok(path);
            }
            tmp_platform += 1;
        }

        // Look for the minimum API level supported by the NDK
        let mut tmp_platform = min_sdk_version;
        while tmp_platform < 100 {
            let path = sysroot_lib_dir.join(tmp_platform.to_string());
            if path.exists() {
                return Ok(path);
            }
            tmp_platform += 1;
        }

        Err(NdkError::PlatformNotFound(min_sdk_version))
    }

    //noinspection SpellCheckingInspection
    pub fn detect_abi(&self, device_serial: Option<&str>) -> Result<Target, NdkError> {
        let mut adb = self.adb(device_serial)?;

        let stdout = adb
            .arg("shell")
            .arg("getprop")
            .arg("ro.product.cpu.abi")
            .output()?
            .stdout;
        let abi = std::str::from_utf8(&stdout).or(Err(NdkError::UnsupportedTarget))?;
        Target::from_android_abi(abi.trim())
    }

    pub fn adb(&self, device_serial: Option<&str>) -> Result<Command, NdkError> {
        let mut adb = Command::new(self.adb_path()?);

        if let Some(device_serial) = device_serial {
            adb.arg("-s").arg(device_serial);
        }

        Ok(adb)
    }
}

pub struct Key {
    pub path: PathBuf,
    pub password: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[ignore]
    fn test_detect() {
        let ndk = Ndk::from_env().unwrap();
        assert_eq!(ndk.build_tools_version(), "29.0.2");
        assert_eq!(ndk.platforms(), &[29, 28]);
    }
}