Skip to content

Commit

Permalink
run demo-uikit on device (#604)
Browse files Browse the repository at this point in the history
Speedup with newest org.jetbrains.gradle.apple.applePlugin
Fix gradle problems on Mac with Intel CPU
Add possibility to run on real iOS device
  • Loading branch information
dima-avdeev-jb authored Jun 21, 2023
1 parent f56a88b commit 8f38e6e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 51 deletions.
26 changes: 26 additions & 0 deletions MULTIPLATFORM.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,29 @@ Run native macos Arm64 sample:
```bash
./gradlew :compose:mpp:demo:runDebugExecutableMacosArm64
```

#### Run mpp/demo-uikit sample on iOS
- Install plugin **Kotlin Multiplatform Mobile** for AppCode
- Disable Android Plugin in IDE preferences
- Open root of repository with AppCode
- Increase AppCode IDE memory more that 10GB
- Restart AppCode
- Wait while project synchronization with Gradle
- [Optional] To run on real iOS device
- Device should be at least with iOS 16.5. And Xcode should be at least 14.3
- Find your TEAM_ID with instruction https://github.com/JetBrains/compose-multiplatform-template#running-on-a-real-ios-device
- Create file project.properties in the root of repository. Add property `TEAM_ID=[your team id]` without double quotes, for example `TEAM_ID=ABC123ABC1`.
- Choose run configuration **iOS App**

#### Run mpp/demo sample on iOS with Xcode
Run script:
```bash
./compose/mpp/demo/regenerate_xcode_project.sh
```
Wait while Xcode is opening, and press run button.

### Clean IDE and Gradle cache
- Close project
- ```bash
./cleanTempFiles.sh
```
14 changes: 14 additions & 0 deletions cleanTempFiles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -e
cd "$(dirname "$0")"

echo "--------------------------------------------------------"
echo "This command will remove those files added to .gitignore"
git clean -ndX
read -p "Do you want to remove this files? (y/n) " yn

case $yn in
y ) git clean -fdX;;
n ) exit;;
* ) exit 1;;
esac
4 changes: 0 additions & 4 deletions compose/mpp/demo-uikit/README.md

This file was deleted.

112 changes: 65 additions & 47 deletions compose/mpp/demo-uikit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,64 +1,50 @@
import androidx.build.AndroidXComposePlugin
import java.util.Properties
import org.jetbrains.kotlin.gradle.dsl.KotlinNativeBinaryContainer
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinTargetWithBinaries

plugins {
id("AndroidXPlugin")
id("AndroidXComposePlugin")
id("kotlin-multiplatform")
id("org.jetbrains.gradle.apple.applePlugin") version "222.4550-0.21"
id("org.jetbrains.gradle.apple.applePlugin") version "222.4550-0.22"
}

val RUN_ON_DEVICE = false

AndroidXComposePlugin.applyAndConfigureKotlinPlugin(project)


dependencies {

}

repositories {
mavenLocal()
}

fun KotlinNativeBinaryContainer.configureFramework() {
framework {
baseName = "shared"
freeCompilerArgs += listOf(
"-linker-option", "-framework", "-linker-option", "Metal",
"-linker-option", "-framework", "-linker-option", "CoreText",
"-linker-option", "-framework", "-linker-option", "CoreGraphics"
)
}
}

kotlin {
if (System.getProperty("os.arch") == "aarch64") {
iosSimulatorArm64("uikitSimArm64") {
binaries {
framework {
baseName = "shared"
freeCompilerArgs += listOf(
"-linker-option", "-framework", "-linker-option", "Metal",
"-linker-option", "-framework", "-linker-option", "CoreText",
"-linker-option", "-framework", "-linker-option", "CoreGraphics"
)
}
}
val isArm64Host = System.getProperty("os.arch") == "aarch64"
iosArm64 {
binaries {
configureFramework()
}
} else {
iosX64("uikitX64") {
}
if (isArm64Host) {
iosSimulatorArm64 {
binaries {
framework {
baseName = "shared"
freeCompilerArgs += listOf(
"-linker-option", "-framework", "-linker-option", "Metal",
"-linker-option", "-framework", "-linker-option", "CoreText",
"-linker-option", "-framework", "-linker-option", "CoreGraphics"
)
}
configureFramework()
}
}
}
if (RUN_ON_DEVICE) {
iosArm64("uikitArm64") {
} else {
iosX64 {
binaries {
framework {
baseName = "shared"
freeCompilerArgs += listOf(
"-linker-option", "-framework", "-linker-option", "Metal",
"-linker-option", "-framework", "-linker-option", "CoreText",
"-linker-option", "-framework", "-linker-option", "CoreGraphics"
)
}
configureFramework()
}
}
}
Expand Down Expand Up @@ -86,13 +72,11 @@ kotlin {
val nativeMain by creating { dependsOn(skikoMain) }
val darwinMain by creating { dependsOn(nativeMain) }
val uikitMain by creating { dependsOn(darwinMain) }
if (System.getProperty("os.arch") == "aarch64") {
val uikitSimArm64Main by getting { dependsOn(uikitMain) }
val iosArm64Main by getting { dependsOn(uikitMain) }
if (isArm64Host) {
val iosSimulatorArm64Main by getting { dependsOn(uikitMain) }
} else {
val uikitX64Main by getting { dependsOn(uikitMain) }
}
if (RUN_ON_DEVICE) {
val uikitArm64Main by getting { dependsOn(uikitMain) }
val iosX64Main by getting { dependsOn(uikitMain) }
}
}
}
Expand All @@ -104,10 +88,44 @@ apple {
sceneDelegateClass = "SceneDelegate"
launchStoryboard = "LaunchScreen"

val runOnDevice = findProperty("xcode.arch") == "arm64"
val projectProperties = Properties()
val projectPropertiesFile = rootProject.file("project.properties")
if (projectPropertiesFile.exists()) {
projectProperties.load(projectPropertiesFile.reader())
} else {
projectPropertiesFile.createNewFile()
}
val teamId = projectProperties.getProperty("TEAM_ID")
if (runOnDevice && teamId == null) {
error("Add TEAM_ID=... to file ${projectPropertiesFile.absolutePath}")
}
if (teamId != null) {
buildSettings.DEVELOPMENT_TEAM(teamId)
}
buildSettings.DEPLOYMENT_TARGET("15.0")

// TODO: add 'CADisableMinimumFrameDurationOnPhone' set to 'YES'

dependencies {
// Here we can add additional dependencies to Swift sourceSet
}
}
}

// TODO: Workaround, see https://youtrack.jetbrains.com/issue/KT-55751
val myAttribute = Attribute.of("myOwnAttribute", String::class.java)
// replace releaseFrameworkIosFat by the name of the first configuration that conflicts
configurations.named("releaseFrameworkIosFat").configure {
attributes {
// put a unique attribute
attribute(myAttribute, "release-all")
}
}

// replace debugFrameworkIosFat by the name of the second configuration that conflicts
configurations.named("debugFrameworkIosFat").configure {
attributes {
attribute(myAttribute, "debug-all")
}
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ kotlin.options.suppressFreeCompilerArgsModificationWarning=true
kotlin.native.binary.memoryModel=experimental
# Disable kotlin/native klib binary cache
kotlin.native.cacheKind=none
# Workaround for Apple Gradle plugin
generateBuildableXcodeproj.skipKotlinFrameworkDependencies=true

androidx.compose.multiplatformEnabled=true
androidx.versionExtraCheckEnabled=false
Expand Down

0 comments on commit 8f38e6e

Please sign in to comment.