How can I use ADB to store a String value on an Android device before the app is installed to be consumed on first launch?

huangapple go评论139阅读模式
英文:

How can I use ADB to store a String value on an Android device before the app is installed to be consumed on first launch?

问题

要求:我需要使用adb将一个字符串值存储在Android设备上的某个位置,以便在首次启动我的应用程序时被应用程序使用。当存储字符串值时,应用程序尚未安装。以下要求适用:

  • 它不应要求任何需要用户交互授予的运行时权限(adb授权是可以的)
  • 它必须在Android 12及以上版本上工作
  • 字符串值必须能够独立于APK更改,因此不能捆绑在APK中
  • 不能通过Instrumentation args传递(尽管这是用于Android测试)

尝试的方法:

  • 将文件存储在预定义位置,例如/sdcard/Downloads。由于Android 11及以上版本的权限模型限制,这种方法不起作用
  • 将文件存储在应用程序本地文件夹中,例如/sdcard/Android/data/com.my.app/files/。由于某些(但不是所有)设备在安装时会清除该文件夹,因此这种方法不起作用

编辑:我需要这个功能用于特定的CI/CD配置场景。这不是我会发布到公众的东西。

英文:

Requirements: I need to use adb to store a string value somewhere on an Android device that later is to be consumed by the app I'm programming upon first launch. The app is not installed when the String value is to be stored. The following requirements apply:

  • It must not require any runtime permissions that require user interaction to grant (adb grant is OK)
  • It must work on Android 12 and above
  • The String value must be able to change independently of the APK so it cannot be bundled
  • It cannot be passed though Instrumentation args (although this is for Android tests)

Methods attempted:

  • Store a file on a predefined location, like /sdcard/Downloads. Does not work because of tightened permission model from Android 11 and above
  • Store a file in the app local folder, like /sdcard/Android/data/com.my.app/files/. Does not work because the folder is cleared on install on some (not all) devices

edit: I need this for a specific CI/CD config scenario. This is not something I will ship into the wild.

答案1

得分: 1

最接近您要求的解决方案是使用自定义系统属性。

  • 仅可通过 adb 写入值
  • 无需 root
  • 自定义属性需要以 'debug.' 前缀,因此 adb shell setprop debug.foobar 'customString' 可以工作,而 adb shell setprop foobar 'customString' 无法工作

不确定您为何没有找到 如何在Android测试仪测试中定义和使用系统属性?

但是,此答案 在经过测试后在三星Galaxy S9 Android 10和Pixel 6a Android 13上可行。

请注意,在设置/取消自定义系统属性值以及在受测试的应用程序中检查这些属性值时要谨慎,即您可能需要终止受测试的应用程序,以便更新的值被采用。

正如@Robert和@DiegoTorresMilano在评论中提到的,ContentProvider/ContentResolver 可能是另一种解决方案。另一个持久性应用程序可以向受测试的应用程序提供配置测试值。这将是在无法使用 '开发者模式' 或通过 adb 自定义命令造成负担的环境中的解决方案。

英文:

The closest solution to your requirements is to use a custom system property.

  • value only writable via adb
  • no root required
  • custom property requires 'debug.' prefix, so adb shell setprop debug.foobar 'customString' works while adb shell setprop foobar 'customString' won't work

Not sure how you didn't find How to define and use a system property in Android Instrumentation test?

but this answer works as tested on a Samsung Galaxy S9 Android 10 and a Pixel 6a Android 13.

Just be aware of when you are setting/unsetting custom system property values and when those are checked in the app under test, i.e. you may need to kill the app under test so that updated values are picked up.

As @Robert and @DiegoTorresMilano mentioned in the comments a ContentProvider/ContentResolver would be another solution. A second persistent app which would provide configuration test values to the app under test. That would be a solution for environments where 'developer mode' is unavailable OR where custom commands via adb impose a burden.

huangapple
  • 本文由 发表于 2023年8月11日 00:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定