You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写自定义ls命令并在已Root安卓终端运行及C代码部署疑问

Running Your Custom ls Command on Rooted Android

Great job getting your custom ls code written! Let's walk through how to get it running on your rooted Android device, and answer your question about deploying to /system/bin.

First: Compile Your C Code for Android

Android uses different CPU architectures (most commonly ARM64 or 32-bit ARM) than desktop x86 systems, so you need to compile your code to match your device's hardware. Here are two straightforward ways:

Option 1: Cross-Compile on a Desktop (Linux/macOS)

  1. Grab the Android NDK (Native Development Kit) — you can get it via Android Studio or as a standalone download.
  2. Identify your device's architecture: Run uname -m in a rooted terminal (output like aarch64 for ARM64, armv7l for 32-bit ARM).
  3. Use the NDK's toolchain to compile:
    • For ARM64: aarch64-linux-android29-clang your_ls.c -o custom_ls (replace 29 with your device's Android API level if needed)
    • For 32-bit ARM: armv7a-linux-androideabi29-clang your_ls.c -o custom_ls

Option 2: Compile Directly on Android (Easier for Testing)

Use Termux, a powerful terminal emulator for Android:

  1. Install Termux from F-Droid (the Google Play version is outdated).
  2. Open Termux and install the compiler: pkg install clang
  3. Copy your C code into Termux's storage: cp /sdcard/your_ls.c ~/
  4. Compile: clang your_ls.c -o custom_ls

Deploy the Binary to Your Rooted Device

If you cross-compiled on a desktop, transfer the binary to your device via ADB:
adb push custom_ls /sdcard/

Install It to /system/bin (Or Run It Directly)

Your question: Is putting it in /system/bin enough? Mostly yes, but there are critical steps to make it work properly:

  1. Gain root access: Open your terminal emulator and run su (grant root permissions when prompted).
  2. Remount /system as read-write: By default, /system is read-only to protect system files. Run:
    mount -o rw,remount /system
    Note: Some newer devices use /system_root instead — check with mount | grep /system if this fails.
  3. Copy the binary:
    cp /sdcard/custom_ls /system/bin/ (adjust the source path if you compiled in Termux, e.g., cp ~/custom_ls /system/bin/)
  4. Set correct permissions: The binary needs to be executable by all users:
    chmod 755 /system/bin/custom_ls
  5. Remount /system back to read-only: Restore the device's default security state:
    mount -o ro,remount /system

Run Your Custom ls

Now, open any terminal emulator (rooted or not) and type custom_ls — it should run just like the built-in ls command!

Alternative: Run Without Modifying /system

If you don't want to alter the system directory, you can run the binary directly:

  • Navigate to its folder: cd /sdcard/
  • Execute it: ./custom_ls
    Or add its path to your PATH temporarily: export PATH=$PATH:/sdcard/, then just type custom_ls.

Troubleshooting Tips

  • "Exec format error": Your binary's architecture doesn't match your device's. Double-check the compiler toolchain you used.
  • "Permission denied": Ensure you set 755 permissions, and temporarily disable SELinux if needed: su -c setenforce 0 (note: this reduces security, so only use for testing).
  • Unexpected behavior: Android uses Bionic libc, which is mostly POSIX-compliant but has minor differences from glibc. Verify your code uses Android-compatible functions.

内容的提问来源于stack exchange,提问作者NoNoob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:28:22