Whats the idea behind task clean on gradle android projects - android-gradle-plugin

When I create a new project on android studio I see in gradle.build:
task clean(type: Delete) {
delete rootProject.buildDir
}
Whats the idea behind it?

In this way the clean task (with the type=delete) deletes the build directory when it runs.
It is useful when you modify some config files like the settings.gradle which can requires a complete clean.

Related

Gradle tasks are not visible in new version of Android Studio

I have a project made with Android Studio 3.0 with coworkers. There are so many Tasks for installing the application. After I finished updating the Android Studio from 3.0 to 3.3.2, almost tasks in project(root)>tasks are not visible. Checkout my gradle project below.
3.0 :
my-project
ㄴmy-project (root)
ㄴTasks
ㄴandroid
ㄴbuild
ㄴbuild setup
ㄴhelp
ㄴinstall
ㄴother
ㄴverification
ㄴmy-module-one
ㄴTasks
ㄴandroid
ㄴbuild
ㄴhelp
ㄴinstall
ㄴother
ㄴverification
ㄴmy-module-two
...
3.3.2 :
my-project
ㄴmy-project (root)
ㄴTasks
ㄴbuild setup
ㄴhelp
ㄴother
ㄴmy-module-one
ㄴTasks
ㄴandroid
ㄴbuild
ㄴcleanup
ㄴhelp
ㄴinstall
ㄴother
ㄴverification
ㄴmy-module-two
...
I can check some tasks are not visible and suddenly the "cleanup" thing appears in every modules. How can i solve it and What's the reason of these problems.
Had the same problem: after import from GitHub there weren't any Gradle tasks in Android Studio. Spent hours on solving this issue but nothing helped. Only these steps solved problem in my case:
In Android Studio go to File -> Settings... -> Experimental and uncheck the Do not build Gradle task list during Gradle build checkbox.
Delete cloned project and clone it again (if some of required files like keystore properties are not present, add them and do File -> Invalidate Caches / Restart).
Android Studio 4.2 has a new experimental feature for Gradle, which is Do not build Gradle task list during Gradle sync. Unfortunately, it comes checked (on) by default, and results in the Gradle task list not being visible in the Gradle view. This because the Gradle task list is not even being generated. However, it leaves many of us wondering what happened to the Gradle task list.
You can go to Settings (e.g., File -> Settings on windows, Preferences -> Settings on mac) and go to Experimental. There you can uncheck the new option.
Then just click on Sync Project with Gradle Files and the Gradle task list will be generated and visible again.
Accroding to this link
Behavior changes:
Lazy task configuration: The plugin now uses Gradle’s new task creation API to avoid initializing and configuring tasks that are not required to complete the current build (or tasks not on the execution task graph). For example, if you have multiple build variants, such as “release” and “debug” build variants, and you're building the “debug” version of your app, the plugin avoids initializing and configuring tasks for the “release” version of your app.
Calling certain older methods in the Variants API, such as variant.getJavaCompile(), might still force task configuration. To make sure that your build is optimized for lazy task configuration, invoke new methods that instead return a TaskProvider object, such as variant.getJavaCompileProvider().
If you execute custom build tasks, learn how to adapt to Gradle’s new task-creation API.

IDEA 2016.1, Gradle's processResources expanding not working automatically

I have an IDEA 2016.1 Enterprise and a Gradle 2.12 multi-module project. In one of the modules, in src/main/resources, I have a file which I would like Gradle to 'expand', here is my configuration:
processResources {
filesMatching('my.properties') {
expand(project.properties)
}
}
(I would like to expand just this single file, and just copy the rest.)
It all works fine when built on the command line, but not by default in IDEA - when I clean and build the project, the file lands in build/resources/main but the placeholders are not replaced. I have to manually invoke the Gradle processResources task using the Gradle pane in IDEA and double clicking on the task.
Is this something I should report to Jetbrains (i.e. a bug) or has anybody have it working and I should change something in my configuration?
When you build from command line, you are using gradle. However, when you build the project from intellij, by default intellij doesn't use gradle to build, but use its internal build system which doesn't understand your gradle's processResources.
One way to solve it is to check "Delegate IDE build/run actions to gradle" as shown below:
If you don't want to use gradle build in intellij, there's another workaround - add processResources as a gradle task to run after build in your "Run/Debug Configurations":
Try adding the dependency in your build.gradle file, eg.
assemble.dependsOn processResources
This should work if you have java plugin applied.

How to get IntelliJ to associate Gradle sources with build.gradle?

When writing Gradle scripts for my Java project, specifically, when writing build.gradle files, IntelliJ does not recognize the Gradle API.
For instance, Gradle methods calls like apply, dependencies configure appear with a black line under them and it is not possible to navigate to method declarations, there is no auto-completion etc.
I managed to work around this by adding compile gradleApi() to the build's dependencies block. However, I don't want to have this explicit dependency in my code.
I tried editing IntelliJ's project structure and add a dependency on a Gradle library (tried gradle-core and gradle-all) to my modules, but that seems to have no effect.
Is there a way to make IntelliJ associate all build.gradle files with the Gadle sources?
I solved this problem as follows:
As mention in already posted answers, configure gradle
update gradle/wrapper/gradle-wrapper.properties file
change bin to all in distributionUrl i.e.
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
OR
[optional] If you are using old version of gradle wrapper and wanted to upgrade, then execute
./gradlew wrapper --gradle-version 6.8.3 --distribution-type all
Update gradle task (if present in build file)
wrapper {
gradleVersion = '6.8.3'
distributionType = Wrapper.DistributionType.ALL
}
Before importing the project to IntelliJ-Idea IDE, update build.gradle and add java and idea plugin to the plugins list
plugins {
id "java-library"
id "idea"
}
From a terminal, execute ./gradlew clean build idea or simply ./gradlew idea
Import project to IntelliJ idea.
Go to Preferences --> build,Execution,Deployment --> BuildTools --> Gradle
You can see
Restart IntelliJ idea IDE.
So above we have configured both of the options so choose either of them, except the specified location option. That's it.
Before
After
Autocomplete functionality as mentioned in this answer.
I had similar frustrations with Grails 3, which defines and runs a wrapper task when an app is created. Changing to the "all" zip in the wrapper properties file did not work because this kept getting changed back to the "bin" zip.
This was solved when it was understood that the "gradle-wrapper.properties" file simply stores the values from the "wrapper" task, and if this task is run after the properties are changed, they get changed right back.
This is easily fixed by setting some properties on the wrapper task:
wrapper.gradleVersion='3.2.1'
wrapper.distributionType=Wrapper.DistributionType.ALL
Now importing the project into IDEA gives you smart editing of your build.gradle.
when I choose build.gradle in IDEA and open it, IDE prompts
You can configure Gradle wrapper to use distribution with sources. It will provide IDE with Gradle API/DSL documentation.
I choose Ok, apply suggestion!
after project refreshing I am able to use code completion
before you import your project, configure it to use the customizable gradle wrapper as per the instructions here :-
https://docs.gradle.org/current/userguide/gradle_wrapper.html
add a task to your top level project like this:-
task wrapper(type: Wrapper) {
println "Wrapper gradleVersion = '2.12'"
gradleVersion = '2.12'
}
or whatever the latest version is.
make sure you can build the project from the gradle command line before you try importing into intelliJ, using the ./gradlew command, which will download and install a gradle distribution for you the first time you build.
set your java home, intelliJ home and gradle home variables in your machine and in intelliJ (mine look like this, yours may be different depending on your setup and your history of hacking around your machine...:-
(from .bashrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
)
When you do import, choose the customisable gradle wrapper. if all is well, when you open the top level build.gradle for your project, you will be asked to configure sources for the gradle dsl, which will also update your gradle wrapper properties file to this:-
#Thu Mar 31 14:04:00 BST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
.. the change being from ... bin.zip to all.zip. and that's it. This had been giving me lots of grief for a long time, but that's the way to do it. (on IntelliJ IDEA 2016.1 CE at least...)
most of this was in
Dimitry's answer too, but I couldn't get it to work using the default wrapper , it had to be the customisable wrapper.

Attaching Gradle sources in IntelliJ IDEA

Once after I create a Gradle project in IntelliJ using the default gradle wrapper and create directories option I see the project structure gets created with build.gradle file.
IntelliJ tips me to "You can configure Gradle wrapper to use distribution with sources. It will provide IDE with Gradle API/DSL documentation" - but I am not able to attach the sources even after clicking "Ok, apply suggestion". The Gradle project is getting refreshed but the sources are not attached.
We are using a Nexus repository.
To improve on #anon58192932 answer you can use only gradleVersion and distributionType fields of Wrapper task and don't need to manually create distributionUrl which is more error prone since you could change gradle version in one place, but not in the other.
task wrapper(type: Wrapper) {
gradleVersion = '4.2'
distributionType = Wrapper.DistributionType.ALL
}
#edit
gradle 4.8+ will produce error for above. Use instead
wrapper {
gradleVersion = '4.2'
distributionType = Wrapper.DistributionType.ALL
}
Sounds like some IntelliJ problem. To do this manually, change gradle-bin to gradle-all in $projectDir/gradle/wrapper/gradle-wrapper.properties.
Peter's answer is not correct. The gradle-wrapper.properties and gradle-wrapper.jar files are generated by the 'wrapper' task which is shown in IntelliJ as a build task to perform.
When wrapper is performed it will build out the .jar and .properties file based on your settings therefore you should NOT be editing these files manually as they are GENERATED.
You can call the wrapper task manually very easily in your project folder: ./gradlew wrapper for *nix platforms. This will generate updated gradle-wrapper.properties and gradle-wrapper.jar files for your project.
If you want to specify to use all sources or not you can easily modify your main build.gradle file with the following:
allprojects {
task wrapper(type: Wrapper) {
gradleVersion = '4.1'
distributionUrl = 'https://services.gradle.org/distributions/gradle-4.1-bin.zip'
}
}
Substitute gradle-4.1-bin.zip for gradle-4.1-all.zip to include gradle sources, documentation, and examples.

How to attach sources to auto-generated Gradle-based dependencies in IntelliJ IDEA 13.0 in a way that will survive next Gradle projects refresh?

Is there a simple way to attach sources to auto-generated Gradle-based dependencies with IntelliJ IDEA 13.0 that won't be erased on next Gradle refresh?
For example, my build.gradle has such entry:
project(":projectName") {
dependencies {
compile files("c:/Program Files (x86)/groovy-2.2.1/embeddable/groovy-all-2.2.1.jar")
// more stuff here
}
}
Thus when I click Refresh all Gradle projects
I get a nice dependency set looking like so:
but there are no sources attached and if I do attach them manually, on next refresh they are erased.
I have sources for many different libraries, sometimes in jar file, sometimes directly in the file system (e.g. my groovy install has sources in c:\Program Files (x86)\groovy-2.2.1\src\).
Some of the dependencies I use can be downloaded from maven central repo, but in my build.gradle all the dependencies are configured to be taken from my local file system.
Thanks!
Konrad
The only easy solution is to get the dependencies straight from a Maven repository (either Maven Central or an inhouse repository). If that's not an option for you, you'll have to configure sources via a hook such as idea.module.iml.withXml or idea.module.iml.whenMerged (after applying the idea plugin to allprojects). You can find details on these APIs in the Gradle Build Language Reference and the Gradle User Guide.