1. 程式人生 > >如何預設開啟user版本 debug 選項, 預設開啟adb 連線

如何預設開啟user版本 debug 選項, 預設開啟adb 連線

如何預設開啟user 版本的USB debug 選項, 預設開啟adb 連線

[Keyword]

量產版本 user usb debug root adb 連線

[Solution]
1. 在android 4.0 之前,這個設定是在frameworks/base/service/..../SystemServer.java 裡面
設定會根據system property 的persist.service.adb.enable 來設定。您可以看到類似如程式碼:
  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),false
    new AdbSettingsObserver());  


而這個persist.service.adb.enable 預設是放在在default.prop 中,在編譯的時候在
build/core/main.mk 中確認,
  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 中,您也可以
看到類似的程式碼如:
  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. publicvoid  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

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

2. 確定預設的usb功能

build/tools/post_process_props.py

  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 fordefault
  6.   if prop.get("ro.mmitest") == "true":  
  7.     prop.put("persist.sys.usb.config""mass_storage")  
  8.   # If ro.debuggable is 1, then enable adb on USB by default
  9.   # (this is for userdebug builds)  
  10.   if prop.get("ro.build.type") == "eng":  
  11.     val = prop.get("persist.sys.usb.config").strip('\r\n')  
  12.     if val == "":  
  13.       val = "adb"
  14.     else:  
  15.       val = val + ",adb"
  16.     prop.put("persist.sys.usb.config", val)  
  17.   # UsbDeviceManager expects a value here.  If it doesn't get it, it will  
  18.   # default to "adb". That might not the right policy there, but it's better  
  19.   # to be explicit.  
  20.   if not prop.get("persist.sys.usb.config"):  
  21.     prop.put("persist.sys.usb.config""none");  
  22. # Put the modifications that you need to make into the /system/build.prop into this
  23. # function. The prop object has get(name) and put(name,value) methods.  
  24. def mangle_default_prop(prop):  
  25.   # If ro.debuggable is 1, then enable adb on USB by default
  26.   # (this is for userdebug builds)  
  27.   if prop.get("ro.debuggable") == "1":  
  28.     val = prop.get("persist.sys.usb.config")  
  29.     if val == "":  
  30.       val = "adb"