Some Errors I've Found Developing with KMM
So you are trying to code a Kotlin Multiplatform module.
After setting up a build gradle file for the Android project with this new module, you are led to believe that you will:
- Build on android.
- Then run pod install to install this new precompiled module into an iOS project.
- Then you go on Xcode, run build, and everything should work out. Right?
Wrong!
As of today, Kotlin Multiplatform isn’t in Beta yet. So there have been some errors along the way. Here’s some that I’ve found.
MAC Problems with Access
Make sure these Applications have full Access to folders (or at least the folders required by the android and iOS projects):
Terminal, Xcode, Android Studio.
MAC Cannot find PodFile
If you haven’t created one already, one blueprint for one is:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'DebugFlavor' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'shared_core', :path => '../yourPath/YourAndroidProject/shared_core'
end
target 'ReleaseFlavor' do
use_frameworks!
pod 'shared_core', :path => '../yourPath/YourAndroidProject/shared_core'
end
post_install do |installer|
installer.pods_project.build_configurations.each do |config|
config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
end
end
If you have a PodFile already, there’s a few things that can happen:
- Android Studio can’t find PodFile declared in the build.gradle.kts because it isn’t there.
- Android Studio doesn’t have permission to search that folder.
Example:
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "11"
frameworkName = "shared_core"
podfile = project.file("../../ios/Podfile")
}
In this case, check if the file exists there and it is the correct path.
Also, check if Android Studio has permission to access that path.
If nothing works you can delete that line declaring the podfile on build.gradle.kts
, run pod install manually after you build on android.
MAC M1 Problems
Installing Cocoapods for the M1
Check out this answer:
How to install Cocoapods on Apple Silicon M1 computers? by Nikolay Nikonov
Simulator + M1
If you are on a Mac M1 and are gonna build for the simulator, here’s what you should do.
val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = when {
System.getenv("SDK_NAME")?.startsWith("iphoneos") == true -> ::iosArm64
// System.getenv("NATIVE_ARCH")?.startsWith("arm") == true -> ::iosSimulatorArm64 <--------- comment this line
else -> ::iosX64
}
If you are running with the physical iPhone, no need for Rosetta.
M1 + Room Library
From:
ext.room_version = '2.3.0'
To:
ext.room_version = '2.4.0-alpha03'
You should change your room dependency value:From: ext.room_version = '2.3.0'
To: ext.room_version = '2.4.0-alpha03'
JAVA_HOME on Xcode M1
Download OpenJDK from Azul’s M1 OpenJDK builds. Make it that the default version used is no greater than 11. XCode bugs with greater than 11 version during building Gradle.
Here’s a more detailed answer.
Cannot find [yourSharedModule].klib in XCode
See if you gave access for Xcode and Terminal to search in the android folder’s project.
Delete .gradle
from the android project.
Delete .konan
from ios project.
Go in gradle folder in your android project and type
chmod -R +x *
-R means recursive through directories
+x means add to every file the permission to be executed
Rebuild all of them. (Build Android, Pod install, Build iOS).
Now I found some Mac Intels that just don’t like this .klib file and can’t seem to find it. I’m still trying to solve this for a particular annoying mac, you can check how it’s going here.
That’s it, that sums up about a month of hitting my head on the desk to solve each individual problem that the KMM had so far.