Android Studio how to run gradle sync manually?
Asked Answered
O

14

254

I'm debugging Gradle issues in Android Studio and see references to "Run gradle sync", but I'm not sure how to run this command.

How do I run "Gradle sync" from Android studio or Mac terminal?

Octosyllable answered 10/4, 2015 at 15:29 Comment(2)
If you came here for the "...or Mac terminal" you are out of luck. None of these answers are about syncing Android Studio itself from command line.Haystack
See @Cai's answer belowCreaturely
B
398

Android studio should have this button in the toolbar marked "Sync project with Gradle Files"

Or:

enter image description here

Or on 3.3.1:

enter image description here

Or in Android Studio Hedgehog with the new UI: enter image description here

OR by going to File -> Sync Project with Gradle Files from the menubar.

Borosilicate answered 10/4, 2015 at 15:34 Comment(9)
missing in Adnroid Studio 3.0.1Nahama
@RomanM It's not missing... It moved to "File"... So, it is next to save iconManzanilla
I don't see that button in the toolbar or File menu in Android Studio 4.Bufordbug
I can confirm the sync button appears on the toolbar in Android Studio 4.0.1Jeremiahjeremias
Why this question has so many upvotes if it is not answering OP's question?Morava
Is there a command that does this function? Can I run ./gradlew <something>?Therefore
The real question is: why do the people in charge of Android Studio keep changing this functionality? It's clearly a case of fixing something that's not broken.Wristwatch
@Wristwatch Or better yet, why isn't there a Context Menu option so that we don't have to memorize the iconset each time?Stumpage
@Stumpage that's even better! +1Wristwatch
T
67

WARNING: --recompile-scripts command has been deprecated since gradle's version 5.0. To check your gradle version, run gradle -v.

./gradlew --recompile-scripts

it will do a sync without building anything.


Alternatively, with command line in your root project

./gradlew build

It will sync and build your app, and take longer than just a Gradle sync

To see all available gradle task, use ./gradlew tasks


OR USING THE BUTTON

gradle icon sync

Tenuto answered 10/4, 2015 at 15:35 Comment(5)
Yes but build does more right? What is the difference between sync and build?Windage
build is a very generic task. it's like using a sledgehammer to crack a nut. On Android projects, running the "build" task could take forever!Cyclamen
I use com.android.tools.build:gradle:3.5.1,and it has no option --recompile-scriptsCallahan
since gradle 5.0, --recompile-scripts has become an unknow command-line option,Whittington
I don't want to build, I only want to sync, I don't understand why Google would deprecate and remove --recompile-scripts without introducing an alternative ...Inenarrable
S
43

In Android Studio 3.3 it is here:

enter image description here

According to the answer https://mcmap.net/q/50288/-where-is-a-quot-sync-project-with-gradle-files-quot-button-in-android-studio-3 in Android Studio 3.1 it is here:

enter image description here

This command is moved to File > Sync Project with Gradle Files.

enter image description here

Singer answered 30/3, 2018 at 14:47 Comment(6)
ROFL! I have been looking for 2 days for this button! Thanks!Perpetuate
@swooby, you are welcome. I also was finding it for some time, even asked a question, and they helped.Singer
This option appears to no longer exist? i.imgur.com/cNFUoxB.png (screenshot just taken, in Android Studio 3.5.3)Ideational
@Venryx, it is strange. I have 3.6.3 and this menu still exists (imgur.com/a/F8FCpvk). Anyway, it is better to use a button in the Toolbar.Singer
It was apparently a problem with some AndroidStudio/Gradle files being outdated. After deleting them and reimporting the project, the option showed up again.Ideational
@Venryx, thanks for the explanation! Sometimes custom buttons disappear from the Toolbar (and then appear again).Singer
T
25

Keyboard shortcut lovers can add a shortcut for running gradle sync manually by going to File -> Settings -> Keymap -> Plugins -> Android Support -> Sync Project with gradle files (Right click on it to add keyboard shortcut) -> Apply -> OK and you are done. Gradle Sync keyboard shortcutChoose any convenient key as your gradle sync shortcut which doesnot conflict with any other shortcut key, (I have choosen Shift + 5 as my gradle sync key), so next when you want to run gradle sync manually just press this keyboard shortcut key.

Thornberry answered 15/4, 2015 at 6:22 Comment(3)
it's 'Android Studio->Preferences' for the first two steps on Mac OS XDorweiler
+1 the best answer to questionAranda
Best answer, No need to find Sync Project icon from similar icons of android studio. Just press Shift+5 and you are done.Ec
H
14

I presume it is referring to Tools > Android > "Sync Project with Gradle Files" from the Android Studio main menu.

Hassanhassell answered 10/4, 2015 at 15:31 Comment(0)
P
7
gradle --recompile-scripts

seems to do a sync without building anything. you can enable automatic building by

gradle --recompile-scripts --continuous

Please refer the docs for more info:

https://docs.gradle.org/current/userguide/gradle_command_line.html

Prototrophic answered 17/1, 2017 at 16:51 Comment(2)
Also noted here: docs.gradle.org/current/userguide/upgrading_version_4.htmlHercules
gradle 5.1.1 do not have option --recompile-scriptsWhittington
K
1

Shortcut (Ubuntu, Windows):

Ctrl + F5

Will sync the project with Gradle files.

Kozlowski answered 25/3, 2016 at 8:25 Comment(1)
on MacOS shortcut is not defined, one should assign it. Described below https://mcmap.net/q/50243/-android-studio-how-to-run-gradle-sync-manuallyEosinophil
G
1

From terminal:

gradle prepareKotlinBuildScriptModel

NOTE: you can see what android studio is doing by inspecting the "Build" tab. There will be difference windows inside there. One should be "Sync"

Glabella answered 18/6, 2021 at 15:5 Comment(1)
It does something, like downloading the gradle version, but when I open the project in Android Studio afterwards, it still needs to sync.Inenarrable
M
0

I think ./gradlew tasks is same with Android studio sync. Why? I will explain it.

I meet a problem when I test jacoco coverage report. When I run ./gradlew clean :Test:testDebugUnitTest in command line directly , error appear.

Error opening zip file or JAR manifest missing : build/tmp/expandedArchives/org.jacoco.agent-0.8.2.jar_5bdiis3s7lm1rcnv0gawjjfxc/jacocoagent.jar

However, if I click android studio sync firstly , it runs OK. Because the build/../jacocoagent.jar appear naturally. I dont know why, maybe there is bug in jacoco plugin. Unit I find running .gradlew tasks makes the jar appear as well. So I can get the same result in gralde script.

Besides, gradle --recompile-scripts does not work for the problem.

Mae answered 23/7, 2019 at 7:27 Comment(1)
Hi Victor, I have a trouble may proof you wrong, Here is what I got, I set a configProductID=11111 from my gradle.properties, from my build.gradle, I add "resValue "string". If I do a build clean from ADT, the resource R.string.ProductID can be generated. Then I can do gradlew assembleDebug to build. Then I change my config name from "ProductID" to "myProductID", I will get error to do 'gradlew tasks', also I can't do 'gradlew clean', all of them just fail. I am still struggling. As ".\gradlew.bat --recompile-scripts " seems no longer exists?Grave
G
0

I have a trouble may proof gradlew clean is not equal to ADT build clean. And Now I am struggling to get it fixed.

Here is what I got: I set a configProductID=11111 from my gradle.properties, from my build.gradle, I add

resValue "string", "ProductID", configProductID

If I do a build clean from ADT, the resource R.string.ProductID can be generated. Then I can do bellow command successfully.

gradlew assembleDebug

But, as I am trying to setup build server, I don't want help from ADT IDE, so I need to avoid using ADT build clean. Here comes my problem. Now I change my resource name from "ProductID" to "myProductID", I do:

gradlew clean

I get error

PS D:\work\wctposdemo> .\gradlew.bat clean

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\work\wctposdemo\app\build.gradle'

* What went wrong:
Could not compile build file 'D:\work\wctposdemo\app\build.gradle'.
> startup failed:
  General error during semantic analysis: Unsupported class file major version 57

If I try with:

.\gradlew.bat --recompile-scripts

I just get error of

Unknown command-line option '--recompile-scripts'.
Grave answered 9/1, 2020 at 8:31 Comment(2)
I've the same error-message some minutes ago: > Unsupported class file major version 57 The issue for me was, that gradle doesn't work with current openJdk13 on Linux (I use Manjaro... an Arch-Like Linux). So to fix this, I've done this: I've checked which Java version I've installed at my pc, and choosed the version 8: sudo archlinux-java stauts sudo archlinux-java set java-8-openjdk Maybe this helping you out, even If your system seems to be windows. So try another SDK Version. Maybe it also solve your issue.Galyak
Hi @suther, many thanks for your reply, I have find the root cause and fixed, I don't think it is because of JAVA version. But as I described "gradlew clean" is not equal to "ADT clean". My fix was simply do "gradlew :app:dependencies", this will generate resource IDs from gradle configuration files.Grave
L
0

The shortcut on Windows is Ctrl + Shift + o

In the main menu it is under File -> Sync Project with Gradle Files

I am using Android Studio Giraffe | 2022.3.1 Patch 2

Lycopodium answered 18/10, 2023 at 6:2 Comment(0)
C
0
gradlew assembleDebug

will sync and download all dependencies when not cached yet, and android studio and intellij idea will follow the thread started by above command

Conceit answered 28/10, 2023 at 7:50 Comment(0)
P
0

Without considering UI:

./graduate -- refresh dependencies

or Considering Android Studio UI synchronization(only macOS):

Osascript -e 'tell application' System Events' to tell process' Android Studio 'to click menu item' Sync Project with Gradle Files' of menu 'File' of menu bar 1'

Make it a gradle task in project's build.gralde file:

tasks.register('GradleSync', Exec) {
    commandLine 'bash', '-c', 'osascript -e \'tell application "System Events" to tell process "Android Studio" to click menu item "Sync Project with Gradle Files" of menu "File" of menu bar 1\''
}

in the Terminal:

./gradlew GradleSync
Pyoid answered 6/4, 2024 at 10:2 Comment(0)
W
-1

Anyone wants to use command line to sync projects with gradle files, please note:

Since Gradle 5.0,

The --recompile-scripts command-line option has been removed.

Whittington answered 10/7, 2020 at 6:50 Comment(2)
That's great, but how about offering the proper alternative.Hyperphagia
Haven't found one, pityWhittington

© 2022 - 2025 — McMap. All rights reserved.