我按照本网站的教程使用 Intelli-j IDE(社区版)创建了一个 Kotlin 多平台项目:
https://medium.com/@cafonsomota/set-up-your-first-kotlin-multiplatform-project-for-android-and-ios-april-2020-258e2b1d9ef4
我没有关注的是本教程的 xCode 部分,因为在这一点上,虽然我希望这个项目是多平台的,但我的主要兴趣是 Android。
当我运行 common
样本测试,我看到错误:e: org.jetbrains.kotlin.konan.MissingXcodeException: An error occurred during an xcrun execution. Make sure that Xcode and its command line tools are properly installed.
我还可以看到,对于关联的配置,任务详细信息如下:cleanIosTest iosTest
这就是我收到错误的原因。
我想不通的是如何更改Sample Test
不运行该配置。我试图删除这些任务,应用和保存,但是当我运行它们时它们会不断重新出现。我在 build.gradle 文件中看不到任何指示 iOS 特定于测试的任何内容。
build.gradle.app
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
}
repositories {
google()
jcenter()
mavenCentral()
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId 'org.jetbrains.kotlin.mpp_app_android'
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName '1.0'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
kotlin {
android("android")
// This is for iPhone emulator
// Switch here to iosArm64 (or iosArm32) to build library for iPhone device
iosX64("ios") {
binaries {
framework()
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
androidMain {
dependencies {
implementation kotlin('stdlib')
}
}
androidTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
iosMain {
}
iosTest {
}
}
}
// This task attaches native framework built from ios module to Xcode project
// (see iosApp directory). Don't run this task directly,
// Xcode runs this task itself during its build process.
// Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
task copyFramework {
def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
def target = project.findProperty('kotlin.target') ?: 'ios'
dependsOn kotlin.targets."$target".binaries.getFramework(buildType).linkTask
doLast {
def srcFile = kotlin.targets."$target".binaries.getFramework(buildType).outputFile
def targetDir = getProperty('configuration.build.dir')
copy {
from srcFile.parent
into targetDir
include 'app.framework/**'
include 'app.framework.dSYM'
}
}
}
示例测试如下所示:package sample
import kotlin.test.Test
import kotlin.test.assertTrue
class SampleTests {
@Test
fun testMe() {
assertTrue(Sample().checkMe() > 0)
}
@Test
fun testProxy() {
assertTrue(Proxy().proxyHello().isNotEmpty())
}
}
配置如下所示:有谁知道我如何在不需要下载 xCode 的情况下解决这个问题?我很乐意分享任何其他信息,但我不确定我应该为此分享什么。
顺便说一句,我确实创建了另一个没有该行的配置,但是当我在第一次测试中按下绿色 PLAY 按钮时,它始终默认为
Sample Test
配置 iOS 任务。
最佳答案
这个问题是由已经在 Kotlin 问题跟踪器中描述的错误引起的,请参阅 here 。如果您想运行仅限 Android 的测试,您应该使用名为 test<Debug | Release>UnitTest
的 Gradle 任务,而不是 allTests
,它包括您案例中唯一的 iOS。
关于android - 为 Kotlin 多平台项目运行单元测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64207376/