Gradle shouldRunAfter not working in Android Studio - android

I want to run my custom task after assembleDebug task in Android Studio.My normal build.gradle is
apply plugin: 'com.android.application'
android {
...
}
dependencies {
...
}
task printName{
println 'Hello Guffy'
}
printName.shouldRunAfter(tasks.assembleDebug)
// or printName.shouldRunAfter(assembleDebug)
// or assembleDebug.shouldRunAfter(printName)
which is not compiling.The gradle error is
Error:(36, 0) Could not get unknown property 'assembleDebug' for task set
Is assembleDebug or other tasks not available to custom tasks ? Or is there any basic error I am doing ? Thanks

Suggested by Piotr Zawadzki, putting task in quotes worked .
So the code should be like
printName.shouldRunAfter("assembleDebug")

Related

Gradle .execute method showing error after gradle update

My project was running perfect with gradle plugin version 3.0.1 after updating to version 3.4.1 I am going through sync failure.
Previously I was using wrapper 4.10.1 which is now updated to 5.1.1.
afterEvaluate(new Action<Project>() {
#Override
void execute(Project project) {
tasks.getByName("assembleRelease").doLast {
tasks.copyReleaseBuild.execute() //error is here
tasks.copyReleaseBuildToXX.execute()
tasks.copyReleaseBuildToXXXX.execute()
}
}
copyReleaseBuild task is written something like below...
task copyReleaseBuild(type: Copy) {
def releaseDir = getProjectProperty('releaseDir')
if (releaseDir?.trim()) {
//if release folder is provided
def releaseAarFile =
getProjectProperty('sourceCodeDir') + "/android-corekit/kit/build/outputs/aar/kit-release.aar"
from releaseAarFile
into releaseDir
}
task copyReleaseBuildToXX(type: Copy) {
from "./build/outputs/aar/kit-release.aar"
into "../kitwrapper/libs"
}
task copyReleaseBuildToXXXX(type: Copy) {
from "./build/outputs/aar/kit-release.aar"
into "../kitwrapperapp/libs"
}
}
I have been trying to resolve from last two days but nothing is working as I lack knowledge of groovy.
Please check error log I am getting while building the project...
It happens because you are using an updated version of gradle (check the migration to gradle v5)
The following properties and methods of TaskInternal have been removed — use task dependencies, task rules, reusable utility methods, or the Worker API in place of executing a task directly.
execute()
executer
getValidators()
addValidator()
Now you can't call the execute method directly, but you have to use the tasks dependencies to obtain the same result.
Just define in your build.gradle:
task copyReleaseBuild {
dependsOn 'assembleRelease'
//...
}
and remove this:
afterEvaluate(new Action<Project>() {
#Override
void execute(Project project) {
tasks.getByName("assembleRelease").doLast {
tasks.copyReleaseBuild.execute() //error is here
}
}
Upgrading your build from Gradle 4.x to 5.0
The following properties and methods of TaskInternal have been removed — use task dependencies, task rules, reusable utility methods, or the Worker API in place of executing a task directly.
execute()
executer
getValidators()
addValidator()
in your case seems you have dependency between tasks: copyReleaseBuild must run after assembleRelease
so, this simple build.gradle represents this dependency:
task copyReleaseBuild {
dependsOn 'assembleRelease'
doLast {
println 'run copyReleaseBuild'
}
}
task assembleRelease {
doLast {
println 'run assembleRelease'
}
}
and the command gradle copyReleaseBuild
runs both tasks:
# gradle copyReleaseBuild
> Task :assembleRelease
run assembleRelease
> Task :copyReleaseBuild
run copyReleaseBuild
BUILD SUCCESSFUL in 2s
2 actionable tasks: 2 executed

Checkstyle Plugin does not add gradle tasks

i want to use checkstyle plugin in my gradle project, the gradle documentation says that it will add a few tasks:
https://docs.gradle.org/current/userguide/checkstyle_plugin.html
checkstyleMain, checkstyleTest, checkstyleSourceSet
I added this into my app build.gradle file:
apply plugin: 'checkstyle'
I want to run gradle task from cmd to perform code style check, but there are no one checkstyle task. I checked the whole list by typing:
./gradlew tasks
I also tried to add checkstyle jar as library dependency to app module.
Can anyone tell me what i am doing wrong and how can i get my checkstyle tasks?
Well, the checkstyle plugin adds its tasks in the Other tasks virtual task group. Those are really the tasks which have not been assigned to a task group. So, they are shown only when you run ./gradlew tasks --all (note the --all).
Complete working build.gradle:
apply plugin: 'java';
apply plugin: 'checkstyle';
Then run ./gradlew tasks --all, output:
<...snip...>
Other tasks
-----------
checkstyleMain - Run Checkstyle analysis for main classes
checkstyleTest - Run Checkstyle analysis for test classes
<...snip...>
If you want the Checkstyle tasks to appear without --all, then assign them to a task group, for example:
tasks.withType(Checkstyle).each {
it.group = 'verification'
}
Looking at other Android projects, built with Gradle, that run checkstyle (thanks Square) I found that I needed to do some setup for the task to appear. Without the task declaration I would still see my initial error.
build.gradle:
...
apply plugin: 'checkstyle'
...
checkstyle {
configFile rootProject.file('checkstyle.xml')
ignoreFailures false
showViolations true
toolVersion = "7.8.1"
}
task Checkstyle(type: Checkstyle) {
configFile rootProject.file('checkstyle.xml')
source 'src/main/java'
ignoreFailures false
showViolations true
include '**/*.java'
classpath = files()
}
// adds checkstyle task to existing check task
afterEvaluate {
if (project.tasks.getByName("check")) {
check.dependsOn('checkstyle')
}
}
You also need a checkstyle configuration file, either by placing one at the default location as documented or by configuring it explicitly.
For example:
checkstyle {
config = resources.text.fromFile('config/checkstyle.xml')
}

Error:Execution failed for task ':app:transformClassesWithPreJackRuntimeLibrariesForDebug'

My project was working fine, I stopped development for a week and when I came back, when I build the project I get this exception:
Error:Execution failed for task ':app:transformClassesWithPreJackRuntimeLibrariesForDebug'. > com.android.sched.scheduler.RunnerProcessException: Error during 'ReachingDefinitions' runner on 'static void org.mozilla.universalchardet.prober.contextanalysis.JapaneseContextAnalysis.
I tried to change org.gradle.jvmargs=-Xmx1024m, it was actually 1536.
No change.
What is this exception about? How to fix it?
Thank you.
UPDATE:
I was planning to use Data Binding in this project. Apparently,
apply plugin: 'com.android.databinding'
and this
classpath 'com.android.databinding:dataBinder:1.0-rc4'
caused his behaviour.
The question is why?
You just need to enable DataBinding in build.gradle to use it
android{
defaultConfig{
dataBinding {
enabled true
}
}
}

Error:Could not find property 'assembleDebug' on project ':app'

I am using 'com.android.tools.build:gradle:2.2.0-alpha6' and Android Studio 2.2 Preview 6. The build runs perfectly fine on Gradle 2.1.0, but to enable instant run it asks me to update Gradle plugin.
On updating Gradle plugin, the build shows "Error:Could not find property 'assembleDebug' on project ':app'". I already tried cleaning .gradle and .idea and reloading the project, but nothing works.
Please help.
find which task is depending on assembleDebug task
changing the following did the trick for me at least:
from:
task findbugs(type: FindBugs, dependsOn: assembleDebug)
to:
task findbugs(type: FindBugs, dependsOn: "assembleDebug")
so just surrounding the task with quotes was enough.
It's from In that case, a workaround is this way:
//assembleDebug.doFirst {
// println '=============assembleDebug============='
//}
//assembleRelease.doFirst {
// println '=============assembleRelease============='
//}
//
// =======>
tasks.whenTaskAdded { task ->
if (task.name == 'assembleDebug') {
//task.dependsOn 'checkstyle', 'findbugs', 'pmd', 'lint'
println '=============assembleDebug============='
} else if (task.name == 'assembleRelease') {
//task.dependsOn 'checkstyle', 'findbugs', 'pmd', 'lint'
println '=============assembleRelease============='
}
}
If you have no any "assemble" in your project, so check an answer from this post:
Could not get unknown property 'assemble'

gradle plugin that used data from other plugin declared in main build

How to use aplicationId from
apply plugin: PublishApkToUpdateServerPlugin
version = '1.0'
class PublishApkToUpdateServerPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('uploadapk') << {
logger.lifecycle("appId ${android.defaultConfig.applicationID}");
}
}
}
my problem is here android.defaultConfig.applicationID
What went wrong:
Execution failed for task ':uploadapk'.
Could not find property 'android' on task ':uploadapk'.
How to use properties declared at other plugin and used in main build script?
I'd guess you are missing a namespace in front of uploadapk like
project.task('com.externalinc:uploadapk')
and its trying to use android:uploadapk instead. Try playing around with it.

Categories

Resources