Skip to content

Instantly share code, notes, and snippets.

@dominicbartl
Created September 30, 2014 16:30
Show Gist options
  • Save dominicbartl/3dd1f036ddea41f3c39f to your computer and use it in GitHub Desktop.
Save dominicbartl/3dd1f036ddea41f3c39f to your computer and use it in GitHub Desktop.

Revisions

  1. dominicbartl created this gist Sep 30, 2014.
    9 changes: 9 additions & 0 deletions ServerUtil.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    private static final String HOST_EMULATOR = "10.0.2.2";
    private static final String HOST_PRODUCTION = "example.com";

    public static String getHost() {
    if (BuildConfig.DEBUG) {
    return (Build.PRODUCT).contains("sdk") ? HOST_EMULATOR : BuildConfig.LOCAL_IP;
    }
    return HOST_PRODUCTION;
    }
    20 changes: 20 additions & 0 deletions build.gradle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    ...
    android {

    defaultConfig {
    minSdkVersion 14
    targetSdkVersion 19
    buildConfigField "String", "LOCAL_IP", '\"' + getLocalIp("eth0") + '\"'
    }
    }

    // Get the ip address by interface name
    def getLocalIp(String interfaceName) {
    NetworkInterface iface = NetworkInterface.getByName(interfaceName);
    for (InterfaceAddress address : iface.getInterfaceAddresses()) {
    String ip = address.getAddress().getHostAddress()
    if (ip.length() <= 15) {
    return ip;
    }
    }
    }