android studio 命令行替换,5.2、Android Studio 从命令行构建您的应用

本文介绍了如何使用Gradle命令行工具执行Android项目的构建任务,包括查看任务列表、构建调试APK等。通过`gradlew tasks`命令可以列出所有可用任务,如`assembleDebug`用于构建调试APK,`installDebug`则会安装到设备进行调试。此外,还详细阐述了不同构建变体的任务及其功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章摘要

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 任务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值