groovy 将build.gradle转换为build.gradle.kts时,无法获得类似Unresolved reference的错误:工件ID

ckx4rj1h  于 2023-04-05  发布在  其他
关注(0)|答案(1)|浏览(450)

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如下所示)出现了一些错误:

  1. project.ext
group = project.ext.group_id
Unresolved reference: group_id

为什么不设置为最高级别的脚本呢?

allprojects { proj ->
    ext {
        group_id = getGroupId(proj)
        old_version = getOldVersion(proj)
    }
}
  1. 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}")
            }
        }
    }
}
8cdiaqws

8cdiaqws1#

它需要使用project.extra

group = checkNotNull(project.extra.get("group_id"))
version = BuildVersions.version
project.extra.set("artifactId", "proj-core")

相关问题