首页 > 解决方案 > androidMain 和 android 测试文件夹未被识别为模块

问题描述

几天以来我一直在努力解决这个问题,但找不到任何解决方案,所以我希望我能在这里得到任何帮助。我使用 IntelliJ IDEA 2020.2.1 的移动应用程序模板创建了一个多平台项目,并且由于项目设置 androidMain 和 AndroidTest 文件夹未被识别为模块。在启动 androidApp 任务或 PackForXcode 时,这似乎不是问题,因为对预期/实际定义的引用已解决,但想法建议无论如何显示 androidMain 引用似乎是错误的,因为根据屏幕截图似乎从父 vpayConnectLib 模块解决了引用。 在此处输入图像描述

我使用标准的 android() 目标 在此处输入图像描述

启动 compileDebugAndroidTestKotlinAndroid 任务时,此错误会突出显示 在此处输入图像描述

我尝试使用不同的模板创建新项目,但 androidMain 和 androidTest 文件夹从所有项目模板开始就不被视为模块

这是一个已知的错误还是我做错了什么?这是我正在使用的版本的根项目 gradle 文件 在此处输入图像描述

这是我的根项目 gradle 文件:

buildscript {
    repositories {
        gradlePluginPortal()
        jcenter()
        google()
        mavenCentral()
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0")
        classpath("com.android.tools.build:gradle:4.0.1")
        classpath("com.squareup.sqldelight:gradle-plugin:1.4.3")
        classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.8")
    }
}

group = "com.retailinmotion"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

这是共享库 gradle 文件

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("kotlin-android-extensions")
    id("com.squareup.sqldelight")
    id("org.sonarqube")
    jacoco
}
group = "com.retailinmotion"
version = "1.0-SNAPSHOT"

repositories {
    gradlePluginPortal()
    google()
    jcenter()
    mavenCentral()
}

sqldelight {
    database("vPayConnectDatabase") {
        packageName = "com.retailinmotion.vpayconnect"
        sourceFolders = listOf("sqldelight")
    }
}

task<JacocoReport>("jacocoTestReport") {
    dependsOn("testDebugUnitTest")
    reports {
        xml.isEnabled = true
        csv.isEnabled = false
        html.isEnabled = true
    }
    sourceDirectories.setFrom(files(fileTree("../vPayConnectLib")))
    classDirectories.setFrom(files(fileTree("build/tmp/kotlin-classes/debug")))
    executionData.setFrom(file("build/jacoco/testDebugUnitTest.exec"))
}

sonarqube {
    properties {
        property("sonar.sourceEncoding", "UTF-8")
        property("sonar.java.coveragePlugin", "jacoco")
        property("sonar.sources", "src/commonMain/kotlin, src/iosMain/kotlin, src/androidMain/kotlin")
        property("sonar.tests", "src/commonTest/kotlin, src/iosTest/kotlin, src/androidTest/kotlin")
        property("sonar.binaries", "build/tmp/kotlin-classes/debug")
        property("sonar.java.binaries", "build/tmp/kotlin-classes/debug")
        property("sonar.java.test.binaries", "build/tmp/kotlin-classes/debugUnitTest")
        property("sonar.junit.reportPaths", "build/test-results/testDebugUnitTest")
        property("sonar.jacoco.reportPaths", "build/jacoco/testDebugUnitTest.exec")
        property("sonar.coverage.jacoco.xmlReportPaths", "build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml")
    }
}

kotlin {


    android()

    iosArm64("ios") {
        binaries {
            framework {
                baseName = "vPayConnectLib"
            }
        }
        compilations["main"].cinterops {
            //import Datecs library
            val datecs by creating {
                packageName ("com.retailinmotion.datecs")
                defFile = file("$projectDir/src/iosMain/c_interop/Datecs.def")
                includeDirs ("$projectDir/src/iosMain/c_interop/Datecs/")
            }
            //import Datecs Utils library
            val datecsutils by creating {
                packageName ("com.retailinmotion.datecsutils")
                defFile = file("$projectDir/src/iosMain/c_interop/DatecsUtils.def")
                includeDirs ("$projectDir/src/iosMain/c_interop/DatecsUtils/")
            }
            //import Magtek library
            val magtek by creating {
                packageName ("com.retailinmotion.magtek")
                defFile = file("$projectDir/src/iosMain/c_interop/Magtek.def")
                includeDirs ("$projectDir/src/iosMain/c_interop/Magtek")
            }
        }
    }

    iosX64(){
        binaries {
            framework {
                baseName = "vPayConnectLib"
            }
        }
        compilations["main"].cinterops {
            //import Datecs library
            val datecs by creating {
                packageName ("com.retailinmotion.datecs")
                defFile = file("$projectDir/src/iosMain/c_interop/Datecs.def")
                includeDirs ("$projectDir/src/iosMain/c_interop/Datecs/")
            }
            //import Datecs Utils library
            val datecsutils by creating {
                packageName ("com.retailinmotion.datecsutils")
                defFile = file("$projectDir/src/iosMain/c_interop/DatecsUtils.def")
                includeDirs ("$projectDir/src/iosMain/c_interop/DatecsUtils/")
            }
            //import Magtek library
            val magtek by creating {
                packageName ("com.retailinmotion.magtek")
                defFile = file("$projectDir/src/iosMain/c_interop/Magtek.def")
                includeDirs ("$projectDir/src/iosMain/c_interop/Magtek")
            }
        }
    }

    sourceSets {

        val commonMain by getting {
            dependencies {
                implementation("com.squareup.sqldelight:runtime:1.4.3")
            }
        }

        val commonTest by getting {
            dependsOn(commonMain)

            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
                implementation("org.jetbrains.kotlin:kotlin-test-junit:1.4.10")
            }
        }

        val androidMain by getting {
            dependencies {
                implementation("androidx.core:core-ktx:1.3.1")
                implementation("com.squareup.sqldelight:android-driver:1.4.3")
            }
        }

        val androidTest by getting {
            dependsOn(androidMain)
        }

        val iosMain by getting {
            dependencies {
                implementation("com.squareup.sqldelight:native-driver:1.4.3")
            }
        }

        val iosTest by getting {
            dependsOn(iosMain)
        }

        val iosX64Main by getting {
            dependsOn(iosMain)
        }

        val iosX64Test by getting {
            dependsOn(iosTest)
        }
    }

    tasks {
        register("universalFramework", org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask::class) {
            val debugMode = "DEBUG"
            val mode = System.getenv("CONFIGURATION") ?: debugMode
            baseName = "vPayConnectLib"

            val iosArm64Framework = iosArm64("ios").binaries.getFramework(mode)
            val iosX64Framework = iosX64().binaries.getFramework(mode)

            from(
                iosArm64Framework,
                iosX64Framework
            )

            destinationDir = buildDir.resolve("xcode-universal-framework")
            group = "Universal framework"
            description = "Builds a universal (fat) $mode framework"

            dependsOn(iosArm64Framework.linkTask)
            dependsOn(iosX64Framework.linkTask)
        }
    }
}
android {
    compileSdkVersion(29)
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
    dependencies {
        implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    }
}
val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)

任何帮助表示赞赏,谢谢

标签: kotlinintellij-ideakotlin-multiplatform

解决方案


推荐阅读