我正在尝试将基于Gradle构建的Kotlin Native项目放在一起。 我的目标是2个android目标和1个ios目标。我可以用它来正确地建立目标,并正确地引用通用/平台代码。我没有成功的是在android应用程序中(未尝试过IOS是)引用了多平台gradle构建的输出构建。
SharedCode / build.gradle:
apply plugin: 'kotlin-multiplatform'
def mobilePlatformAttribute = Attribute.of('com.inspirefitness.mobile', String)
def consolePlatformAttribute = Attribute.of('com.inspirefitness.console', String)
configurations {
mobileConfiguration {
extendsFrom configurations.default
attributes.attribute(mobilePlatformAttribute, 'mobile')
}
consoleConfiguration {
extendsFrom configurations.default
attributes.attribute(consolePlatformAttribute, 'console')
}
}
kotlin {
targets {
final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
? presets.iosArm64 : presets.iosX64
fromPreset(iOSTarget, 'iOS') {
compilations.main.outputKinds('FRAMEWORK')
}
}
jvm('android').attributes {
attribute(mobilePlatformAttribute, 'mobile')
}
jvm('console').attributes {
attribute(consolePlatformAttribute, 'console')
}
sourceSets {
commonMain.dependencies {
api 'org.jetbrains.kotlin:kotlin-stdlib-common'
}
androidMain.dependencies {
api 'org.jetbrains.kotlin:kotlin-stdlib'
}
consoleMain.dependencies {
api 'org.jetbrains.kotlin:kotlin-stdlib'
}
}
}
// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
compileClasspath
}
task packForXCode(type: Sync) {
final File frameworkDir = new File(buildDir, "xcode-frameworks")
final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
inputs.property "mode", mode
dependsOn kotlin.targets.iOS.compilations.main.linkTaskName("FRAMEWORK", mode)
from { kotlin.targets.iOS.compilations.main.getBinary("FRAMEWORK", mode).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
app / build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.jetbrains.jonnyzzz.myapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation project(path: ':SharedCode', configuration: 'mobileConfiguration')
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
由于我有2个android目标,因此我为它们指定了别名,并附加了属性以及在应用程序中引用的内置配置。 当我尝试将包导入Kotlin时,无法从通用名称空间解析作为模块的依赖项。在我对配置进行任何操作之前,我没有遇到任何问题,但是在出现问题时,我在2个android目标之间存在歧义。