如何编译 Android 的 mdnsresponder

How to compile Android's mdnsresponder

我想更改我的 Android phone 从 Android 广播的主机名。从this answer,我发现我必须编辑https://android.googlesource.com/platform/external/mdnsresponder的源代码。我做到了,现在我该如何编译它? make 在根目录和 mDNSPosix 中都不起作用。

编辑:我试过 soong_build -b test -l Android.bp -o Android.ninja Android.bp 但它失败了 panic: Found two Blueprint files in directory

好的,我知道了,但是有点复杂(我可能会遗漏一些步骤):

所有步骤都可以 运行 在任何支持 Android NDK 的地方

  1. 克隆https://android.googlesource.com/platform/external/mdnsresponder
  2. cd进去。
  3. mkdir include 并下载 https://android.googlesource.com/platform/system/core/+/master/libcutils 进去。
  4. 编辑mDNSPosix/mDNSPosix.c
#ifndef __ANDROID__
       gethostname((char *)(&namelabel->c[1]), MAX_DOMAIN_LABEL);
   #else
       // use an appropriate default label rather than the linux default of 'localhost'
       strncpy(&namelabel->c[1], "Android", MAX_DOMAIN_LABEL);

#ifndef __ANDROID__
    gethostname((char *)(&namelabel->c[1]), MAX_DOMAIN_LABEL);
#else
    const char* hostname = getenv("HOSTNAME");
    // use an appropriate default label rather than the linux default of 'localhost'
    strncpy(&namelabel->c[1], hostname, MAX_DOMAIN_LABEL);
  1. 编辑mDNSPosix/PosixDaemon.c并删除
if (mStatus_NoError == err)
        {
        const struct passwd *pw = getpwnam(MDNS_USERNAME);
        if (pw != NULL)
            setuid(pw->pw_uid);
        else
            LogMsg("WARNING: mdnsd continuing as root because user \"%s\" does not exist", MDNS_USERNAME);
        }
  1. 下载 Android NDK here 并解压。该位置将被称为 $NDK.
  2. 复制/system/lib64/libcutils.so$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/31/libcutils.so
  3. 然后,执行这个:
$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/clang-12 --target=aarch64-linux-android31 -O2  -g  -fno-strict-aliasing  -fwrapv  -D_GNU_SOURCE  -DHAVE_IPV6  -DNOT_HAVE_SA_LEN  -DPLATFORM_NO_RLIMIT  -DMDNS_DEBUGMSGS=0  -DMDNS_UDS_SERVERPATH='"/dev/socket/mdnsd"' -DMDNS_USERNAME='"mdnsr"'  -W  -Wall  -Wextra  -Wno-address-of-packed-member  -Wno-array-bounds  -Wno-pointer-sign  -Wno-unused  -Wno-unused-const-variable  -Wno-unused-parameter  -Werror=implicit-function-declaration -DTARGET_OS_LINUX -DHAVE_LINUX -DUSES_NETLINK -Werror -I./mDNSCore        -I./mDNSPosix        -I./mDNSShared mDNSCore/DNSCommon.c        -I./include -DMDNS_VERSIONSTR_NODTS=1 -lcutils mDNSCore/DNSDigest.c        -llog mDNSCore/mDNS.c        mDNSCore/uDNS.c        mDNSPosix/mDNSPosix.c        mDNSPosix/mDNSUNP.c        mDNSPosix/PosixDaemon.c        mDNSShared/dnssd_ipc.c        mDNSShared/GenLinkedList.c        mDNSShared/mDNSDebug.c        mDNSShared/PlatformCommon.c        mDNSShared/uds_daemon.c 
  1. mdnsd./a.out
  2. 执行HOSTNAME=<whatever you want> ./a.out