Android kotlin 上传根目录配置中所有子模块配置的mavne失败,找不到名称'release'

问题描述 投票:0回答:1

子模块build.gradle.kts


group = "net.xxxx.xxxx"
version = "1.0.0"
plugins {
    alias(libs.plugins.android.library) //  com.android.library 8.7.3
    alias(libs.plugins.kotlin.android) // org.jetbrains.kotlin.android 2.1.0
}

android {
    namespace = "net.xxxx.xxxx"
    compileSdk = 34
    defaultConfig {
        minSdk = 21

        testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }

    buildTypes {

        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
        debug {
            isMinifyEnabled = false
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
}
//afterEvaluate {
//    publishing {
//        publications {
//            create<MavenPublication>("release") {
//                println(components.asMap.keys)
//                from(components["release"])
//            }
//        }
//    }
//}
dependencies {
    implementation(libs.appcompat.v7)
    implementation(libs.retrofit)
    implementation(libs.okhttp3.loging.interceptor)
    implementation(libs.okhttp3.converter.gson)
    implementation(libs.woaoo.model)
    testImplementation(libs.junit)
    androidTestImplementation(libs.runner)
    androidTestImplementation(libs.espresso.core)
}

在子模块项目中create("release")组件可以获取[debug,release]

根构建.gradle.kts

import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import io.gitlab.arturbosch.detekt.Detekt

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.jetbrains.kotlin.jvm) apply false
    alias(libs.plugins.arturbosch.detekt)
    alias(libs.plugins.android.library) apply false
    id("maven-publish")
}

subprojects {
    apply(plugin = "io.gitlab.arturbosch.detekt")
    apply(plugin = "maven-publish")

    detekt {
        source.from(files("src/main/java", "src/main/kotlin"))
        buildUponDefaultConfig = true
        allRules = false
        disableDefaultRuleSets = false
        debug = false
        parallel = false
    }

    tasks.withType<Detekt>().configureEach {
        reports {
            html.required.set(true)
            xml.required.set(true)
            txt.required.set(true)
            sarif.required.set(true)
        }
    }
    afterEvaluate {
        if (plugins.hasPlugin("com.android.library")) {
            publishing {
                publications {
                    println(components.asMap.keys)
                    create<MavenPublication>("${project.name}Release") {
                        artifactId = project.name 
                        if (artifactId == "app") return@create
                        val isJavaLib = components.asMap.keys.contains("kotlin")
                        if (isJavaLib) {
                            from(project.components.getByName("kotlin"))
                        } else {
                        from(components["release"]) 
                        }
                        groupId = project.group.toString() // 使用项目组作为 groupId
                        version = project.version.toString() // 使用项目版本作为 version
                    }
                }
//            repositories {
//                maven {
//                    isAllowInsecureProtocol = true
//                    name = "release"
//                    url = uri("http://xxxxxxx/repository/maven-releases")
//                    credentials {
//                        username = "xxxxx"
//                        password = "xxxxxxxx"
//                    }
//                }
            }
        }

    }
}
}

同步项目会出现错误并且组件 [] 为空

* What went wrong:
A problem occurred configuring project ':xxxxx'.
> SoftwareComponent with name 'release' not found.

我要实现的是根目录下maven打包的统一配置,有两个子模块Java libray和Android库,现在Java库正常了

android kotlin maven gradle
1个回答
0
投票

我是这样解决的。 方法调用顺序为 subprojects.afterEvaluate -> child.afterEvaluate。组件是在 child.afterEvaluate 创建的,所以我们无法在子项目中获取“release”。afterEvaluate,用户

gradle.projectsEvaluated
child.afterEvaluate

之后被调用
import io.gitlab.arturbosch.detekt.Detekt

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.jetbrains.kotlin.jvm) apply false
    alias(libs.plugins.arturbosch.detekt)
    alias(libs.plugins.android.library) apply false
    id("maven-publish")
}
gradle.projectsEvaluated {
    subprojects {
        detekt {
            source.from(files("src/main/java", "src/main/kotlin"))
            buildUponDefaultConfig = true
            allRules = false
            disableDefaultRuleSets = false
            debug = false
            parallel = false
        }

        tasks.withType<Detekt>().configureEach {
            reports {
                html.required.set(true)
                xml.required.set(true)
                txt.required.set(true)
                sarif.required.set(true)
            }
        }
        publishing {
            publications {
                create<MavenPublication>("${project.name}Release") {
                    if (artifactId == "app") return@create
                    val isJavaLib = components.asMap.keys.contains("kotlin")
                    if (isJavaLib) {
                        from(components.getByName("kotlin"))
                    } else {
                        from(components.getByName("release"))
                    }
                    groupId = project.group.toString() 
                    version = project.version.toString() 
                }
                create<MavenPublication>("${project.name}snapshot") {
                    artifactId = project.name 
                    if (artifactId == "app") return@create
                    val isJavaLib = components.asMap.keys.contains("kotlin")
                    if (isJavaLib) {
                        from(components.getByName("kotlin"))
                    } else {
                        from(components.getByName("release"))
                    }
                    groupId = project.group.toString() 
                    version = "${project.version}-SNAPSHOT"
                }
            }
            repositories {
                maven {
                    isAllowInsecureProtocol = true
                    name = "release"
                    url = uri("xxxxxxx")
                    credentials {
                        username = "xxxxx"
                        password = "xxxxxxxxxx"
                    }
                }
                maven {
                    isAllowInsecureProtocol = true=
                    name = "snapshot"
                    url = uri("http://xxxxxxxxx")
                    credentials {
                        username = "xxxxxx"
                        password = "xxxxxxxxx"
                    }
                }
            }
        }
    }
}

subprojects {
    apply(plugin = "io.gitlab.arturbosch.detekt")
    apply(plugin = "maven-publish")
}
© www.soinside.com 2019 - 2024. All rights reserved.