android studio启动Task配置

本文介绍了AndroidStudio高版本中如何手动开启Task配置,包括低版本和高版本的配置路径,以及Gradle中task的概述、创建、执行顺序和自带任务。重点提到了`assemble`、`clean`等常用任务及其执行逻辑。
摘要由CSDN通过智能技术生成

Android studio 高版本默认不开启Task配置,需要自己手动开启

1.低版本配置路径:(复制他人图片)

在这里插入图片描述

2.高版本路径:添加下图勾选配置即可

3.gradle task

3.1 初识task

gradle中所有的构建工作都是由task完成的,它帮我们处理了很多工作,比如编译,打包,发布等都是task.我们可以在项目的根目录下,打开命令行(AS自带,底部有Terminal,打开就行)执行gradlew tasks查看当前项目所有的task.

在命令行如果执行失败,则将项目的JDK location设置成本地jdk的路径,而且jdk的版本还需要是java 8. 我用的jdk版本是1.8.0_231.

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for the base and test modules
sourceSets - Prints out all the source sets defined in this project.

Build tasks
-----------
assemble - Assemble main outputs for all the variants.
assembleAndroidTest - Assembles all the Test applications.
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.
bundle - Assemble bundles for all the variants.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Cleanup tasks
-------------
lintFix - Runs lint on all variants and applies any safe suggestions to the source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'Hello'.
components - Displays the components produced by root project 'Hello'. [incubating]
dependencies - Displays all dependencies declared in root project 'Hello'.
dependencyInsight - Displays the insight into a specific dependency in root project 'Hello'.
dependentComponents - Displays the dependent components of components in root project 'Hello'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'Hello'. [incubating]
projects - Displays the sub-projects of root project 'Hello'.
properties - Displays the properties of root project 'Hello'.
tasks - Displays the tasks runnable from root project 'Hello' (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.
lintVitalRelease - Runs lint on just the fatal issues in 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.
To see all tasks and more detail, run gradlew tasks --all

可以看到,这里有很多的task.比如我们在命令行执行gradlew clean就是clean.执行gradlew installDebug就是构建debug项目然后安装到手机上.

3.2 编写task

书写task非常简单,比如我们在根目录的build.gradle中加入一个hello的task

task hello() {
    println "hello world"

    //将给定的闭包 添加到此task操作链表的开头
    doFirst {
        println "hello task doFirst"
    }

    doLast {
        println "hello task doLast"
    }
}

然后在命令行执行gradlew hello,输出如下

setting 开始配置
setting 配置完成

> Configure project :
根build.gradle 开始配置
hello world
根build.gradle 配置完成

> Configure project :app
app build.gradle 开始配置
app build.gradle 配置完成

> Task :hello
hello task doFirst
hello task doLast

它会先配置完成,才会执行.在一个task内部其实拥有一个action列表,执行的时候其实就是执行这个列表,它的类型是一个List.上面的doFirst和doLast就是创建action的两个方法,文档.doFirst是在最开始执行,doLast是在最后执行,大括号里面传入的是闭包.

3.3 task执行顺序

task是有执行顺序的,在创建完Android项目之后,根目录下的build.gradle中,有一个clean的task.这个是AS自动给我们生成的.

task clean(type: Delete) {
    delete rootProject.buildDir
}

我们先在根目录下创建test.txt文件,然后我们在这个task中做一些改动,执行到clean这个task时删除根目录下的test.txt文件.

task clean(type: Delete) {
    delete rootProject.buildDir

    doLast {
        def file = new File('test.txt')
        delete file
        println "清理"
    }
}

然后我们在hello这个task的下面写上

hello.dependsOn clean
这样就表示hello这个task依赖clean这个task,当执行hello这个task时需要先执行clean. 我们在命令行执行gradlew hello看看是不是这样.我执行之后它的输出是

Task :clean
清理

Task :hello
hello task doFirst
hello task doLast
先执行clean,再执行hello这个task.而且还看到test.txt文件被删除(如果看到没删除,刷新一下看看)了,那么说明确实是clean先执行.

这个顺序有什么用?其实是很有用的,比如执行安装task的时候,肯定会先执行编译,打包这些步骤.

3.4 自带 gradle task

当我们在AS中创建Android项目的时候,默认会带一些Android的一些gradle task,这些task都是gradle和Android Gradle Plugin给我们创建好的,可以直接用.
在这里插入图片描述
比如我们上面使用到的gradlew clean是用来清理项目的.和编译相关的task主要有:build和assemble,其中build依赖assemble,也就是说执行build之前会先执行assemble。在Android上,会根据buildType和productFlavor的不同自动创建多个assembleXxx任务,如assembleDebug,assembleRelease等,assemble会依赖所有的assembleXxx任务,也就是说执行assemble会先执行assembleDebug,assembleRelease等一系列的assemble任务。

如果想看Android Gradle Plugin源码,可以在app/build.gradle中的dependencies下面引入

compileOnly 'com.android.tools.build:gradle:3.5.2'

然后就可以在项目的External Libraries中看到该jar的源码,
在这里插入图片描述

比如clean这个task是在com.android.build.gradle.tasks.CleanBuildCache.java里面定义的

@TaskAction
public void clean() throws IOException {
    Preconditions.checkNotNull(buildCache, "buildCache must not be null");
    buildCache.delete();
}

通过查询gradle官方文档可知,@TaskAction的作用:Marks a method as the action to run when the task is executed. 将方法标记为执行任务时要运行的动作.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio配置包括以下几个步骤: 1. 下载Android Studio:首先,你需要从官方网站下载Android Studio安装程序。你可以在网站上找到下载按钮,并选择适合你的操作系统的版本进行下载。\[1\] 2. 安装Android Studio:安装Android Studio时,你需要按照安装向导的指示进行操作。这包括选择安装组件、安装位置和开始菜单文件夹等。\[2\] 3. 启动配置Android Studio:一旦安装完成,你可以启动Android Studio。在启动过程中,你需要选择是否导入先前的设置,选择安装类型,选择默认JDK位置,选择用户界面主题以及选择安卓SDK组件和安装位置等。\[2\] 4. 创建安卓应用:配置完成后,你可以开始创建安卓应用程序。Android Studio提供了许多功能,如基于Gradle的灵活构建系统、快速和功能丰富的模拟器、丰富的测试工具和性能工具、C++和NDK支持等。你还可以使用布局编辑器来设计应用程序的界面。\[3\] 总结起来,Android Studio配置包括下载和安装Android Studio启动配置Android Studio,以及使用Android Studio创建安卓应用程序。希望这些步骤对你有帮助! #### 引用[.reference_title] - *1* *2* *3* [Android Studio 安装与配置](https://blog.csdn.net/ZQIR12/article/details/126840856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值