How to pass a parameter to the Java code in run/debug configuration from Android Studio

后端 未结 4 2046
北海茫月
北海茫月 2021-02-04 12:47

My android app does some http requests to my server. However sometimes I am debugging the new api code that runs on my development machine. I would like to be able to pass somet

4条回答
  •  醉酒成梦
    2021-02-04 12:58

    Probably the simplest thing is to write the data you want to pass to a file on the device in /data/data; your Android app can read the device trivially (perhaps make it a .properties file and use java.util.Properties to read it in). To write it out, use this kind of task in your build.gradle file (and use the correct path to the adb command for your setup):

    task writeValue(type:Exec) {
        commandLine '/usr/local/bin/adb', 'shell', 'echo \'API_SERVER=http://10.0.2.2/myapp/\' > /data/data/values.properties'
    }
    

    There's documentation on Gradle exec tasks at http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html

    You can execute this task manually from Android Studio by using the Gradle tasks view:

    Screen shot of Gradle tasks view

提交回复
热议问题