如何在build.gradle中禁用任务

时间:2017-11-09 02:07:58

标签: android gradle build.gradle

我想在运行gradle build时跳过一些任务。我知道可以使用-x命令行完成:

gradle build -x unwantedTask 

我的问题是如何在build.gradle中实现相同的结果?

4 个答案:

答案 0 :(得分:18)

您可以尝试例如:

unwantedTask.enabled = false

答案 1 :(得分:15)

因为我需要禁用一堆任务,所以我在func startMonitoring(list:List) { if !CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) { return } self.list = list let region = self.region(list: list) self.locationBasedNotification(list: list, flag: true, region: region as! CLCircularRegion) locationManager.startMonitoring(for:region) locationManager.startUpdatingLocation() } func stopMonitoring(list:List) { self.list = list for region in locationManager.monitoredRegions { let circularRegion = region as? CLCircularRegion if circularRegion?.identifier == list.identifier { self.locationBasedNotification(list: list, flag: false, region: circularRegion!) locationManager.stopMonitoring(for: circularRegion!) // monitoredRegions.removeValueForKey(region.identifier) } } } func locationBasedNotification(list:List,flag:Bool,region:CLCircularRegion) { var trigger : UNLocationNotificationTrigger! if flag == true { self.locationIdentifier = "location"+String(list.seqNo) let categoryIdentifier = "locationmyCategory"+String(list.seqNo) let textIdentifier = "locationtext"+String(list.seqNo) let action = UNNotificationAction(identifier: self.locationIdentifier!, title: "You, have entered \(list.storeName)", options: []) let category = UNNotificationCategory(identifier: categoryIdentifier, actions: [action], intentIdentifiers: [], options: []) UNUserNotificationCenter.current().setNotificationCategories([category]) trigger = UNLocationNotificationTrigger(region: region, repeats: true) let content = UNMutableNotificationContent.init() content.title = list.storeName content.body = "They are \(list.totalItems) in Shopping List" content.sound = UNNotificationSound.default() content.categoryIdentifier = categoryIdentifier let request = UNNotificationRequest(identifier: textIdentifier, content: content, trigger: trigger) UNUserNotificationCenter.current().delegate = self UNUserNotificationCenter.current().add(request) {(error) in if let error = error { print("Uh oh! We had an error: \(error)") } } } else { UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["locationIdentifier"+String(list.seqNo)]) } } 文件中的apply plugin:之前使用了以下代码:

build.gradle

答案 2 :(得分:1)

对于通用方法,您可以:

unwantedTask.onlyIf { <expression> }

例如:

compileJava.onlyIf { false }

高级IDE(如IDEA)通过代码完成,将为您提供build.gradle中任何给定对象上可以做的很多事情-毕竟,这只是一个Groovy脚本。

答案 3 :(得分:0)

@LukasKörfer在评论中暗示要真正从构建中删除任务,而不只是跳过它,一种解决方案是将其添加到构建脚本中:

project.gradle.startParameter.excludedTaskNames.add('yourTaskName')

但是,这似乎删除了所有子项目的任务。