Alex Stone
Alex Stone

Reputation: 47348

Android Studio how to run gradle sync manually?

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?

Upvotes: 259

Views: 273868

Answers (16)

Gabriel Bento
Gabriel Bento

Reputation: 93

In AS Ladybug | 2024.2.1, on Windows you can press:

Ctrl+Shift+O

Or clicking in the UI gradle button: enter image description here

Upvotes: 0

Gits
Gits

Reputation: 16

Execute the task gradle :prepareKotlinBuildScriptModel, this is what Android Studio does when you 'Sync' a project if you're using Kotlin DSL for the gradle scrpts In Android Studio Koala(this is what I'm using currently, I don't know about other versions), you can 'Sync' your project using the shortcut Ctr + Shift + O, or navigate to Menu > File > Sync project with Gradle files

Upvotes: 0

GavinZhang
GavinZhang

Reputation: 1

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

Upvotes: 0

Hugo Gresse
Hugo Gresse

Reputation: 17899

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

Upvotes: 71

Cai
Cai

Reputation: 5323

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.

Upvotes: 409

Dimas Lanjaka
Dimas Lanjaka

Reputation: 294

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

Upvotes: 0

bobycom
bobycom

Reputation: 21

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

Upvotes: 0

jeffski13
jeffski13

Reputation: 627

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"

Upvotes: 1

Francis Bacon
Francis Bacon

Reputation: 4745

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.

Upvotes: -1

yunfei
yunfei

Reputation: 616

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'.

Upvotes: 0

Victor Choy
Victor Choy

Reputation: 4246

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.

Upvotes: 0

CoolMind
CoolMind

Reputation: 28793

In Android Studio 3.3 it is here:

enter image description here

According to the answer https://stackoverflow.com/a/49576954/2914140 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

Upvotes: 43

oc1d
oc1d

Reputation: 233

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

Upvotes: 7

Andrew
Andrew

Reputation: 37969

Shortcut (Ubuntu, Windows):

Ctrl + F5

Will sync the project with Gradle files.

Upvotes: 1

Amrit Pal Singh
Amrit Pal Singh

Reputation: 7132

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.

Upvotes: 25

CommonsWare
CommonsWare

Reputation: 1006674

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

Upvotes: 14

Related Questions