7

During app development, I want the app to connect back to the machine it was built on (over Wifi).

As we have multiple developers, this will be a different IP address for each developer.

How can I do this?

(Some developers use AndroidStudio on Windows 7, other AndroidStudio on Ubuntu 14.04LTS.)

0

3 Answers 3

7

This is my solution. Really useful to keep a clean git status.

/**
 * Find the first LAN address of the development machine.
 */
def getDevHost = { ->
    def defaultDevHost = InetAddress.getLocalHost()
            .getCanonicalHostName()

    return NetworkInterface.getNetworkInterfaces()
            .findResult(defaultDevHost) {
        def ifOk = it.isUp() &&
                !it.isVirtual() &&
                !it.isPointToPoint() &&
                !it.isLoopback() &&
                !it.getName().startsWith("br-") // 

        if (ifOk)
            return it.getInetAddresses().find {
                it instanceof Inet4Address &&
                        !it.isLoopbackAddress()
            }
        else
            return null
    }
}

I'm using it for a single buildType of the project:

android {
  // ...

  buildTypes {
    // ...

    dev {
      // ...
      buildConfigField "String", "BACKEND_URL", "\"http://${getDevHost()}:9000/\""
    }
  }
}
2
  • Awesome. I then did subprojects { afterEvaluate { Project project -> android.buildTypes.all { /*Define the devlopment machines ip as constant (e.g. for instrumentation tests)*/ buildConfigField "String", "DEV_HOST_IP", "\""+getDevHost() + "\"" } } } and now I have the IP available at BuildConfig.DEV_HOST_IP
    – PhilLab
    Jan 15, 2018 at 9:07
  • I edited my answer to show an example usage for the most common use case. Apr 5, 2018 at 16:26
3

I would look into using Gradle to customize your build, for example by having the host machine's IP added as a constant in your BuildConfig.java file that is generated during build.

This page describes a very similar case that you might be able to use: Inject dynamic host IP address with Gradle

0

I guess these things should be in place

  1. Your workstations should be assigned a static ip addresses in order for the app to connect to it.

  2. For writing a code for such setup can be found here. I gave this answer long ago.

Read here

For Ubuntu

  • Use a make file and use commands as mentioned in the link above to fetch the computer ip.
  • Append the IP into a file , read from that file in the app and use it to connect to the build machine later.

I hope it helps.

11
  • Thanks, but I think the linked page is getting the mobile's IP from the PC. I need to insert the PC's IP address into the App during the build process. It is the App that connects to the PC, not vice versa.
    – fadedbee
    Feb 24, 2015 at 11:37
  • Well then as I said...the IP has to be static so ...ask a Dev to hard code it instead... Feb 24, 2015 at 11:40
  • 1
    Hard coding it is exactly what I we can't do as we have multiple developers, using different PCs - that's why I want it to be part of the build.
    – fadedbee
    Feb 24, 2015 at 11:53
  • 1
    It will be connecting to the (presumably DHCP assigned) IP of whatever development machine it was built on. Feb 24, 2015 at 13:30
  • 1
    @cafebabe1991 I don't really see how your link is helpful, as is already pointed out, it receives the ip of the device running the app as opposed to the host machine that built it? I would assume my suggestion of using BuildConfig with Grade works quite well.
    – JHH
    Feb 24, 2015 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.