使用 gradle 通过命令行构建 android 应用程序
build android apps through command-line using gradle
我比较好奇,而不是一个如果不解决就可能导致问题的问题;
我发现要构建一个 android 应用程序,您可以在项目的根目录中调用 gradlew(.bat)。所以当我 运行 例如
gradlew tasks
它生成以下内容
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.call(Install.java:61)
at org.gradle.wrapper.Install.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)
我只是可以连接到互联网并让它下载必要的文件,
但这里我提到的好奇心起源于:
当我通过 android studio 构建(调试)应用程序时,android studio 生成的 APK 完全没有错误,这是怎么回事?
在 android studio 中,我定义了 'local gradle distribution' 来代替下面 link 中出现的 'default gradle wrapper':
captured_and_std_img
回到命令行,然后我定义了环境变量 GRADLE_HOME 并再次尝试使用 gradle[=60 构建应用程序=] 现在是一个可识别的命令,如下所示:
gradle assembleDebug
但它也产生了以下内容:
Starting a Gradle Daemon, 2 incompatible Daemons could not be reused, use --status for details
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'MyApplication2'.
> Could not resolve all files for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:3.0.1.
Required by:
project :
> Could not resolve com.android.tools.build:gradle:3.0.1.
> Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom
'.
> Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom'.
> dl.google.com
> Could not resolve com.android.tools.build:gradle:3.0.1.
> Could not get resource 'https://jcenter.bintray.com/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom'.
> Could not GET 'https://jcenter.bintray.com/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom'.
> jcenter.bintray.com
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 38s
我不确定 assembleDebug 是否可以用作 gradle 命令的参数,我知道你可以将它与 gradlew,但我仍然不想下载文件 (gradle-4.1-all.zip) 并想使用 [= 构建 APK 70=]。 :))
任何可能的解决方案将不胜感激
编辑:
build.gradle(项目)文件内容:
//task wrapper(type: Wrapper) {
// gradleVersion = '4.1'
//}
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(模块):
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
buildToolsVersion '28.0.3'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:25.3.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:25.3.1'
}
检查您的 Android Studio 是否有代理并检查您的 gradle.properties 是否也有代理
我注意到 Android Studio 每次我 调试 应用程序并将该文件作为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"
普通视角下的内容:
allprojects {
buildscript {
repositories {
maven { url 'C:\Program Files\Android\Android Studio\gradle\m2repository'}
}
}
repositories {
maven { url 'C:\Program Files\Android\Android Studio\gradle\m2repository'}
}
}
使用 --init-script 选项传递给 gradle command-line:
gradle --init-script path/to/file --offline assembleDebug
--init-script 选项大概可以在gradle.properties 文件中定义,我没有测试过!
我比较好奇,而不是一个如果不解决就可能导致问题的问题;
我发现要构建一个 android 应用程序,您可以在项目的根目录中调用 gradlew(.bat)。所以当我 运行 例如
gradlew tasks
它生成以下内容
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.call(Install.java:61)
at org.gradle.wrapper.Install.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)
我只是可以连接到互联网并让它下载必要的文件,
但这里我提到的好奇心起源于:
当我通过 android studio 构建(调试)应用程序时,android studio 生成的 APK 完全没有错误,这是怎么回事?
在 android studio 中,我定义了 'local gradle distribution' 来代替下面 link 中出现的 'default gradle wrapper':
captured_and_std_img
回到命令行,然后我定义了环境变量 GRADLE_HOME 并再次尝试使用 gradle[=60 构建应用程序=] 现在是一个可识别的命令,如下所示:
gradle assembleDebug
但它也产生了以下内容:
Starting a Gradle Daemon, 2 incompatible Daemons could not be reused, use --status for details
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'MyApplication2'.
> Could not resolve all files for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:3.0.1.
Required by:
project :
> Could not resolve com.android.tools.build:gradle:3.0.1.
> Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom
'.
> Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom'.
> dl.google.com
> Could not resolve com.android.tools.build:gradle:3.0.1.
> Could not get resource 'https://jcenter.bintray.com/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom'.
> Could not GET 'https://jcenter.bintray.com/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom'.
> jcenter.bintray.com
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 38s
我不确定 assembleDebug 是否可以用作 gradle 命令的参数,我知道你可以将它与 gradlew,但我仍然不想下载文件 (gradle-4.1-all.zip) 并想使用 [= 构建 APK 70=]。 :))
任何可能的解决方案将不胜感激
编辑:
build.gradle(项目)文件内容:
//task wrapper(type: Wrapper) {
// gradleVersion = '4.1'
//}
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(模块):
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
buildToolsVersion '28.0.3'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:25.3.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:25.3.1'
}
检查您的 Android Studio 是否有代理并检查您的 gradle.properties 是否也有代理
我注意到 Android Studio 每次我 调试 应用程序并将该文件作为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"
普通视角下的内容:
allprojects {
buildscript {
repositories {
maven { url 'C:\Program Files\Android\Android Studio\gradle\m2repository'}
}
}
repositories {
maven { url 'C:\Program Files\Android\Android Studio\gradle\m2repository'}
}
}
使用 --init-script 选项传递给 gradle command-line:
gradle --init-script path/to/file --offline assembleDebug
--init-script 选项大概可以在gradle.properties 文件中定义,我没有测试过!