Could not get unknown property 'assembleRelease' for project - android

After updating Android Studio to version 2.2 and the gradle plugin to 2.2.0, I get following error:
Error:(32, 1) A problem occurred evaluating project ':jobdispatcher'.
Could not get unknown property 'assembleRelease' for project ':jobdispatcher' of type org.gradle.api.Project.
The problem is in the build.gradle file of an imported jobdispatcher module:
task aar(dependsOn: assembleRelease)
What changes can I make to fix this?
Note, this issue is very similar to, but still a bit different to, that reported here.

Move your dependency dependsOn inside your gradle task like shown below:
task aar() << {
dependsOn 'assembleRelease'
}

Just add "" like this to fix your problem:
from:
task aar(dependsOn: assembleRelease)
to:
task aar(dependsOn: "assembleRelease")

I tried all the previous answers, all are not working. Here is the one working after gradle 2.2.
Starting from 2.2, those tasks also include "assembleDebug" and "assembleRelease". To access such tasks, the user will need to use an afterEvaluate closure:
afterEvaluate {
task aar(dependsOn: assembleRelease) {
//task
}
}

task aar {
....
}
aar.dependsOn('assembleRelease')
and task aar will run after task "assembleRelease" finished~
wish this will help you~ :-D

I had the same problem.
Disabling instant run under Android Studio/Preferences/Build, Execution, Deployment/Instant Run worked for me.

Related

What is type in Gradle task?

Hi, I'm almost new at Gradle in Android.
So.. I'm leanring the Gradle but there are many things that I don't understand.
task clean(type: Delete) {
println "task clean~~"
delete rootProject.buildDir
}
In project A when I input "gradlew" in Android Studio Terminal,
I can see below result.
C:\Users\xxxx\AndroidStudioProjects\ProjectA>gradlew
> Configure project :
task clean~~
(..snip..)
I have questions:
Why is the task clean executed?
Is it default task?
I couldn't find something like below code in the project's gradle file.
defaultTasks 'clean', 'run' .
What is type in task? (I saw type "Copy" too) .
Well task clean is a default task of gradle. It runs every time you start your android studio. To know the working of gradle you should read this gradle documentation which will surely help you in your questions:
Gradle Docs
And for the second point according to gradle documentation:
A Task represents a single atomic piece of work for a build, such as compiling classes or generating javadoc.
So actually it's a task not type, type is a general term and to be specific task type belongs to central types which are used in Gradle scripts. If you go through the documentation you will also find the type:Copy too.

Could not get unknown property 'assembleRelease' while using gradle 2.14.1

When using older version then gradle 2.14.1 then below statement compiled and
run successfully but after updating with gradle 2.14.1, its not working.
code:
assembleRelease.dependsOn 'increaseVersionCodeAndName'
Thanks,
Devang
You may change your task increaseVersionCodeAndName that it will depend on release like:
task increaseVersionCodeAndName() << {
//your code
dependsOn 'assembleRelease'
}
Instead of calling gradle assemble call your own gradle task increaseVersionCodeAndName

Android: Skip Gradle "testClasses" task for a dependency project

I have followed this guide to create a JUnit test file for my main Android module (let's call it "module-a"), in Android Studio v1.4.
My "module-a" has a dependency on an external library that is provided as a .aar file and for which I had to create a dedicated module.
This dependency causes an error:
When right clicking the test Java file and hitting "Run MyTestName" , it fails with this error
Error:Gradle:
FAILURE: Build failed with an exception.
* What went wrong:
Task 'testClasses' not found in project ':module-b'.
Removing the dependency on module-b solves the problem.
Excerpt of module-a build.gradle:
compile project(':module-b')
module-b build.gradle:
configurations.create("default")
artifacts.add("default", file('library-b.aar'))
How should I configure Gradle so that it does not try to run the testClasses task on "module-b" ? (this should solve my issue)
I did not find a way to skip the testClasses task for module-b: it seems that actions started from Android Studio (like running a JUnit test) run Gradle commands that cannot be modified. In my case:
Information:Gradle: Executing tasks:
[:module-a:prepareFree_flavorDebugUnitTestDependencies,
:module-a:generateFree_flavorDebugSources,
:module-a:mockableAndroidJar,
:module-a:assembleFree_flavorDebug,
:module-a:assembleFree_flavorDebugUnitTest,
:module-b:testClasses]
I found a workaround for my problem, though:
Add the following code to module-b build.gradle:
task testClasses {
doLast {
println 'This is a dummy testClasses task'
}
}

Gradle: Only resolve dependencies for the desired variant

I have a test app with three flavors:
dev: Uses a local copy of the library during development
qa: Uses a snapshot during QA
rc: Uses a prerelease build for release candidate testing.
dependencies {
devCompile project(':library')
qaCompile 'com.example:library:1.0.0-SNAPSHOT#aar'
rcCompile 'com.example:library:1.0.0#aar'
}
I run Gradle, and expect it to do the minimum amount of work necessary to build just what I want:
./gradlew :test-app:connectedAndroidTestDevDebug
However, the build fails, because it is trying to resolve dependencies for all build flavors, not just the one I am building.
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':test-app'.
Could not resolve all dependencies for configuration ':test-app:_qaDebugCompile'.
Could not find com.example:library1.0.0-SNAPSHOT.
Searched in the following locations:
https://repo1.maven.org/maven2/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
https://repo1.maven.org/maven2/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
https://repo1.maven.org/maven2/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
http://oss.sonatype.org/content/repositories/snapshots/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
http://oss.sonatype.org/content/repositories/snapshots/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
http://oss.sonatype.org/content/repositories/snapshots/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
file:/opt/android-sdk-macosx/extras/android/m2repository/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
file:/opt/android-sdk-macosx/extras/android/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
file:/opt/android-sdk-macosx/extras/android/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
file:/opt/android-sdk-macosx/extras/google/m2repository/com/example/library/1.0.0-SNAPSHOT/maven-metadata.xml
file:/opt/android-sdk-macosx/extras/google/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.pom
file:/opt/android-sdk-macosx/extras/google/m2repository/com/example/library/1.0.0-SNAPSHOT/library-1.0.0-SNAPSHOT.aar
Required by:
project-name:test-app:unspecified
The SNAPSHOT that the qa flavor is trying to resolve doesn't exist yet, and that should be fine, because I'm not trying to build the qa flavor. If that SNAPSHOT build is present, then everything works fine.
Questions:
Why are all build flavors having their dependencies resolved?
How can I accomplish building just one flavor without encountering this problem?
Is there some better way to do this that will be more "Gradley"?
I assume that the build works if you exclude the task manually?
gradle connectedAndroidTestDevDebug -x _qaDebugCompile
It looks like the task connectedAndroidTestDevDebug has a dependency on the task _qaDebugCompile, which causes your problem. I don't know how your tasks are defined, but you can study your dependencies using Gradle's built-in tasks gradle dependencies and gradle dependencyInsight. Maybe that will give you a hint in the right direction:
gradle dependencyInsight --dependency com.example:library:1.0.0-SNAPSHOT
You can read more about task dependencies in the gradle User's guide.
I finally solved this by checking the list of tasks and if there is a task for the desired build variant, adding the dependency.
dependencies {
gradle.startParameter.taskRequests.each { taskRequest ->
taskRequest.args.each { taskName ->
String flavorName = "qa";
if (taskName.toLowerCase().endsWith(flavorName+"debug") ||
taskName.toLowerCase().endsWith(flavorName+"release")) {
qaCompile 'com.example:my-library:1.0.0-SNAPSHOT#aar'
}
}
}
devCompile project(':localLibrary')
//qaCompile 'com.example:my-library:1.0.0-SNAPSHOT#aar' // What I used to do.
rcCompile 'com.example:my-library:1.0.0#aar'
}
Keep in mind that the task list will not contain dependent task names, so if you aren't invoking a task that contains your build flavor name, it won't work. This is just something I got working.

Android Studio - gradle task to execute after SYNC

Is there a way to execute gradle task once after project Sync with Gradle files is complete?
I've tried to set task dependency to preBuild, as I've seen gradle:build is triggered when Sync is executing. But the problem is that dependency doesn't seem to work, task is not executed and I have to manually start the task after each Sync.
This is basically what I've tried so far
apply plugin: 'com.android.library'
...
task myTask {
...
}
gradle.projectsEvaluated {
preBuild.dependsOn(myTask)
}
I've also tried to set task dependency to other tasks that I see are triggered (:generate{Something}), but that wasn't successful either.
Is there anything I can to do force the gradle task to be executed after each Sync?
I'm using Gradle 2.2.1 + Android Studio 1.0.2
Finally, I've managed to trigger the task on every Sync event.
Apparently gradle.projectsEvaluated is either not executed at all when syncing, or it is executed after build task, so the solution is to get rid of it completely
apply plugin: 'com.android.library'
...
task myTask {
...
}
preBuild.dependsOn(myTask)
Inside the Gradle menu (usually located on the upper right corner of Android Studio), there is a list of tasks. By right clicking on the task, it is possible to set Execute After Sync.
Some while ago JetBrains extended their idea gradle plugin and now you can write something like
idea.project.settings {
taskTriggers {
afterSync tasks.getByName("myTask")
}
}
You must apply the plugin, such as
plugins {
id "org.jetbrains.gradle.plugin.idea-ext" version "0.7"
}
according to docs:
A Gradle build has three distinct phases
Initialization...
Configuration During this phase the project objects are configured.
The build scripts of all projects which are part of the build are
executed.
Execution...
Our build.gradle files are executed during Configuration phase. Task is a class (we extend org.gradle.api.DefaultTask when developing them). So let's just call our task's execute method:
task myStandaloneTask(type: MyStandaloneTaskImpl){
println("myStandaloneTask works!")
}
// let's call our task
myStandaloneTask.execute()
task myInsideGradleTask {
println("myInsideGradleTask works!")
}
// let's call our task
myInsideGradleTask
where MyStandaloneTaskImpl is a Task developed in buildSrc or as Standalone project, details
P.S. No need to use parenthesis () after myInsideGradleTask because of Groovy

Categories

Resources