使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理

Android Studio 3.5 is released! Have you updated yet? Go to https://developer.android.com/studio, I will wait.

Personally, I'm most excited not by any new feature but by a bugfix which makes relevant again for android developers my open-source project for managing dependencies.

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 jmfayard / refreshVersions

Better dependencies management with Gradle, IntelliJ & Android Studio

de.fayard.buildSrcVersions

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理

The Gradle plugin de.fayard.buildSrcVersions goal is to make it as painless as possible to upgrade your project to the latest and greatest version of everything.

It does that by extracting all your dependencies, searching for available dependencies updates and generating Kotlin code, Groovy code or Gradle properties.

The plugin was called before de.fayard.buildSrcVersions, which still exists and is currently identical.

Installation

Edit your root build.gradle(.kts) file

We recommend to use the plugins block:

buildscript {
    //...
}
plugins {
  id("de.fayard.buildSrcVersions") version "0.7.0"
}
// Don't put any code before the buildscripts {} and plugins {} block

Updating Gradle is usually a good idea. You get less bugs, more features and more build speed, and it’s as simple as this:

$ ./gradlew wrapper --gradle-version 5.6.4

The task :buildSrcVersions

The task :buildSrcVersions is…

这个项目是由于对Android Studio的这个黑暗角落越来越厌恶而诞生的:

dle Gradle项目同步失败。 [再试一次]????

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理

“再试一次”这里有点用词不当。

Android Studio support for editing the Gradle files was bad ????

Android Studio is in general a magnificent IDE, and integration with Gradle is an essential component of it. So you would expect to have great tooling support for editing your build files.

而是,此错误消息是所有损坏的确切时间。 代替再试一次,其措辞应更像是:您在某处有一些错误,并且需要您自己解决。 祝你好运,告诉我你什么时候认为你完成了。”。

开发人员的体验变得更像JavaScript世界所期望的。 您可以从某个网站复制/粘贴一些东西,希望在正确的位置(这是不平凡的!)。 希望达到最佳效果,然后运行它,并抛出一条错误的错误消息。 再次重复。

为什么工具如此糟糕? 在javascript世界中,原因大致相同:我们正在使用动态语言(Groovy)做很多元编程工作(带有Gradle插件)。

Gradle has the right long-term plan to overcome this problem: write less Groovy and more Kotlin.

但是虽然Kotlin可能是未来,但Groovy是现在。 而且,我们需要一些渐进的进展,以利用现有的构建在这里和现在为我们提供帮助。

The Gradle "buildSrc" module ????

使用Android Studio 3.5时,可以使用Gradle“ buildSrc”模块。

“ buildSrc”是一个Gradle模块,您可以像往常一样编写Kotlin代码(具有完整的工具支持)。 然后,该代码可用于所有构建文件,而不是最终应用程序。 至关重要的是,IntelliJ IDEA和Android Studio对于从以下位置调用它具有良好的支持build.gradle

您可以将所有有点复杂或应该重复使用的东西放在那里,并保持您的build.gradle文件整洁。

The main use-case I had in mind was to replace those Groovy libraries.gradle files we used to write:

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理

这很简单,但是...我必须手动编写所有这些代码吗?

Laziness is one of the three great virtues of a programmer, so I wrote a plugin which asks Gradle which dependencies my project is using and let the KotlinPoet generate those files.

Gradle buildSrcVersions ????

通过编辑根目录安装插件build.gradle(.kts)像这样的文件:

该插件将任务添加到您的构建中,也称为:buildSrcVersions。

像这样运行它:

同步您的Gradle版本。

现在,您可以开始将魔术字符串替换为中提供的属性库

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理

最后,我们值得拥有的IDE工具:

  • 自动完成跳到定义...

但是,插件所做的更重要的一半是自动为您查找可用于所有依赖项的较新版本!

???? Search for dependencies updates

升级依赖性往往很乏味。 但是,正确管理它们确实很重要。 原因如下:

  • 很少有项目具有版本化的文档:主/README.md是他们的文档。 这意味着您可能会浪费时间尝试在您使用的版本中不起作用的代码片段。打开问题时,经常会问您:您能否以最新版本重现此问题?另一方面,您不想破坏您的应用程序,因为发生了重大变化,并且您也不必费心阅读CHANGELOG对其进行记录。

This plugin inherits from ben-manes/gradle-versions-plugin the feature to automatically determine which dependencies have updates.

当您决定要更新某些依赖项时,请再次运行插件:

文件Versions.kt用评论指示可用的新版本。

那时,您的构建完全相同。 承诺。

不要浪费时间手动找出可用的确切最新稳定版本。 取而代之,用它来决定是否要更新版本。 该格式经过优化,可以使用一个DELETE键进行更新:

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理

此截屏视频显示了此操作:

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理

完成后:

  • 同步您的Gradle版本运行单元测试,否则验证其是否有效。承诺。

Show me an example! ????

我向提交了拉取请求开发到的Android应用

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 Dependencies management with Gradle buildSrcVersions #44

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理
jmfayard commented on Aug 20, 2019

What type of PR is this?

  • [x] Refactor

Description

Hello, I refactored the Gradle build to use my plugin buildSrcVersions to manage dependencies.

See https://github.com/jmfayard/buildSrcVersions

See this article that I will publish once Android Studio 3.5 is out

https://dev.to/jmfayard/new-in-android-studio-3-5-dependency-management-with-gradle-buildsrcversions-22ia-temp-slug-2042205?preview=373ea29273e40c80e4579a999bbc305e8246511e13d599564be52fba5f20607b13305942fd8c4855b5e1807a66249612df27e19193a563106eb8d275

The files {Libs,Versions}.kt have been generated with the command

$ ./gradlew buildSrcVersions

Next Steps

Currently the versions in the dependencies are exactly the same as on master.

If you wish to update kotlin, edit Versions.kt

If you want to update Gradle, run $ ./gradlew wrapper

New in Android Studio 3.5 ???? ?

Let me backtrack a bit: the buildSrc is technically not a new thing. Gradle made it available a number of years ago and is widely used in JVM/IntelliJ projects. But it may well be new to you: the IDE integration that makes it a game changer launched in Android Studio 3.2. Without enough publicity. And then it got broken immediately after in Android Studio 3.3 and 3.4 with bug #123032843.

因此,如果您错过了新闻,可以为您辩解,现在是个尝试的好时机。

总结一下,我将在首次发布此故事后分享我最喜欢的反应:

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理

References ????

Github

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 jmfayard / refreshVersions

Better dependencies management with Gradle, IntelliJ & Android Studio

de.fayard.buildSrcVersions

使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理 使用Gradle buildSrcVersions在Android Studio 3.5中更好地进行依赖项管理

The Gradle plugin de.fayard.buildSrcVersions goal is to make it as painless as possible to upgrade your project to the latest and greatest version of everything.

It does that by extracting all your dependencies, searching for available dependencies updates and generating Kotlin code, Groovy code or Gradle properties.

The plugin was called before de.fayard.buildSrcVersions, which still exists and is currently identical.

Installation

Edit your root build.gradle(.kts) file

We recommend to use the plugins block:

buildscript {
    //...
}
plugins {
  id("de.fayard.buildSrcVersions") version "0.7.0"
}
// Don't put any code before the buildscripts {} and plugins {} block

Updating Gradle is usually a good idea. You get less bugs, more features and more build speed, and it’s as simple as this:

$ ./gradlew wrapper --gradle-version 5.6.4

The task :buildSrcVersions

The task :buildSrcVersions is…

from: https://dev.to//jmfayard/better-dependency-management-in-android-studio-3-5-with-gradle-buildsrcversions-34e9