设置默认adb 调试

设置默认adb 调试

用adb调试android设备时,首次连接时,会出现一个授权提示:

error: device unauthorized. Please check the confirmation dialog on your device.

 

工作原理:

原来在我们的PC机(以windows为例)上启动了adb.exe进程时,adb会在本地生成一对密钥adbkey(私钥)与adbkey.pub(公钥);

根据弹框提示“The computer's RSA key fingerprint is:xxxx”,可以看出是一对RSA算法的密钥,其中公钥是用来发送给手机的;

当你执行“adb shell”时,adb.exe会将当前PC的公钥(或者公钥的hash值)(fingerprint)发送给android设备;这时,如果android上已经保存了这台PC的公钥,则匹配出对应的公钥进行认证,建立adb连接;如果android上没有保存这台PC的公钥,则会弹出提示框,让你确认是否允许这台机器进行adb连接,当你点击了允许授权之后,android就会保存了这台PC的adbkey.pub(公钥);

 

当然手机厂商也有可能会内置一些adbkey.pub(公钥);

 

那么问题来了,这些密钥在PC与Android上分别存储在哪里?

首先PC上,以Windows7为例,当你首次启动adb.exe时,会在C盘的当前用户的目录下生成一个".android"目录,其中adbkey与adbkey.pub就在这个目录下;(adb.exe会在启动时读取这两个文件(没有就重新生成),所以如果你要是删除或者修改了这两个文件之后,必须要关闭adb.exe进程,重启之后才能生效;)

其次Android上,PC的公钥被保存在一个文件中"/data/misc/adb/adb_keys";

 

在知道了adb这种认证的原理之后,你可以在不希望自己android设备授权任何PC设备进行adb链接时,清除"/data/misc/adb/adb_keys"文件;

也可以在没有屏幕的情况下,让已经认证过的PC将你PC上的adbkey.pub中的公钥导入到android中的"/data/misc/adb/adb_keys"文件中,或者将已经认证过的PC机上的adbkey与adbkey.pub拷贝到本机上覆盖你自己的adbkey与adbkey.pub,然后重启adb.exe,即可执行adb命令;

 

需求:在用户版的情况下使用usb 调试时,不需要弹出对话框进行手动允许操作,而自动默认允许操作。

修改方法:

方法一:

1、在/frameworks/base/packages/SystemUI/src/com/android/systemui/usb 该目录下修改 UsbDebuggingActivity.java

   
   
   
   
  1. privateclassUsbDisconnectedReceiverextendsBroadcastReceiver{
  2. privatefinalActivity mActivity;
  3. publicUsbDisconnectedReceiver(Activity activity){
  4. mActivity = activity;
  5. }
  6. @Override
  7. publicvoid onReceive(Context content,Intent intent){
  8. String action = intent.getAction();
  9. if(!UsbManager.ACTION_USB_STATE.equals(action)){
  10. return;
  11. }
  12. boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED,false);
  13. //boolean connected = false;//直接关闭对话框
  14. if(!connected){
  15. mActivity.finish();
  16. }
  17. /*
  18. //直接确认允许通过
  19. //allowUsbDebugging
  20. try {
  21. IBinder b = ServiceManager.getService(USB_SERVICE);
  22. IUsbManager service = IUsbManager.Stub.asInterface(b);
  23. service.allowUsbDebugging(true, mKey);
  24. } catch (Exception e) {
  25. Log.e(TAG, "Unable to notify Usb service", e);
  26. }
  27. */
  28. }
  29. }

2、在/build/core/main.mk

把 ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=0 

修改为

             ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1 

 

方法二:

直接关闭adb的认证机制(google adb secure)user版直接adb 调试模式

需要修改 /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
  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
  9. ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1
  10. ADDITIONAL_DEFAULT_PROPERTIES += ro.adb.secure=0
  11. endif # !enable_target_debugging

 

根据ro.debuggable = 1 or 0 来设置,1 就是开启adb, 0 即关闭adb debug.

关闭授权框提示:这个ro.adb.secure=0(0为不显示信任此电脑,1为显示信任此电脑)

去掉adb 密钥校验:ro.adb.secure=0 不显示对话框

 

而ro.adb.secure 这个system property 对于adbd 的控制点在/system/core/adb/adb.c 中的 

   
   
   
   
  1. property_get("ro.adb.secure", value,"0");
  2. auth_enabled =!strcmp(value,"1");
  3. if(auth_enabled)
  4. adb_auth_init();

 

话外:

修改ro.adb.secure默认值  

android4.4通过一个名为 ro.adb.secure 的常量来控制是否弹出adb debug调试授权的窗口 
在UsbDeviceManager.java构造函数中有这样一段代码:

   
   
   
   
  1. boolean secureAdbEnabled =SystemProperties.getBoolean("ro.adb.secure",false);
  2. boolean dataEncrypted ="1".equals(SystemProperties.get("vold.decrypt"));
  3. if(secureAdbEnabled &&!dataEncrypted){
  4. mDebuggingManager =newUsbDebuggingManager(context);
  5. }

可以看到通过ro.adb.secure这个key拿到的boolean值secureAdbEnabled如果是true并且通过vold.decrypt这个key拿到的boolean值dataEncrypted如果是false,那么就会实例化一个UsbDebuggingManager对象,而UsbDebuggingManager对象就是弹出adb debug授权窗口的一整套流程的入口,至于vold.decrypt常量暂时还没去研究。 


要注意的是通过ro.adb.secure这个key拿到的boolean值如果是false(有些源码平台默认是false的)那么就不会进入授权认证流程也就是不会弹出允许USB调试吗?的窗口直接就可以进行调试了而我这边的需求是要改变这个窗口的样式和按钮逻辑所以必须让它弹出来,如何改变ro.adb.secure的默认值网上也有很多文章: 


http://www.tuicool.com/articles/UBF7ji (开启与关闭adb 的认证机制),解决这个之后或者你开发的源码平台默认就是开启的那么当你插入usb调试线连接PC与android设备的时候就会弹出一个窗口,这个窗口的弹出逻辑就是在UsbDebuggingManager.java中。


ro.secure=1  

#赋值为1是开启安全策略

ro.adb.secure=1  

#赋值为1是开启adb的安全策略

ro.debuggable=0  

#赋值为0是关闭调试

service.adb.root=0  

#赋值为0是关闭adb root

 

user/userdebug/eng

Built Type

具体影响

eng

This is the default flavor. A plain "make" is the same as "make eng". droid is an alias for eng. Installs modules tagged with: eng, debug, user, and/or development. Installs non-APK modules that have no tags specified. Installs APKs according to the product definition files, in addition to tagged APKs. ro.secure=0 ro.debuggable=1 ro.kernel.android.checkjni=1 adb is enabled by default.

user

"make user" This is the flavor intended to be the final release bits. Installs modules tagged with user. Installs non-APK modules that have no tags specified. Installs APKs according to the product definition files; tags are ignored for APK modules. ro.secure=1 ro.debuggable=0adb is disabled by default.

userdebug

"make userdebug" The same as user, except: Also installs modules tagged with debug.ro.debuggable=1 adb is enabled by default.

  

默认即档ro.secure 为0 时,即开启root 权限,为1时再根据ro.debuggable 等选项来确认是否可以用开启root 权限。

将ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=1 改成 ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=0 即可。

 


 


你可能感兴趣的:(root)