简体   繁体   English

Android Studio 如何手动运行 gradle 同步?

[英]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.我正在 Android Studio 中调试 Gradle 问题并查看对“运行 gradle 同步”的引用,但我不确定如何运行此命令。

How do I run "Gradle sync" from Android studio or Mac terminal?如何从 Android Studio 或 Mac 终端运行“Gradle sync”?

Android studio should have this button in the toolbar marked "Sync project with Gradle Files" Android studio 的工具栏中应该有这个按钮,标记为“Sync project with Gradle Files”

EDIT: I don't know when it was changed but it now looks like this:编辑:我不知道它是什么时候改变的,但现在看起来像这样:

在此处输入图片说明

EDIT: This is what it looks like on 3.3.1编辑:这就是它在 3.3.1 上的样子在此处输入图片说明
OR by going to File -> Sync Project with Gradle Files from the menubar.或者通过从菜单栏中转到File -> Sync Project with Gradle Files

WARNING : --recompile-scripts command has been deprecated since gradle 's version 5.0.警告:-- --recompile-scripts命令自gradle版本 5.0 起已被弃用。 To check your gradle version, run gradle -v .要检查您的gradle版本,请运行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它将同步和构建您的应用程序,并且比 Gradle 同步花费的时间更长

To see all available gradle task, use ./gradlew tasks要查看所有可用的 gradle 任务,请使用./gradlew tasks

In Android Studio 3.3 it is here:在 Android Studio 3.3 中,它在这里:

在此处输入图片说明

According to the answer https://stackoverflow.com/a/49576954/2914140 in Android Studio 3.1 it is here:根据Android Studio 3.1中的答案https://stackoverflow.com/a/49576954/2914140 ,它在这里:

在此处输入图片说明

This command is moved to File > Sync Project with Gradle Files .此命令移至File > Sync Project with Gradle Files

在此处输入图片说明

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.键盘快捷键爱好者可以通过转到 File -> Settings -> Keymap -> Plugins -> Android Support -> Sync Project with gradle files(右键单击它添加键盘快捷键) -> Apply - 添加手动运行 gradle 同步的快捷方式> 好的,你完成了。Gradle 同步键盘快捷键 Choose 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.选择任何方便的键作为您的 gradle 同步快捷键,不会与任何其他快捷键冲突,(我选择了 Shift + 5 作为我的 gradle 同步键),所以接下来当您想手动运行 gradle 同步时,只需按此键盘快捷键即可。

我认为它是指 Android Studio 主菜单中的“工具”>“Android”>“与 Gradle 文件同步项目”。

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 https://docs.gradle.org/current/userguide/gradle_command_line.html

Shortcut (Ubuntu, Windows):快捷方式(Ubuntu、Windows):

Ctrl + F5

Will sync the project with Gradle files.将项目与 Gradle 文件同步。

I think ./gradlew tasks is same with Android studio sync .我认为./gradlew tasksAndroid studio sync相同。 Why?为什么? I will explain it.我会解释的。

I meet a problem when I test jacoco coverage report.我在测试 jacoco 覆盖率报告时遇到问题。 When I run ./gradlew clean :Test:testDebugUnitTest in command line directly , error appear.当我直接在命令行中运行./gradlew clean :Test:testDebugUnitTest时,出现错误。

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.但是,如果我先单击 android studio sync ,它运行正常。 Because the build/../jacocoagent.jar appear naturally.因为build/../jacocoagent.jar自然出现。 I dont know why, maybe there is bug in jacoco plugin.我不知道为什么,也许 jacoco 插件中存在错误。 Unit I find running .gradlew tasks makes the jar appear as well.我发现运行.gradlew tasks单元也会使 jar 出现。 So I can get the same result in gralde script.所以我可以在 grade 脚本中得到相同的结果。

Besides, gradle --recompile-scripts does not work for the problem.此外, gradle --recompile-scripts对这个问题不起作用。

I have a trouble may proof gradlew clean is not equal to ADT build clean.我有一个麻烦可能证明 gradlew clean 不等于 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这是我得到的:我从我的 gradle.properties,从我的 build.gradle 设置了一个 configProductID=11111,我添加

resValue "string", "ProductID", configProductID

If I do a build clean from ADT, the resource R.string.ProductID can be generated.如果我从 ADT 执行构建清理,则可以生成资源 R.string.ProductID。 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.但是,当我尝试设置构建服务器时,我不需要 ADT IDE 的帮助,所以我需要避免使用 ADT build clean。 Here comes my problem.我的问题来了。 Now I change my resource name from "ProductID" to "myProductID", I do:现在我将资源名称从“ProductID”更改为“myProductID”,我这样做:

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

Anyone wants to use command line to sync projects with gradle files, please note:任何想使用命令行同步项目与 gradle 文件的人,请注意:

Since Gradle 5.0,从 Gradle 5.0 开始,

The --recompile-scripts command-line option has been removed. --recompile-scripts命令行选项已被删除。

From terminal:从终端:

gradle prepareKotlinBuildScriptModel

NOTE: you can see what android studio is doing by inspecting the "Build" tab.注意:您可以通过检查“构建”选项卡来查看 android studio 正在做什么。 There will be difference windows inside there.里面会有差异窗口。 One should be "Sync"一个应该是“同步”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM