gpt4 book ai didi

android - kotlin 多平台 : how to reduce build time?

转载 作者:行者123 更新时间:2023-12-01 16:01:54 25 4
gpt4 key购买 nike

我有一个 kotlin 多平台项目来创建一个针对 iOS 和 Android 的共享库。我有一些多平台外部库:sqldelight、kodein-di、ktor、klock、kotlinx-coroutines 和 kotlinx-serialization。

使用命令行 ./gradlew :SharedCode:build 将共享库构建为苹果框架大约需要 5 分钟

是否可以减少此构建时间?

我的 SharedCode 多平台库 build.gradle

apply plugin: 'kotlinx-serialization'
apply plugin: 'com.android.library'
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: "com.squareup.sqldelight"
apply plugin: "org.jetbrains.dokka"

group = 'com.example.multiplatform'
version = '1.0'

android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
}
buildTypes {
release {
minifyEnabled false
}
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}

sqldelight {
MyDB {
packageName = "com.example.multiplatform"
}
}

dokka {
outputFormat = "markdown"
impliedPlatforms = ["common"] // This will force platform tags for all non-common sources e.g. "JVM"
kotlinTasks {
// dokka fails to retrieve sources from MPP-tasks so they must be set empty to avoid exception
// use sourceRoot instead (see below)
[]
}
sourceRoot {
// assuming there is only a single source dir...
path = kotlin.sourceSets.commonMain.kotlin.srcDirs[0]
platforms = ["common"]
}
}

dependencies {
// Specify Kotlin/JVM stdlib dependency.
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

testImplementation 'junit:junit:4.12'
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'

androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.jetbrains.kotlin:kotlin-test'
androidTestImplementation 'org.jetbrains.kotlin:kotlin-test-junit'

androidTestImplementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

kotlin {
targets {
final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
? presets.iosArm64 : presets.iosX64

fromPreset(iOSTarget, 'ios') {
binaries {
framework('SharedCode')
}
}

fromPreset(presets.android, 'android')
}

sourceSets {
all {
dependencies {
implementation "org.kodein.di:kodein-di-core:$kodein_version"
implementation "org.kodein.di:kodein-di-erased:$kodein_version"
}
}

commonMain {
dependencies {
//HTTP
implementation "io.ktor:ktor-client-json:$ktor_json_version"
implementation "io.ktor:ktor-client-serialization:$ktor_version"
//Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$kotlinx_coroutines_version"
//Kotlinx serialization
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
//kclock
implementation "com.soywiz.korlibs.klock:klock:$klock_version"
}
}

commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'

implementation "io.ktor:ktor-client-mock:$ktor_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$kotlinx_coroutines_version"
implementation "org.kodein.di:kodein-di-core:$kodein_version"
implementation "org.kodein.di:kodein-di-erased:$kodein_version"
}
}

androidMain {
dependencies {
//HTTP
implementation "io.ktor:ktor-client-json-jvm:$ktor_json_version"
implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"
//Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinx_coroutines_version"
//Kotlinx serialization
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
//sqldelight
implementation "com.squareup.sqldelight:android-driver:$sqldelight_version"
}
}

androidTest {
dependencies {
implementation 'junit:junit:4.12'
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
implementation "io.ktor:ktor-client-mock-jvm:$ktor_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines_version"
}
}

iosMain {
dependencies {
//HTTP
implementation "io.ktor:ktor-client-ios:$ktor_version"
implementation "io.ktor:ktor-client-json-native:$ktor_json_version"
implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
//Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$kotlinx_coroutines_version"
//kotlinx serialization
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"
//sqldelight
implementation "com.squareup.sqldelight:ios-driver:$sqldelight_version"
}
}

iosTest {
dependencies {
implementation "io.ktor:ktor-client-mock-native:$ktor_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$kotlinx_coroutines_version"
}
}
}
}

task iosTest {
def device = project.findProperty("iosDevice")?.toString() ?: "iPhone X"
def isDevice = System.getenv('SDK_NAME')?.startsWith("iphoneos")
dependsOn 'linkTestDebugExecutableIos'
group = JavaBasePlugin.VERIFICATION_GROUP
description = "Runs tests for target 'ios' on an iOS simulator"

doLast {
if (!isDevice) {
def binary = kotlin.targets.ios.binaries.getExecutable('test', 'DEBUG').outputFile
exec {
commandLine 'xcrun', 'simctl', 'spawn', device, binary.absolutePath
}
}
}

}

tasks.check.dependsOn iosTest

tasks.withType(Test) {
testLogging {
exceptionFormat "full"
events "passed", "failed"
showStandardStreams true
}
}

task packForXCode(type: Sync) {
final File frameworkDir = new File(buildDir, "xcode-frameworks")
final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
final def framework = kotlin.targets.ios.binaries.getFramework("SharedCode", mode)

inputs.property "mode", mode
dependsOn framework.linkTask

from { framework.outputFile.parentFile }
into frameworkDir

doLast {
new File(frameworkDir, 'gradlew').with {
text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n"
setExecutable(true)
}
}
}
tasks.build.dependsOn packForXCode

最佳答案

当我积极编码和测试时,我只是构建框架目标,而不是使用 build 构建整个项目。 .调用build通常制作调试和发布框架,以及两者的测试框架。

所以,就我而言,我运行 linkMainDebugFrameworkIos而不是完整的build .这需要的时间要少得多。 1m 35s 对比至少 5m,如果不是更长,完整构建。

该任务名称可能会随着 1.3.40+ 而更改,并且 sqldelight 和其他库应该很快就会出现在 1.3.40 上。仅供引用。

关于android - kotlin 多平台 : how to reduce build time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56904599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com