Android Studio-将构建机的IP地址添加到Android应用中

fad*_*bee 5 android gradle android-studio

在应用程序开发期间,我希望该应用程序重新连接到其构建的机器(通过Wifi)。

由于我们有多个开发人员,因此每个开发人员都将使用不同的IP地址。

我怎样才能做到这一点?

(某些开发人员在Windows 7上使用AndroidStudio,在Ubuntu 14.04LTS上使用其他AndroidStudio。)

Lás*_*tra 6

这是我的解决方案。保持干净的 git 状态非常有用。

/**
 * 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
    }
}
Run Code Online (Sandbox Code Playgroud)

我将它用于单个buildType项目:

android {
  // ...

  buildTypes {
    // ...

    dev {
      // ...
      buildConfigField "String", "BACKEND_URL", "\"http://${getDevHost()}:9000/\""
    }
  }
}
Run Code Online (Sandbox Code Playgroud)