[FAQ04776]各個版本如何判斷是否打開adb端口【google原生代碼】

1. 在android 4.0 之前,這個設置是在frameworks/base/service/..../SystemServer.java 裏面
設置會根據system property 的persist.service.adb.enable 來設置。您可以看到類似如代碼:

[java] view plain copy
  1. // make sure the ADB_ENABLED setting value matches the secure property value  
  2. Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,  
  3.         "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);  
  4. // register observer to listen for settings changes  
  5. mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secu  
  6. ENABLED),falsenew AdbSettingsObserver());  
而這個persist.service.adb.enable 默認是放在在default.prop 中,在編譯的時候在
build/core/main.mk 中確認,

[java] view plain copy
  1. ifeq (true,$(strip $(enable_target_debugging)))  
  2.   # Target is more debuggable and adbd is on by default  
  3.   ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1 persist.service.adb.enable=1  
  4.   # Include the debugging/testing OTA keys in this build.  
  5.   INCLUDE_TEST_OTA_KEYS := true  
  6. else # !enable_target_debugging  
  7.   # Target is less debuggable and adbd is off by default  
  8.   ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=0 persist.service.adb.enable=0  
  9. endif # !enable_target_debugging  
您需要將: ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=0
persist.service.adb.enable=0  改成

ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1 persist.service.adb.enable=1

2. 在android 4.0 之後,因爲adb 的控制,統一使用了persist.sys.usb.config 來控制,於是對

應的設置點也改到了frameworks/base/service/...../usb/UsbDeviceManager.java 中,您也可以

看到類似的代碼如:
[java] view plain copy
  1. public  UsbHandler(Looper looper) {  
  2.        // persist.sys.usb.config should never be unset.  But if it is, set it to "adb"  
  3.        // so we have a chance of debugging what happened.  
  4.         mDefaultFunctions = SystemProperties.get("persist.sys.usb.config""adb");  
  5.        // sanity check the sys.usb.config system property  
  6.        // this may be necessary if we crashed while switching USB configurations  
  7.        String config = SystemProperties.get("sys.usb.config""none");  
  8.        if (!config.equals(mDefaultFunctions)) {  
  9.            Slog.w(TAG, "resetting config to persistent property: " + mDefaultFunctions);  
  10.            SystemProperties.set("sys.usb.config", mDefaultFunctions);  
  11.        }  
  12.        mCurrentFunctions = mDefaultFunctions;  
  13.        String state = FileUtils.readTextFile(new File(STATE_PATH), 0null).trim();  
  14.        updateState(state);  
  15.        mAdbEnabled = containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ADB);  
  16. public void  systemReady() {  
  17. // make sure the ADB_ENABLED setting value matches the current state  
  18.    Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED, mAdbEnabled ?  
  19. 1 : 0);  
而這個persist.sys.usb.config 中adb 的配置是在alps/build/tools/post_process_props.py 中
根據ro.debuggable = 1 or 0 來設置,1 就是開啓adb, 0 即關閉adb debug. 而這個
ro.debuggable 也是在alps/build/core/main.mk 中設置,和2.3 修改類似
不過您這樣打開之後,對於user 版本adb shell 開啓的還是shell 權限,而不是root 權限,如果
您需要root 權限,需要再改一下system/core/adb/adb.c 裏面的should_drop_privileges() 這個

函數,在#ifndef ALLOW_ADBD_ROOT 時return 0; 而不是return 1; 即可。

---------------------------------------------------------------------------------------

1). 根據編譯命令確定ro.debuggable

build/core/main.mk

[java] view plain copy
  1. ## user/userdebug ##  
  2.   
  3. user_variant := $(filter user userdebug,$(TARGET_BUILD_VARIANT))  
  4. enable_target_debugging := true  
  5. tags_to_install :=  
  6. ifneq (,$(user_variant))  
  7.   # Target is secure in user builds.  
  8.   ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=1  
  9.   
  10.   ifeq ($(user_variant),userdebug)  
  11.     # Pick up some extra useful tools  
  12.     tags_to_install += debug  
  13.   
  14.     # Enable Dalvik lock contention logging for userdebug builds.  
  15.     ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.lockprof.threshold=500  
  16.   else  
  17.     # Disable debugging in plain user builds.  
  18.     enable_target_debugging :=  
  19.   endif  
  20.   
  21.   # enable dex pre-optimization for all TARGET projects in default to   
  22.   # speed up device first boot-up  
  23.   #add by yanqi.liu for costomization @{  
  24.   ifneq ($(JRD_IS_GOAL_PERSO),true)  
  25.      WITH_DEXPREOPT := true  
  26.   endif  
  27. #}  
  28.   # Turn on Dalvik preoptimization for user builds, but only if not  
  29.   # explicitly disabled and the build is running on Linux (since host  
  30.   # Dalvik isn't built for non-Linux hosts).  
  31.   ifneq (true,$(DISABLE_DEXPREOPT))  
  32.     ifeq ($(user_variant),user)  
  33.       ifeq ($(HOST_OS),linux)  
  34.         ifneq ($(JRD_IS_GOAL_PERSO),true)  
  35.            WITH_DEXPREOPT := true  
  36.         endif  
  37.       endif  
  38.     endif  
  39.   endif  
  40.   
  41.   # Disallow mock locations by default for user builds  
  42.   ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=0  
  43.   
  44. else # !user_variant  
  45.   # Turn on checkjni for non-user builds.  
  46.   # Kirby: turn off it temporarily to gain performance {  
  47.   # ADDITIONAL_BUILD_PROPERTIES += ro.kernel.android.checkjni=1  
  48. #  ADDITIONAL_BUILD_PROPERTIES += ro.kernel.android.checkjni=0  
  49.   # } Kirby  
  50.   # Set device insecure for non-user builds.  
  51.   ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=0  
  52.   # Allow mock locations by default for non user builds  
  53.   ADDITIONAL_DEFAULT_PROPERTIES += ro.allow.mock.location=1  
  54. endif # !user_variant  
  55.   
  56.   
  57. # always enable aed  
  58. ADDITIONAL_DEFAULT_PROPERTIES += persist.mtk.aee.aed=on  
  59.   
  60. # Turn on Jazz AOT by default if not explicitly disabled and the build  
  61. # is running on Linux (since host Dalvik isn't built for non-Linux hosts).  
  62. ifneq (true,$(DISABLE_JAZZ))  
  63.   ifeq ($(strip $(MTK_JAZZ)),yes)  
  64.     ifeq ($(HOST_OS),linux)  
  65.       # Build host dalvikvm which Jazz AOT relies on.  
  66.       WITH_HOST_DALVIK := true  
  67.       WITH_JAZZ := true  
  68.     endif  
  69.   endif  
  70. endif  
  71.   
  72. ifeq (true,$(strip $(enable_target_debugging)))  
  73.   # Target is more debuggable and adbd is on by default  
  74.   ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1  
  75.   ADDITIONAL_DEFAULT_PROPERTIES += ro.adb.secure=0  
  76.   # Include the debugging/testing OTA keys in this build.  
  77.   INCLUDE_TEST_OTA_KEYS := true  
  78. else # !enable_target_debugging  
  79.   # Target is less debuggable and adbd is off by default  
  80.   ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=0  
  81.   ADDITIONAL_DEFAULT_PROPERTIES += ro.adb.secure=1  
  82. endif # !enable_target_debugging  

2. 確定默認的usb功能

build/tools/post_process_props.py

[java] view plain copy

  1. # Put the modifications that you need to make into the /system/build.prop into this  
  2. # function. The prop object has get(name) and put(name,value) methods.  
  3. def mangle_build_prop(prop):  
  4.   #pass  
  5.   #If ro.mmitest is true, then disable MTP, add mass_storage for default  
  6.   if prop.get("ro.mmitest") == "true":  
  7.     prop.put("persist.sys.usb.config""mass_storage")  
  8.   
  9.   # If ro.debuggable is 1, then enable adb on USB by default  
  10.   # (this is for userdebug builds)  
  11.   if prop.get("ro.build.type") == "eng":  
  12.     val = prop.get("persist.sys.usb.config").strip('\r\n')  
  13.     if val == "":  
  14.       val = "adb"  
  15.     else:  
  16.       val = val + ",adb"  
  17.     prop.put("persist.sys.usb.config", val)  
  18.   # UsbDeviceManager expects a value here.  If it doesn't get it, it will  
  19.   # default to "adb". That might not the right policy there, but it's better  
  20.   # to be explicit.  
  21.   if not prop.get("persist.sys.usb.config"):  
  22.     prop.put("persist.sys.usb.config""none");  
  23.   
  24.   
  25. # Put the modifications that you need to make into the /system/build.prop into this  
  26. # function. The prop object has get(name) and put(name,value) methods.  
  27. def mangle_default_prop(prop):  
  28.   # If ro.debuggable is 1, then enable adb on USB by default  
  29.   # (this is for userdebug builds)  
  30.   if prop.get("ro.debuggable") == "1":  
  31.     val = prop.get("persist.sys.usb.config")  
  32.     if val == "":  
  33.       val = "adb"  
  34.     else:  
  35.       val = val + ",adb"  
  36.     prop.put("persist.sys.usb.config", val)  
  37.   # UsbDeviceManager expects a value here.  If it doesn't get it, it will  
  38.   # default to "adb". That might not the right policy there, but it's better  
  39.   # to be explicit.  
  40.   if not prop.get("persist.sys.usb.config"):  
  41.     prop.put("persist.sys.usb.config""none");  
發佈了16 篇原創文章 · 獲贊 21 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章