Search code examples
androidadb

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?


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.


Solution

  • 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.