5.2、Android Studio 从命令行构建您的应用

文章摘要
1、 Gradle 任务列表
2、构建调式APK


英文文献

一、Gradle 任务

您可以使用 Gradle 包装器命令行工具执行您的 Android 项目中可用的所有构建任务。它可作为 Windows 的批处理文件 (gradlew.bat) 和 Linux 与 Mac 的 shell 脚本文件 (gradlew.sh) 使用,可以从您使用 Android Studio 创建的每个项目的根获取。

要使用包装器运行任务,请使用下列命令之一:

  • 在 Windows 上:
gradlew task-name
  • 在 Mac 或 Linux 上:
./gradlew task-name

要查看您的项目所有可用构建任务的列表,请执行 tasks:

gradlew 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.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
extractDebugAnnotations - Extracts Android annotations for the debug variant into the archive file
extractReleaseAnnotations - Extracts Android annotations for the release variant into the archive file
mockableAndroidJar - 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
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'UsingDemosSample'.
components - Displays the components produced by root project 'UsingDemosSample'. [incubating]
dependencies - Displays all dependencies declared in root project 'UsingDemosSample'.
dependencyInsight - Displays the insight into a specific dependency in root project 'UsingDemosSample'.
dependentComponents - Displays the dependent components of components in root project 'UsingDemosSample'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'UsingDemosSample'. [incubating]
projects - Displays the sub-projects of root project 'UsingDemosSample'.
properties - Displays the properties of root project 'UsingDemosSample'.
tasks - Displays the tasks runnable from root project 'UsingDemosSample' (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 Debug build.
uninstallRelease - Uninstalls the Release build.

Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device 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.

二、构建调试 APK

要想立即测试和调试应用,您可以构建调试 APK。调试 APK 通过 SDK 工具提供的调试密钥签署,并允许通过 adb 调试。
要构建调试 APK,请打开命令行,然后导航至项目目录的根 - 在 Android Studio 中,选择 View > Tool Windows > Terminal。要启动调试构建,请调用 assembleDebug 任务:

gradlew assembleDebug

这将在 project_name/module_name/build/outputs/apk/ 中创建一个名称为 module_name-debug.apk 的 APK。此文件已通过调试密钥签署,并与 zipalign 对齐,因此,您可以在设备上立即安装。

或者要构建 APK,并立即在运行的模拟器或连接的设备上安装,请改为调用 installDebug:

gradlew installDebug

上述任务名称中的“调试”部分仅仅是构建变体名称的骆驼拼写法版本,因此,可以使用您想要汇编或安装的任何构建变体替换。例如,如果您有“演示”产品风味,则可以过 assembleDemoDebug 任务构建调试版本。

要查看每个变体可用的所有构建和安装任务(包括卸载任务),请运行 tasks 任务。

你可能感兴趣的:(5.2、Android Studio 从命令行构建您的应用)