gradle 8.0 DSL如何以上自定义依赖项

1 阅读1分钟

在之前的groovy版本中,比较好定义自定义依赖项,但是在升级到kotlin版本的DSL,很多写法有了改变。现在记录下要如何自定义;

假设,我们现在要集成Bugly。但是我们的apk可能会分很多渠道,比如国内的,我们依赖bugly,google的使用firebase。那这样我们就需要根据渠道来依赖不同的依赖。

有2种方案:

  1. 通过configurations.creating声明变量,然后直接使用变量
private val officialDebugImplementation: Configuration by configurations.creating { 
   extendsFrom(configurations.debugImplementation.get())
}

private val googleDebugImplementation: Configuration by configurations.creating { 
   extendsFrom(configurations.debugImplementation.get())
}

dependencies {
    //国内版本依赖Bugly
    officialDebugImplementation("com.tencent.bugly:crashreport:4.1.9.3")
    //海外依赖其他项
    googleDebugImplementation("com.****")
    

}
  1. 通过configurations.create 声明字符串,使用时也需要使用字符串

configurations {
    create("officialDebugImplementation") {
        extendsFrom(configurations.debugImplementation.get())
    }
    create("officialReleaseImplementation") {
        extendsFrom(configurations.releaseImplementation.get())
    }
     create("googleReleaseImplementation") {
        extendsFrom(configurations.releaseImplementation.get())
    }
}


dependencies {
    //国内版本依赖Bugly
    “officialDebugImplementation”("com.tencent.bugly:crashreport:4.1.9.3")
    //海外依赖其他项
    “googleDebugImplementation”("com.****")
    

}

2个方案都可以,各位可以根据自己的喜好选择