android studio启动Task配置

这篇具有很好参考价值的文章主要介绍了android studio启动Task配置。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

android studio启动Task配置,android studio,android,task

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

android studio启动Task配置,android studio,android,task

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给我们创建好的,可以直接用.
android studio启动Task配置,android studio,android,task
比如我们上面使用到的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的源码,
android studio启动Task配置,android studio,android,task

比如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. 将方法标记为执行任务时要运行的动作.文章来源地址https://www.toymoban.com/news/detail-726880.html

到了这里,关于android studio启动Task配置的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • android studio构建报错Could not create task ‘:app:generateLintModuleInfo‘.

    原因:创建一个全新项目,只导入了一个第三方库。构建时总是无法成功。 一、首先报错The minCompileSdk (32) specified in adependency\\\'s AAR metadata 想是因为三方库和项目配置的build.gradle不一致的原因,修改项目配置到32 二、重新构建,未成功。报错Could not create task \\\':app:generateLintModu

    2024年02月11日
    浏览(41)
  • android studio 我遇到的Task :app:compileDebugJavaWithJavac FAILED问题及解决过程

    前几天一个网友在学习我的一个小项目的时候,发现无法达到目的,在帮他解决问题的过程中发现他用的是最近的giraffe版本的as,我用的是老版本,没办法打开他的项目,没办法只能卸载我的as,安装了最近版的diraffe版。也成功帮网友解决了问题。本来想着退回之前的版本,

    2024年02月04日
    浏览(42)
  • 关于Android Studio编译时提示Execution failed for task ‘:xxx:compileDebugJavaWithJavac‘

    在项目首次导入编译时,总是会出现一些比较难排除的问题,特别是在提示中无法直接找到原因: 这里我使用的是Android Studio 2021.3.1 故在Terminal输入 这里的提示是项目的gradle缺少gradle-wrapper.properties文件,我的解决方案是: 从其他项目中拷贝一份gradle-wrapper.properties

    2024年02月11日
    浏览(73)
  • 解决Flutter启动一直卡在 Running Gradle task ‘assembleDebug‘...

    前言 新建了一个Flutter工程后,Run APP 却一直卡在了Running Gradle task ‘assembleDebug’… 这里。百度查询原因是因为 Gradle 的 Maven 仓库在国外, 因此需要使用阿里云的镜像地址。 1、修改项目中android/build.gradle文件 将 buildscript.repositories 下面的 allprojects.repositories 同上修改 使用\\\"阿

    2024年02月04日
    浏览(33)
  • 【Android】Activity task和Instrumentation杂谈

    Android不仅可以装载众多的系统组件,还可以将它们跨进程组成ActivityTask,这个特性使得每个应用都不是孤立的。 从数据结构角度看,Task有先后之分,源码实现上采取了stack的方式。这种方式不仅符合用户的逻辑思维和使用习惯,还可以极大复用了系统的资源。 如图,contac

    2024年04月16日
    浏览(17)
  • 深入理解 Flink(八)Flink Task 部署初始化和启动详解

    核心入口: 部署 Task 链条:JobMaster -- DefaultScheduler -- SchedulingStrategy -- ExecutionVertex -- Execution -- RPC请求 -- TaskExecutor JobMaster 向 TaskExecutor 发送 submitTask() 的 RPC 请求,用来部署 StreamTask 运行。TaskExecutor 接收到 JobMaster 的部署 Task 运行的 RPC 请求的时候,就封装了一个 Task 抽象,然

    2024年01月17日
    浏览(68)
  • Android Could not create task ‘:app:processDebugResources‘.

    Could not create task \\\':app:processDebugResources\\\'. Cannot use @TaskAction annotation on method IncrementalTask.taskAction$gradle_core() because interface org.gradle.api.tasks.incremental.IncrementalTaskInputs is not a valid parameter to an action method. 出现这个的原因是 当前项目使用的android studio 换成了一个旧版本的Android studio 路

    2024年02月06日
    浏览(34)
  • Android问题笔记 - 编译报错Task :app:compileDebugJavaWithJavac FAILED

    专栏分享 点击跳转=Unity3D特效百例 点击跳转=案例项目实战源码 点击跳转=游戏脚本-辅助自动化 点击跳转=Android控件全解手册 点击跳转=Scratch编程案例 点击跳转=软考全系列 众所周知,人生是一个漫长的流程,不断 克服困难 ,不断反思前进的过程。在这个过程中会产生很多对

    2024年02月17日
    浏览(39)
  • Flink 启动就报错,但exception没提示。其中一个task failure 该怎么办?

    最近我在生产又遇到一个问题,就是消费着一段时间之后,忽然就不再消费了,但也不报错。观察了几次,我发现时间基本是停留在上下班高峰期数据量最大的时候。我主观猜测可能是同时间进来的数据过多,处理不来导致的。但这个问题我还没来的及思考怎么处理,因此我

    2024年02月16日
    浏览(46)
  • Android 开发 错误 Execution failed for task ‘:app:processDebugMainManifest‘.

    在配置文件AndroidManifest.xml中添加代码android:exported=“true” 关于android:exported=\\\"true\\\"的解释: Android相关属性的介绍:android:exported = true 在Activity中该属性用来标示:当前Activity是否可以被另一个Application的组件启动:true允许被启动;false不允许被启动。 android:exported 是Android中的四

    2024年02月10日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包