adb on target devices:在android 手机上、开发板上执行adb remount

一般情况, adb 程序只是运行在 ubuntu 或 windows 上,

你是否想过adb 命令能直接跑在目标设备-手机或者 Android 开发板上呢?

早期4.4 上好像是支持的这样运行adb的, 但是后来的版本又不支持了。

在此给出一个在android 9 版本上适配设备adb 的实现,主要编译的适配。

修改diff 附件

diff --git a/Android.mk b/Android.mk
index c473ed2..3b562bc 100644
--- a/Android.mk
+++ b/Android.mk
@@ -19,7 +19,7 @@ ADB_COMMON_CFLAGS := \
     -DADB_VERSION="\"$(tool_version)\"" \
 
 ADB_COMMON_posix_CFLAGS := \
-    -Wexit-time-destructors \
+    -Wno-exit-time-destructors \
     -Wthread-safety \
 
 ADB_COMMON_linux_CFLAGS := \
@@ -173,6 +173,7 @@ include $(BUILD_HOST_STATIC_LIBRARY)
 include $(CLEAR_VARS)
 LOCAL_MODULE := adbd_test
 LOCAL_CFLAGS := -DADB_HOST=0 $(LIBADB_CFLAGS)
+
 LOCAL_SRC_FILES := \
     $(LIBADB_TEST_SRCS) \
     $(LIBADB_TEST_linux_SRCS) \
@@ -380,6 +381,65 @@ LOCAL_STATIC_LIBRARIES := \
 
 include $(BUILD_EXECUTABLE)
 
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+    adb_client.cpp \
+    bugreport.cpp \
+    client/main.cpp \
+    console.cpp \
+    commandline.cpp \
+    file_sync_client.cpp \
+    line_printer.cpp \
+    services.cpp \
+    shell_service_protocol.cpp \
+    adb.cpp \
+    adb_io.cpp \
+    adb_listeners.cpp \
+    adb_trace.cpp \
+    adb_utils.cpp \
+    fdevent.cpp \
+    sockets.cpp \
+    socket_spec.cpp \
+    sysdeps/errno.cpp \
+    transport.cpp \
+    transport_local.cpp \
+    client/usb_libusb.cpp \
+    client/usb_dispatch.cpp \
+    transport_usb.cpp diagnose_usb_device.cpp \
+    adb_auth_host.cpp \
+    transport_mdns.cpp \
+    sysdeps_unix.cpp \
+    sysdeps/posix/network.cpp \
+    client/usb_linux.cpp \
+
+$(warning ------------LOCAL_SRC_FILES-------${LOCAL_SRC_FILES})
+
+LOCAL_CFLAGS += \
+    -frtti \
+    -Wall -Wextra -Werror \
+    -Wno-unused-parameter \
+    -Wno-exit-time-destructors \
+    -Wno-missing-field-initializers \
+    -Wvla \
+    -DADB_VERSION="\"$(tool_version)\"" \
+    -D_GNU_SOURCE \
+    -DADB_HOST=1 \
+    -DADB_HOST_ON_TARGET=1 \
+    $(ADB_COMMON_CFLAGS) \
+    -fvisibility=hidden \
+
+LOCAL_MODULE := adb
+LOCAL_MODULE_TAGS := debug
+
+LOCAL_STATIC_LIBRARIES := \
+    libmdnssd libusb 
+
+$(warning -------------LOCAL_STATIC_LIBRARIES-------${LOCAL_STATIC_LIBRARIES})
+$(warning -------------DEFAULT_ADB_PORT------${DEFAULT_ADB_PORT})
+LOCAL_SHARED_LIBRARIES := libbase libcutils liblog libcrypto libcrypto_utils
+
+include $(BUILD_EXECUTABLE)
 # adb integration test
 # =========================================================
 $(call dist-for-goals,sdk,$(ALL_MODULES.adb_integration_test_adb.BUILT))
diff --git a/adb_utils.cpp b/adb_utils.cpp
old mode 100644
new mode 100755
index b236fb3..5512763
--- a/adb_utils.cpp
+++ b/adb_utils.cpp
@@ -241,6 +241,9 @@ std::string adb_get_homedir_path() {
         return {};
     }
     return home_str;
+#elif defined(__ANDROID__)
+    D("---android return /data/user/0");
+    return "/data/user/0";
 #else
     if (const char* const home = getenv("HOME")) {
         return home;
diff --git a/bugreport.h b/bugreport.h
old mode 100644
new mode 100755
index 413439b..5288052
--- a/bugreport.h
+++ b/bugreport.h
@@ -31,6 +31,8 @@ class Bugreport {
     }
     int DoIt(int argc, const char** argv);
 
+    virtual ~Bugreport() = default;  // ̭¼Ӑ鎶¹¹º¯ʽ
+
   protected:
     // Functions below are abstractions of external functions so they can be
     // mocked on tests.
diff --git a/client/usb_linux.cpp b/client/usb_linux.cpp
old mode 100644
new mode 100755
index 1f376a4..c59ad21
--- a/client/usb_linux.cpp
+++ b/client/usb_linux.cpp
@@ -33,6 +33,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <sys/sysmacros.h> 
 #include <chrono>
 #include <condition_variable>
 #include <list>
diff --git a/commandline.h b/commandline.h
old mode 100644
new mode 100755
index 36cd798..8c3f2e1
--- a/commandline.h
+++ b/commandline.h
@@ -26,6 +26,8 @@ class StandardStreamsCallbackInterface {
   public:
     StandardStreamsCallbackInterface() {
     }
+    virtual ~StandardStreamsCallbackInterface() = default;  // ̭¼Ӑ鎶¹¹º¯ʽ
+
     // Handles the stdout output from devices supporting the Shell protocol.
     virtual void OnStdout(const char* buffer, int length) = 0;
 
@@ -64,6 +66,8 @@ class DefaultStandardStreamsCallback : public StandardStreamsCallbackInterface {
         : stdout_str_(stdout_str), stderr_str_(stderr_str) {
     }
 
+    virtual ~DefaultStandardStreamsCallback() override = default;  // ̭¼Ӑ鎶¹¹º¯ʽ
+
     void OnStdout(const char* buffer, int length) {
         OnStream(stdout_str_, stdout, buffer, length);
     }
diff --git a/diagnose_usb_device.cpp b/diagnose_usb_device.cpp
new file mode 100644
index 0000000..ed42c25
--- /dev/null
+++ b/diagnose_usb_device.cpp
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "diagnose_usb.h"
+
+#include <errno.h>
+#include <unistd.h>
+
+#include <string>
+
+#include <android-base/stringprintf.h>
+
+#if defined(__linux__)
+#include <grp.h>
+#include <pwd.h>
+#endif
+
+static const char kPermissionsHelpUrl[] = "http://developer.android.com/tools/device.html";
+
+// Returns a message describing any potential problems we find with udev, or an empty string if we
+// can't find plugdev information (i.e. udev is not installed).
+static std::string GetUdevProblem() {
+    return "";
+}
+
+// Short help text must be a single line, and will look something like:
+//
+//   no permissions (reason); see [URL]
+std::string UsbNoPermissionsShortHelpText() {
+    std::string help_text = "no permissions";
+
+    std::string problem(GetUdevProblem());
+    if (!problem.empty()) help_text += " (" + problem + ")";
+
+    return android::base::StringPrintf("%s; see [%s]", help_text.c_str(), kPermissionsHelpUrl);
+}
+
+// Long help text can span multiple lines but doesn't currently provide more detailed information:
+//
+//   insufficient permissions for device: reason
+//   See [URL] for more information
+std::string UsbNoPermissionsLongHelpText() {
+    std::string header = "insufficient permissions for device";
+
+    std::string problem(GetUdevProblem());
+    if (!problem.empty()) header += ": " + problem;
+
+    return android::base::StringPrintf("%s\nSee [%s] for more information", header.c_str(),
+                                       kPermissionsHelpUrl);
+}

遇到的错误:

adb devices
List of devices attached
* daemon not running; starting now at tcp:5038
ADB server didn't ACK
Full server startup log: /tmp/adb.0.log
Server had pid: 2887
--- adb starting (pid 2887) ---
adb I 06-08 17:38:59  2887  2887 main.cpp:56] Android Debug Bridge version 1.0.40
adb I 06-08 17:38:59  2887  2887 main.cpp:56] Version 28.0.0 rc1-eng.pqz.20250608.155900
adb I 06-08 17:38:59  2887  2887 main.cpp:56] Installed as /system/bin/adb
adb I 06-08 17:38:59  2887  2887 main.cpp:56]
adb I 06-08 17:38:59  2887  2887 adb_auth_host.cpp:421] adb_auth_init...
adb F 06-08 17:38:59  2887  2887 adb_utils.cpp:269] Cannot mkdir '//.android': Read-only file system

* failed to start daemon
error: cannot connect to daemon

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值