编译adb到Android的失败历程
zsx2016/4/11 in 挖坑待跳 / 6 / 4534

2018 Update: https://github.com/Magisk-Modules-Repo/adb-ndk
Marshmallow removed adb from the /system/bin directory. For those wanting this functionality back, here’s a simple installer to push static compiles of adb and fastboot to /system/xbin and uses a command wrapper to point adb to the correct HOME directory and set TMPDIR for compatibility. Detects and supports “systemless” install via SuperSU/Magisk as well. It can then be used from Terminal while booted from that point on.

好了,后面不用看了。

=======================

此文章未完工,纯属记录挖坑试错而已……求大牛指点_(:з」∠)_


嗯,Google在Android 6.0里移除了device-on-target的adb(Compile, for android 6.0, adb for “device-on-target”)于是需要自行编译了。作为一个基本没搞过这档事的人,我内心是崩溃的。

先试试用Android NDK编译。先

<pre class="prism-highlight prism-language-bash">git clone https://github.com/android/platform_system_core/把相关内容拿下来,ndk走起

<pre class="prism-highlight prism-language-bash">[email protected] /cygdrive/e/adb/platform_system_core-master/adb

$ /cygdrive/d/Android/AndroidNDK/android-ndk-r10e/ndk-build NDK_APPLICATION_MK=./Android.mk

/cygdrive/d/Android/AndroidNDK/android-ndk-r10e/build/core/add-application.mk:49: Android.mk: No such file or directory

jni/Android.mk:139: /cygdrive/d/Android/AndroidNDK/android-ndk-r10e/build/core/build-host-static-library.mk: No such file or directory

jni/Android.mk:169: /cygdrive/d/Android/AndroidNDK/android-ndk-r10e/build/core/build-host-static-library.mk: No such file or directory

jni/Android.mk:289: /cygdrive/d/Android/AndroidNDK/android-ndk-r10e/build/core/build-host-executable.mk: No such file or directory

Android NDK: Trying to define local module 'adbd' in jni/Android.mk.

Android NDK: But this module was already defined by jni/Android.mk.

/cygdrive/d/Android/AndroidNDK/android-ndk-r10e/build/core/build-module.mk:34: *** Android NDK: Aborting.    .  Stop.

很棒,搜索,得Android NDK build-host-executable.mk: No such file or directory。尝试使用patched NDK(https://github.com/flyskywhy/android-ndk-host)。得有东西没定义。

<pre class="prism-highlight prism-language-bash">[email protected] /cygdrive/e/adb/platform_system_core-master/adb

$ /cygdrive/d/Android/AndroidNDK/android-ndk-r10e/ndk-build

Android NDK:  Required variable TOOLCHAIN_ABIS is not defined by /cygdrive/d/Android/AndroidNDK/android-ndk-r10e/toolchains/llvm-3.5/config.mk

/cygdrive/d/Android/AndroidNDK/android-ndk-r10e/build/core/add-toolchain.mk:38: *** Android NDK: Aborting    .  Stop.

懒得再去对比r10b和r10e的区别了,换方案。直接按照编译到ARM的方法来搞。

以adb-arm项目为基础(https://github.com/bonnyfone/adb-arm),其adb-arm-binary是不能直接使用的(no such file or directory)。直接按其方法编译,当然也是不能用的。只要用file对比一下就知道原因了:

<pre class="prism-highlight prism-language-bash">[email protected]:/system/bin # file audiod

audiod: ELF 32-bit arm LSB shared object, dynamic (/system/bin/linker), needs 1 lib

[email protected]:/system/bin # file adb

adb: ELF 32-bit arm LSB executable, dynamic (/lib/ld-linux.so.3), needs 1 lib

动态链接库的连接器路径不对,改。加入参数

<pre class="prism-highlight prism-language-bash">-Wl,--dynamic-linker=/system/bin/linker```

<pre class="prism-highlight prism-language-bash">root@bacon:/system/bin # file adb

adb: ELF 32-bit arm LSB executable, dynamic (/system/bin/linker), needs 1 lib
但还是跑不起来,提示
<pre class="prism-highlight prism-language-bash">root@bacon:/ # adb version

error: only position independent executables (PIE) are supported.
搜索,得文章:<http://stackoverflow.com/questions/24818902/running-a-native-library-on-android-l-error-only-position-independent-executab>,改。 将Makefile的相关参数改成:
<pre class="prism-highlight prism-language-bash">CFLAGS+= -O2 -g -Wall -Wno-unused-parameter -pie -fPIE

LDFLAGS += -pie -fPIE
再编译,提示:
<pre class="prism-highlight prism-language-bash">root@bacon:/ # adb

CANNOT LINK EXECUTABLE: library “libcrypto.so.1.0.0” not found

page record for 0xb6ea902c was not found (block_size=16)
看来是动态链接库的问题了。Android似乎对带版本的动态链接库不太友好。想要静态链接,但发现压根没有这些源文件,GitHub搜索到的几个.a也不能用,先放弃,选择从toolchain的lib下拷入相关so。
<pre class="prism-highlight prism-language-bash">adb push ‘/opt/poky/1.5/sysroots/armv7a-vfp-neon-poky-linux-gnueabi/lib/libpthread.so.0’ /system/lib

adb push ‘/opt/poky/1.5/sysroots/armv7a-vfp-neon-poky-linux-gnueabi/lib/librt.so.1’ /system/lib

adb push ‘/opt/poky/1.5/sysroots/armv7a-vfp-neon-poky-linux-gnueabi/lib/libc.so.6’ /system/lib

adb push ‘/opt/poky/1.5/sysroots/armv7a-vfp-neon-poky-linux-gnueabi/lib/ld-linux.so.3’ /system/lib

adb push ‘/opt/poky/1.5/sysroots/armv7a-vfp-neon-poky-linux-gnueabi/lib/libcrypto.so.1.0.0’ /system/lib

adb push ‘/opt/poky/1.5/sysroots/armv7a-vfp-neon-poky-linux-gnueabi/lib/libdl.so.2’ /system/lib
拷入了以下so,还在挂:
<pre class="prism-highlight prism-language-bash">WARNING: linker: /system/lib/libpthread.so.0: unsupported flags DT_FLAGS_1=0x28

CANNOT LINK EXECUTABLE: unknown reloc type 19 @ 0xb6db1a39 (59)

page record for 0xb6f3602c was not found (block_size=16)
`

嗯,你开心就好了。不就是libpthread都在libc里吗,不就是这些都没法link吗(笑)

那就是说只有从AOSP直接编译才可以了是吗(笑)

也就是说,先留篇失败记录吧……来日再战。

如果本文对你有帮助,你可以用支付宝支持一下:

Alipay QrCode
taotao at 2019/1/2[回复]
即使把库考进/system/lib/ 和 /usr/lib 也不行
我的是报
CANNOT LINK EXECUTABLE: library "libgnustl_shared.so" not found
page record for 0x7f9eb18010 was not found (block_size=16)
zsx at 2019/1/3[回复]
我本来想说用NDK来编译ADB的,搜了一下,已经有prebuilt了:
https://github.com/Magisk-Modules-Repo/adb-ndk
ShunYea at 2016/5/18[回复]
额,来玩Android了啊?那个,你的WordPress转ZblogPHP的插件还等你更新啊。
js_ at 2016/4/19[回复]
为什么有这种需求==
zsx at 2016/4/23[回复]
因为我手表只能adb wifi然后附近没电脑