本文共 7006 字,大约阅读时间需要 23 分钟。
1.3从命令行执行Gradle构建 问题 您希望从命令行运行Gradle任务。 解 从命令行,使用提供的Gradle包装器或安装Gradle并直接运行它。 讨论 你不需要安装Gradle来构建Android项目。 Android Studio附带了Gradle发行版(以插件的形式),并包含支持它的专用功能。 术语“Gradle包装器”是指在Android应用程序的根目录中的Unix和gradlew.bat脚本的gradlew脚本,其中结尾“w”代表“包装器”。 Gradle包装器的目的是允许客户端运行Gradle,而不必首先安装它。包装器在应用程序根目录中的gradle / wrapper文件夹中使用gradle-wrapper.jar和gradle-wrapper.properties文件来启动进程。属性文件的示例如示例1-5所示。 实例1-5。 gradle-wrapper.properties中的键和值#Mon Dec 28 10:00:20 PST 2015distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distszipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsdistributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zipdistributionUrl属性指示包装器将下载并安装Gradle的2.10版本。 1第一次运行后,Gradle分布将缓存在zipStoreBase目录下的zipStorePath文件夹中,然后可用于Gradle任务的所有后续执行。 只需在Unix上执行./gradlew命令或在Windows上执行gradlew.bat命令即可在命令行中使用包装器(示例1-6)。 实例1-6。从运行构建任务的输出
> ./gradlew buildDownloadinghttps://services.gradle.org/distributions/gradle-2.10-all.zip....................................................... (download of Gradle 2.10) .......................................................Unzipping /Users/kousen/.gradle/wrapper/dists/3i2gob.../gradle-2.10-all.zipto /Users/kousen/.gradle/wrapper/dists/gradle-2.10-all/3i2gob...Set executable permissions for:/Users/kousen/.gradle/wrapper/dists/gradle-2.10-all/3i2gob.../gradle-2.10/bin/gradleStarting a new Gradle Daemon for this build (subsequent builds will be faster).:app:preBuild UP-TO-DATE:app:preDebugBuild UP-TO-DATE... lots of tasks ...:app:compileLint:app:lintWrote HTML report to file:.../MyAndroidApp/app/build/outputs/lint-results.htmlWrote XML report to .../MyAndroidApp/app/build/outputs/lint-results.xml:app:preDebugUnitTestBuild UP-TO-DATE:app:prepareDebugUnitTestDependencies... lots of tasks ...:app:test:app:check:app:buildBUILD SUCCESSFUL在本书中,示例显示了基于Unix的操作系统的./gradlew命令。对于Windows,只需替换gradlew或gradlew.bat没有点斜线。 初始下载可能需要几分钟,具体取决于您的互联网连接速度。然而,它只需要做一次。之后,后续构建将使用缓存版本。 您可以在命令行中运行任何受支持的Gradle任务,包括您自己的自定义任务。编译代码将在app / build文件夹中找到。生成的apk(Android包)文件可以在app / build / outputs / apk目录中找到。 Gradle中的tasks命令显示了构建中可用的任务,如示例1-7所示。 实例1-7。任务输出
:tasks------------------------------------------------------------All tasks runnable from root project------------------------------------------------------------Android tasks-------------androidDependencies - Displays the Android dependencies of the project.signingReport - Displays the signing info for each variant.sourceSets - Prints out all the source sets defined in this project.Build tasks-----------assemble - Assembles all variants of all applications and secondary packages.assembleAndroidTest - Assembles all the Test applications.assembleDebug - Assembles all Debug builds.assembleRelease - Assembles all Release builds.build - Assembles and tests this project.buildDependents - Assembles and tests this project and all projects that depend on it.buildNeeded - Assembles and tests this project and all projects it depends on.compileDebugAndroidTestSourcescompileDebugSourcescompileDebugUnitTestSourcescompileReleaseSourcescompileReleaseUnitTestSourcesmockableAndroidJar - Creates a version of android.jar that's suitable for unit tests.Build Setup tasks-----------------init - Initializes a new Gradle build. [incubating]wrapper - Generates Gradle wrapper files. [incubating]Help tasks----------components - Displays the components produced by root project 'MyAndroidApp'.dependencies - Displays all dependencies declared in root project 'MyAndroidApp'.dependencyInsight - Displays the insight into a specific dependency in rootproject 'MyAndroidApp'.help - Displays a help message.model - Displays the configuration model of root project 'MyAndroidApp'. [incubating]projects - Displays the subprojects of root project 'MyAndroidApp'.properties - Displays the properties of root project 'MyAndroidApp'.tasks - Displays the tasks runnable from root project 'MyAndroidApp'(some of the displayed tasks may belong to subprojects).Install tasks-------------installDebug - Installs the Debug build.installDebugAndroidTest - Installs the android (on device) tests for the Debug build.uninstallAll - Uninstall all applications.uninstallDebug - Uninstalls the Debug build.uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the build.uninstallRelease - Uninstalls the Release build.Verification tasks------------------check - Runs all checks.clean - Deletes the build directory.connectedAndroidTest - Installs and runs instrumentation tests for all flavorson connected devices.connectedCheck - Runs all device checks on currently connected devices.connectedDebugAndroidTest - Installs and runs the tests for debug connected devices.deviceAndroidTest - Installs and runs instrumentation tests using all Providers.deviceCheck - Runs all device checks using Device Providers and Test Servers.lint - Runs lint on all variants.lintDebug - Runs lint on the Debug build.lintRelease - Runs lint on the Release build.test - Run unit tests for all variants.testDebugUnitTest - Run unit tests for the debug build.testReleaseUnitTest - Run unit tests for the release build.Other tasks-----------cleanjarDebugClassesjarReleaseClasseslintVitalRelease - Runs lint on just the fatal issues in the Release build.To see all tasks and more detail, run gradlew tasks --allTo see more detail about a task, run gradlew help --task虽然这看起来像很多任务,但实际上你使用的是一个小数字。 当您向项目中添加多个构建类型和风格时,数量将显着增加。 其他功能和命令行标志 您可以通过使用空格分隔多个任务来运行多个任务,如示例1-8所示。 实施例1-8。执行多个任务 > ./gradlew lint assembleDebug 请注意,重复相同的任务名称只会执行一次。 可以使用-x标志来排除任务,如示例1-9所示。 实施例1-9。排除lintDebug任务 > ./gradlew assembleDebug -x lintDebug tasks命令上的--all标志显示项目中的所有任务以及每个任务的依赖关系。BUILD SUCCESSFUL
gradle任务的输出 - 可以很长。
您可以通过提供足够的字母来唯一确定它来从命令行缩写任务名称(示例1-10)。
实例1-10。 每个配置的依赖关系树> ./gradlew anDep:app:androidDependenciesdebug\--- com.android.support:appcompat-v7:23.3.0+--- com.android.support:support-vector-drawable:23.3.0| \--- com.android.support:support-v4:23.3.0| \--- LOCAL: internal_impl-23.3.0.jar+--- com.android.support:animated-vector-drawable:23.3.0| \--- com.android.support:support-vector-drawable:23.3.0| \--- com.android.support:support-v4:23.3.0| \--- LOCAL: internal_impl-23.3.0.jar\--- com.android.support:support-v4:23.3.0\--- LOCAL: internal_impl-23.3.0.jardebugAndroidTestNo dependenciesdebugUnitTestNo dependenciesrelease\--- com.android.support:appcompat-v7:23.3.0+--- com.android.support:support-vector-drawable:23.3.0| \--- com.android.support:support-v4:23.3.0| \--- LOCAL: internal_impl-23.3.0.jar+--- com.android.support:animated-vector-drawable:23.3.0| \--- com.android.support:support-vector-drawable:23.3.0| \--- com.android.support:support-v4:23.3.0| \--- LOCAL: internal_impl-23.3.0.jar\--- com.android.support:support-v4:23.3.0\--- LOCAL: internal_impl-23.3.0.jarreleaseUnitTestNo dependenciesBUILD SUCCESSFUL骆驼案例符号(anDep for androidDependencies)工作得很好,只要分辨率是唯一的(示例1-11)。 实施例1-11。 没有足够的字母是独一无二的 错误消息显示出错是什么:pro是不明确的,因为它匹配项目和属性。 只需添加另一个字母,使其唯一。 最后,如果您的构建文件未被调用build.gradle,请使用-b标志来指定构建文件名(示例1-12)。 实施例1-12。 使用非默认构建文件名 > ./gradlew -b app.gradle
转载地址:http://szrdi.baihongyu.com/