4

I want to add some code to my settings.gradle file

  if (!hasInternetAccess()) {
    startParameter.offline=true
  }

to allow gradle-code compilation if my computer has no internetconnection.

does anybody know how a gradle script can implement hasInternetAccess()?

Background: gradle or android studios compilation fails if the computer has no internet connection because it could not look for updated dependencies.

instead of manually invoking gradle with the "--offline" parameter

or adding

    startParameter.offline=true

to the settings.gradle file i am looking for a way to automate the configuration

[update]

i followed @opal-s link and added these lines to my settings.gradle file:

boolean isReachable = java.net.InetAddress.getByName("www.google.com").isReachable(1500)
print "has internet: " + isReachable

in the debug output i always get

has internet: false

although i can successfully ping www.google.com

Community
  • 1
  • 1
k3b
  • 14,517
  • 7
  • 53
  • 85
  • Have a look at: http://stackoverflow.com/questions/1402005/how-to-check-if-internet-connection-is-present-in-java. You can implement it as plain java method. – Opal May 09 '14 at 16:17
  • Here you will find a working solution to check internet connection http://stackoverflow.com/questions/9922543/why-does-inetaddress-isreachable-return-false-when-i-can-ping-the-ip-address – nenick May 11 '14 at 10:16

1 Answers1

0

Using --offline should result in the behavior you want.

Dirk Hoffmann
  • 1,444
  • 17
  • 35