Home > front end >  build android apps through command-line using gradle
build android apps through command-line using gradle

Time:04-03

I have rather a curiosity than a problem which in its very own place could lead to a problem if not solved;

I've found out for building an android app you can invoke gradlew(.bat) at root dir of a project. so when I ran for instance
gradlew tasks
it generates the followings

Downloading https://services.gradle.org/distributions/gradle-4.1-all.zip

Exception in thread "main" java.net.UnknownHostException: services.gradle.org
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
        at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
        at sun.net.NetworkClient.doConnect(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
        at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
        at org.gradle.wrapper.Download.downloadInternal(Download.java:58)
        at org.gradle.wrapper.Download.download(Download.java:44)
        at org.gradle.wrapper.Install$1.call(Install.java:61)
        at org.gradle.wrapper.Install$1.call(Install.java:48)
        at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:65)
        at org.gradle.wrapper.Install.createDist(Install.java:48)
        at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128)
        at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)

I simply can connect to internet and let it to download the necessary file,
But here my mentioned curiosity originates:
How it comes that when I build(debug) the app through android studio, android studio generates the APK with no error at all?
In android studio I defined 'local gradle distribution' to be used instead of 'default gradle wrapper' as appeared in the link below:

enter image description here

enter image description here

CodePudding user response:

I noticed that Android Studio creates a temporary file each time I debug an app and pass that file as an argument to gradle command-line:

2022-04-03 10:37:18,658 [ thread 18] INFO - oject.common.GradleInitScripts - init script file sync.local.repo contents "allprojects {\n buildscript {\n repositories {\n maven { url 'C:\\\\Program Files\\\\Android\\\\Android Studio\\\\gradle\\\\m2repository'}\n }\n }\n repositories {\n maven { url 'C:\\\\Program Files\\\\Android\\\\Android Studio\\\\gradle\\\\m2repository'}\n }\n}\n"

the content in a normal perspective:


allprojects {
  buildscript {
    repositories {
      maven { url 'C:\\Program Files\\Android\\Android Studio\\gradle\\m2repository'}
    }
  }
  repositories {
      maven { url 'C:\\Program Files\\Android\\Android Studio\\gradle\\m2repository'}
  }
}



Passing to gradle command-line with --init-script option:

` gradle --init-script path/to/file --offline `

--int-script option prabably could be defined in gradle.properties file, I'v not tested!
  • Related