Android项目(它有几个子项目,proj-core
;proj-module
)尝试转换为KotlinDSL。
在buildSrc
文件夹中添加以下buildSrc/src/main/java/Versions.kt
文件:
object DepVersions {
const val sdkVersion = "1.0.0"
const val versionSuffix = "-SNAPSHOT"
const val supportlib = "28.0.0"
const val androidBuild = "7.4.1" //AGP
const val firebase = "21.1.0"
......
// test dependencies
const val robolectric = "4.8.1"
const val jacoco = "0.8.8"
const val junitVersion = "4.13.2"
......
}
buildSrc
中的build.gradle.kts
repositories {
mavenCentral()
google()
}
plugins {
`kotlin-dsl`
}
在顶层的build.gradle
中,它有一些定义和函数,特别是这一个,它接受项目并从项目中生成group_id
并放入project.ext
。
buildscript {
ext.versions = []
......
}
allprojects { proj ->
ext {
group_id = getGroupId(proj)
old_version = getOldVersion(proj)
}
}
static String getGroupId(Project p) {
String group_id = 'com.xxx.yyy'
String pName = "doSemver"
String localName = "local" // default for local
String buildEnv = 'auto'
if ((System.env.HOST && (System.env.HOST == "true"))) {
if (p.properties.containsKey(pName)) {
return group_id + "." + buildEnv
} else {
return group_id
}
}
return ("${group_id}.${localName}")
}
proj-module
子项目的转换后的build.gradle.kts
(子项目的build.gradle
和转换后的build.gradle.kts
如下所示)出现了一些错误:
project.ext
group = project.ext.group_id
Unresolved reference: group_id
为什么不设置为最高级别的脚本呢?
allprojects { proj ->
ext {
group_id = getGroupId(proj)
old_version = getOldVersion(proj)
}
}
project.ext.artifactId
,为什么artifactId
不是project.ext
的一部分?
project.ext.artifactId = "proj-module"
Unresolved reference: artifactId
列出了子项目的build.gradle在这里,它有
group = project.ext.group_id
version = versions.version
project.ext.artifactId = 'proj-core'
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply from: '../jacoco_coverage.gradle'
android {
compileSdk versions.compilesdk
buildToolsVersion versions.buildtools
defaultConfig {
minSdkVersion versions.minsdk
targetSdk versions.targetsdk
versionCode 1
versionName version
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'proguard-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}"
api "com.google.firebase:firebase-messaging:${versions.firebase}"
......
}
String getVersionTag(String intake) {
if(project.hasProperty("doRelease"))
return intake
return intake+"${versions.versionSuffix}"
}
boolean localWorkspace() {
if(findProject(":proj-core")) {
if(!project.hasProperty("deploying"))
return true
}
return false
}
//add either project dependency or maven
if( localWorkspace() ) {
dependencies {
api project(":proj-core")
}
} else {
dependencies {
api "com.xxx.yyy:proj-core:${getVersionTag(versions.version)}"
}
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion "${versions.supportlib}"
}
}
}
}
转换为build.gradle.kts
后
group = project.ext.group_id. //<== err: Unresolved reference: group_id
version = BuildVersions.sdkVersion
project.ext.artifactId = "proj-module" //<== err: Unresolved reference: artifactId
plugins {
id("com.android.library")
kotlin("android")
}
apply(from = "../jacoco_coverage.gradle")
//apply from: '../jacoco_coverage.gradle'
android {
compileSdk = BuildVersions.compileSdkVersion
buildToolsVersion = BuildVersions.buildToolsVersion
defaultConfig {
minSdk = BuildVersions.minSdkVersion
targetSdk = BuildVersions.targetSdkVersion
// not exit in kts
// versionCode = 1
// versionName = version
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("proguard-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles (getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}
dependencies {
implementation(fileTree(mapOf("include" to listOf("*.jar"), "dir" to "libs", )))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${DepVersions.kotlin}")
api("com.google.firebase:firebase-messaging:${DepVersions.firebase}")
...
}
fun getVersionTag(intake: String): String{
if(project.hasProperty("doRelease"))
return intake
return intake+"${BuildVersions.versionSuffix}"
}
fun localWorkspace(): Boolean {
if(null != findProject(":proj-core")) {
if(!project.hasProperty("deploying"))
return true
}
return false
}
//add either project dependency or maven
if(localWorkspace()) {
dependencies {
api(project(":proj-core"))
}
} else {
dependencies {
api("com.xxx.yyy:proj-core:${getVersionTag(BuildVersions.sdkVersion)}")
}
}
configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "com.android.support") {
if (!requested.name.startsWith("multidex")) {
useVersion("${DepVersions.supportlib}")
}
}
}
}
1条答案
按热度按时间8cdiaqws1#
它需要使用project.extra