Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically set local IP Address in build.gradle

I'm devolping an android app that heavily uses Rest Services.
For developing and debugging, I run the Rest server locally (on my notebook). At home, I have static IP Adresses and therefore, I can put a static String in my build.gradle.
But if I work from somewhere else, I always have to check my notebook's ip address and edit my build.gradle.

Now I'm curious: Is there a way to insert the current local IP address into my build.gradle automatically?

android {
    ...

    buildTypes {
        debug {
            ...
            resValue "string", "host_name", "192.168.0.102" // <--- should be set automatically
        }
        release {
            ...
            resValue "string", "host_name", "example.com/rest/"
        }
}
like image 372
Jörn Buitink Avatar asked Oct 30 '25 00:10

Jörn Buitink


1 Answers

You can use Groovy methods to find the local IP address:

resValue "string", "host_name", InetAddress.localHost.canonicalHostName

Alternatively you also could use hostAddress instead of canonicalHostName.

like image 174
Floern Avatar answered Oct 31 '25 15:10

Floern