ythy / blog

Give everything a shot
7 stars 0 forks source link

Gradle 的 buildSrc 天坑 #544

Open ythy opened 5 days ago

ythy commented 5 days ago

buildSrc作为一个公共目录,按照教程只需要如此这般就能生效↓

build.gradle.kts

plugins {
    `kotlin-dsl`
}

\buildSrc\src\main\kotlin\com\xx\sss\gradle\Configuration.kt

package com.xx.sss.gradle

object Configuration {
    const val compileSdk = 34
    const val targetSdk = 34
    const val minSdk = 31
    const val minSdkDemo = 31
    const val majorVersion = 1
    const val minorVersion = 0
    const val patchVersion = 12
    const val versionName = "$majorVersion.$minorVersion.$patchVersion"
    const val versionCode = 13
    const val snapshotVersionName = "$majorVersion.$minorVersion.${patchVersion + 1}-SNAPSHOT"
    const val artifactGroup = "com.mx.emperor"
}

然后Configuration.compileSdk 一系列就能在app:build.gradle.kts里用了。

事实上呢,buildSrc 会不断的触发下面类似的请求, 你的开发环境如果访问不到https://repo.gradle.org/gradle/list/libs-releases/,那就完啦。

Failed https://repo.gradle.org/gradle/list/libs-releases/org/codehaus/groovy/groovy/3.0.17/groovy-3.0.17.pom 42 s 303 ms 0 B 0 B/s
Failed https://repo.gradle.org/gradle/list/libs-releases/org/codehaus/groovy/groovy-json/3.0.10/groovy-json-3.0.10.pom 42 s 557 ms 0 B 0 B/s
Running https://repo.gradle.org/gradle/list/libs-releases/org/codehaus/groovy/groovy/3.0.10/groovy-3.0.10.pom 0 ms 0 B 0 B/s

官方issue里有人提过这个问题,貌似没有被采纳😓 https://github.com/gradle/gradle/pull/29798 https://github.com/gradle/gradle/pull/29782

折磨一周多,最终还是发现了解决方法:

  1. gradle 升级到8.5,低版本没有下面的属性
  2. gradle.properties 追加以下
    systemProp.org.gradle.internal.http.connectionTimeout=1200
    systemProp.org.gradle.internal.http.socketTimeout=1200
    # the number of retries (initial included) (default 3)
    systemProp.org.gradle.internal.repository.max.retries=1
    # the initial time before retrying, in milliseconds (default 125)
    systemProp.org.gradle.internal.repository.initial.backoff=1

完结,❀撒花❀

ythy commented 5 days ago

后续,试图将 buildSrc 适用到包含proguard项目中,失败。

因为关键性的systemProp.org.gradle.internal.http.connectionTimeout=1200这个属性是 gradle 8.5.0 新增的。同时APG版本7.2的时候,会报错 Cannot use @TaskAction annotation on method IncrementalTask. 如果升级AGP到8+, 又报错API 'android.registerTransform' is removed with Android Gradle plugin 8.x

查阅proguard资料,有如下说明:

Since R8 is now the default Android shrinker, there is little need or demand for using ProGuard on Android apps. For this reason, we don't have any current plans for further support for AGP.
If there is a need or a use-case, one route for the community to take would be for AGP support to be implemented in a manner similar to the ProGuard Maven plugin: as a separate project that implements a ProGuard AGP plugin calling out to ProGuard.
R8 is compatible with ProGuard keep rules, so the manual and tools like the ProGuard Playground can be used with R8 configurations.

人家摆烂了。剧终,不适用了先