Gradle Build Running in Android Studio for too long - android

Whenever I Start or run my project the gradle build running never stops.I have tried creating new project nothing is happening. Some projects are working fine but some of them are not and taking forever in gradle build running. Also whenever I open the project this cmd appt.exe file opens.

Open the terminal tab and try building the project manually gradle --stop clean build --stacktrace
The --stop param will kill any running instances of the gradle daemon. Its also possible gradle ran into a .lock file that is blocking its execution. This can happen if a build is terminated before completion. When you run on the command line it may take as long as 3 minutes before gradle gives up and fails. The error message provided by --stacktrace should tell you where the file causing the problem is.
Just delete the lock file: rm <pathToLockFile> then rebuild. This could happen a few times before the build completes.
It would be nice if there was a way to clean up all locks but to my knowledge this doesn't exist.

Related

How to run 'rebuild project' from the command line

Some Context:
I am working on a Continuous Integration pipeline with MSBuild for a large project that includes an Android application. This application includes some automatically generated files that need to be integrated into the project regularly. This is usually accomplished by manually copying them over and selecting Build->rebuild project in android studio.
This step is necessary since the project also makes use of AndroidAnnotations, which generates classes at compile time.
The Problem:
I haven't found any documentation for what is run when you select 'rebuild project' in android studio.
What I Have Tried:
How to Rebuild and Run android project from command line
as well as:
What is happened after I run rebuild project in Android studio?
But the doccumentation here doesn't talk about the rebuild command, and a 6 week udacity course isn't a helpful resource either.
When I hit 'rebuild project' the Android Studio event log reads:
Executing tasks: [clean, :app:generateDevelopmentDebugSources, :app:generateDevelopmentDebugAndroidTestSources, :app:prepareDevelopmentDebugUnitTestDependencies, :app:mockableAndroidJar, :app:compileDevelopmentDebugSources, :app:compileDevelopmentDebugAndroidTestSources, :app:compileDevelopmentDebugUnitTestSources]
Which I have run in the command line as follows:
gradlew.bat clean :app:generateDevelopmentDebugSources :app:generateDevelopmentDebugAndroidTestSources :app:prepareDevelopmentDebugUnitTestDependencies :app:mockableAndroidJar :app:compileDevelopmentDebugSources :app:compileDevelopmentDebugAndroidTestSources :app:compileDevelopmentDebugUnitTestSources
The command executes, but the build finishes with several hundred errors, which also occur when you try to compile or clean without first running rebuild. Obviously this command is incomplete.
When I look at the Gradle Console in Android Studio I see a couple of dozen targets that are run, but I'm hesitant to copy-paste those, as I have no idea what small changes to the project will necessitate different targets to be run.
I've narrowed down my issue to the :app:compileDevelopmentDebugUnitTestJavaWithJavac target. This target requires one of the other targets listed in the Gradle Console, but I don't know which one.
Those targets have to be coming from somewhere, but where?
tl;dr:
How can I do all the things that Android Studio does when I run 'rebuild project' from the command line?
Please open build view in Android Studio, when you select from menu: Build -> Rebuild Project on the top in build view you will see which tasks were triggered by Android Studio: Executing tasks: [clean, :assembleDebug] in project ...
And now you can try to do this from command line:
./gradlew clean assembleRelease
Rebuild and build are the same thing. You can list all the gradle tasks by running a command gradle task --list or gradle -q :tasks --all . There you can see different types of gradle tasks. For example, install tasks, build tasks, assemble tasks, publishing tasks, verification tasks, build setup tasks, clean tasks, android task, or your own custom named tasks etc. Run on the build tasks from your module (e.g., MyModule:build) like this gradle MyModule:build (or ./gradlew MyModule:build if you wanna use inside gradle wrapper)
Note: you can see your runnable/buildable module from Run Configurations also.

How do I run the same Gradle daemon for terminal and Android Studio?

I noticed that the Gradle uses a separate daemon than the terminal does, and it uses the gradlew (wrapper) to run its own instance of Gradle daemon.
I noticed this when i tried to stop Gradle task in Android Studio by the command gradle --stop. it says there is no daemon running. but when I use ./gradlew --stop it stops the Gradle task. It's like I'm having two daemons running one for Android Studio and one for the terminal.
How can I make the Android Studio to use the same daemon that the terminal is using (when I start a Gradle task by gradle build command, the terminal starts a daemon and uses it).
How can I do this?
I found the answer myself by testing many ways.
the point is when u use Gradle wrapper it will start a new daemon for every project even if they are with the same version you can stop using Gradle wrapper by one change in Android Studio so it uses system Gradle daemon (not using wrapper and running a new daemon for each project)
you need to go to "Settings ->Build, Execution, Deployment -> Build tools -> Gradle" then check the radio button "Use local Gradle distribution" and address the "Gradle Home" field to the system Gradle_Home installation.
though you need to have already Gradle installed on your system.
for me its directory is: "/home/[my_user]/.gradle/wrapper/dists/gradle-3.1-all/ejgglywf033yp6s4x8ahmgm74/gradle-3.1"
the good point about this is you will have just one instance of Gradle Daemon (starting daemons takes time - especially if you are working on multiple project at the same time like me), and the negative point is you will have to change the directory manually when you want to migrate to new Gradle version distributes.
I dont know why but with this method the Gradle syncs faster on my pc. let me know if its also faster in your computer.
It was a lot of work but I was able to find a better solution than using the local gradle distribution
Please have a look at my bug report where everything is explained and hopefully this will be solved by Google or Gradle in a future release
https://issuetracker.google.com/issues/68374709

How to fix transformClassesWithInstantRunForDebug in gradle

I have Android application which was working fine but few hours ago it started showing me error on Run 'app' click in Android Studio:
Error:Execution failed for task ':app:transformClassesWithInstantRunForDebug'.
> org.objectweb.asm.tree.analysis.AnalyzerException: Execution can fall off end of the code
If I build app from console like gradle assembleRelease everything works fine.
So my question is how to fix this, what could cause this problem. It drives me crazy, because I can't start my app only from Android Studio.
Btw: I already tried to google it and a lot of answers about multidex problem but I guess it's not an option in my case.
Delete build dir
Try deleting the contents of your build directory, and then rebuilding.
appName/build and/or appName/app/build
Alternatively, clean your build
A less brute force option to try is to clean the build.
Build -> Clean Project or Build -> Rebuild Project

Run "Instant Run" from command line

I'm running my Android app from Android Studio, and Instant Run kicks in, which is great. I want to run the exact same thing from the command-line.
In Android Studio's Event Log, I see
22:00:28 Executing tasks: [:app:incrementalDevDebugSupportDex]
22:00:48 Gradle build finished in 20s 286ms
22:00:51 Instant Run applied code changes and restarted the current Activity.
So, I expected I would be able to run that task from the command line: ./gradlew :app:incrementalDevDebugSupportDex. However, it's not found:
FAILURE: Build failed with an exception.
* What went wrong:
Task 'incrementalDevDebugSupportDex' not found in project ':app'.
Is there a way to run with Instant Run from the command line?
It is currently not possible to use instant run outside of Android Studio. This was mentioned at the Android Dev Summit, but I can't recall exactly in which talk that was.
You can run instant-run from command line like this:
./gradlew clean :sample-app:assembleDebug -Pandroid.optional.compilation=INSTANT_DEV -Pandroid.injected.build.api=24
As of now it is possible. You can run the below command to make an instant run :
./gradlew :app:incremental"buildvarientname"DebugJavaCompilationSafeguard
You can add the same in gradle task also by adding incremental"buildvarientname"DebugJavaCompilationSafeguard inside the task on the run configuration window,

Does Android Studio keep another version of Gradle?

Here is my Gradle setting:
On my developing app, I usually click the button below and everything goes well.
I want to write a script to auto run my build every day.
I tried gradlew assembleRelease and /Applications/Android\ Studio.app/Contents/gradle/gradle-2.4/bin/gradle assembleRelease but it start downloading all dependencies, I have bad network so I can't run the build.
Obviously I have downloaded all the dependencies before because I could click that "run" button and launch my app. It seems Android Studio keep a gradle cache itself and provider another gradlew program.
How could I build my app from the command line with my bad network. Thanks!

Categories

Resources