Skip to content

Commit

Permalink
Working through Crashlytics missing links
Browse files Browse the repository at this point in the history
  • Loading branch information
kpgalligan committed Mar 13, 2024
1 parent dc5c94f commit 798278f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import java.util.*

fun KotlinMultiplatformExtension.addFrameworkLinkPath(
frameworkFile: File,
linkerName: String?,
subpathBlock: (target: KonanTarget) -> String?
) {
targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget::class.java)
Expand All @@ -35,6 +36,9 @@ fun KotlinMultiplatformExtension.addFrameworkLinkPath(

if (isExecutable || isDynamicFramework) {
subpathBlock(binary.target.konanTarget)?.let { subpath ->
if(linkerName != null) {
binary.linkerOpts.addAll(listOf("-framework", linkerName))
}
binary.linkerOpts.add("-F${frameworkFile.absolutePath}/${subpath}/")
if (isExecutable) {
binary.linkerOpts.addAll(listOf("-rpath", "${frameworkFile.absolutePath}/${subpath}/"))
Expand All @@ -48,7 +52,8 @@ fun KotlinMultiplatformExtension.addFrameworkLinkPath(
fun findFrameworkBinaryFolder(
execOperations: ExecOperations,
zipUrl: String,
frameworkName: String
frameworkName: String,
zipRelativeFrameworkPath: String
): File {
val homeDir = File(System.getProperty("user.home"))
val touchlabDir = File(homeDir, ".touchlab")
Expand Down Expand Up @@ -85,7 +90,7 @@ fun findFrameworkBinaryFolder(
execOperations.exec {
commandLine(
"mv",
"${outDir.absolutePath}/$tempUuid/Carthage/Build/${frameworkName}.xcframework",
"${outDir.absolutePath}/$tempUuid/${zipRelativeFrameworkPath}",
"${outDir.absolutePath}/"
)
}
Expand All @@ -97,7 +102,7 @@ fun findFrameworkBinaryFolder(
}

if (!crashFrameworkDir.exists()) {
throw GradleException("${frameworkName} framework not found at ${crashFrameworkDir.absolutePath}")
throw GradleException("$frameworkName framework not found at ${crashFrameworkDir.absolutePath}")
}

return crashFrameworkDir
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package co.touchlab.crashkios

import org.gradle.api.provider.ValueSource
import org.gradle.api.provider.ValueSourceParameters
import org.gradle.process.ExecOperations
import java.io.File
import javax.inject.Inject

abstract class CrashlyticsFrameworkValueSource : ValueSource<File, ValueSourceParameters.None> {
@get:Inject
abstract val execOperations: ExecOperations

override fun obtain(): File {
val frameworkFile = findFrameworkBinaryFolder(
execOperations = execOperations,
zipUrl = "https://github.com/firebase/firebase-ios-sdk/releases/download/10.22.0/Firebase.zip",
frameworkName = "FirebaseCrashlytics",
zipRelativeFrameworkPath = "Firebase/FirebaseCrashlytics/FirebaseCrashlytics.xcframework"
)

return frameworkFile
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,67 @@

package co.touchlab.crashkios

import org.gradle.api.*
import org.gradle.kotlin.dsl.*
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.of
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.Framework
import java.io.*
import java.util.*
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget

internal val Project.kotlinExtension: KotlinMultiplatformExtension get() = extensions.getByType()

@Suppress("unused")
class CrashlyticsLinkPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = with(project) {
afterEvaluate {
project.kotlinExtension.crashLinkerConfig(
"-U _FIRCLSExceptionRecordNSException " +
"-U _OBJC_CLASS_\$_FIRStackFrame " +
"-U _OBJC_CLASS_\$_FIRExceptionModel " +
"-U _OBJC_CLASS_\$_FIRCrashlytics"
)
if (HostManager.hostIsMac) {
afterEvaluate {
val sentryFrameworkProvider = providers.of(CrashlyticsFrameworkValueSource::class) {}
val sentryFramework = sentryFrameworkProvider.get()

project.kotlinExtension.addFrameworkLinkPath(
frameworkFile = sentryFramework,
linkerName = "FirebaseCrashlytics",
subpathBlock = ::findXcframeworkSubfolder
)
}
}
}
}

private fun KotlinMultiplatformExtension.crashLinkerConfig(linkerOpts: String) {
targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget::class.java)
.map { target ->
val mainCompilation = target.compilations.getByName("main")
val dynamicFrameworks =
target.binaries.filterIsInstance<Framework>().filter { framework -> !framework.isStatic }
private fun findXcframeworkSubfolder(target: KonanTarget): String? = when (target) {
KonanTarget.IOS_ARM64 -> "ios-arm64"
KonanTarget.IOS_SIMULATOR_ARM64,
KonanTarget.IOS_X64 -> "ios-arm64_x86_64-simulator"

Pair(mainCompilation, dynamicFrameworks)
}
.forEach { pair ->
if (!pair.second.isEmpty()) {
pair.first.kotlinOptions.freeCompilerArgs += listOf("-linker-options", linkerOpts)
}
}
KonanTarget.MACOS_ARM64,
KonanTarget.MACOS_X64 -> "macos-arm64_x86_64"

KonanTarget.TVOS_ARM64 -> "tvos-arm64"
KonanTarget.TVOS_SIMULATOR_ARM64,
KonanTarget.TVOS_X64 -> "tvos-arm64_x86_64-simulator"

KonanTarget.WATCHOS_ARM32,
KonanTarget.WATCHOS_ARM64,
KonanTarget.WATCHOS_DEVICE_ARM64 -> "watchos-arm64_arm64_32_armv7k"

KonanTarget.WATCHOS_SIMULATOR_ARM64,
KonanTarget.WATCHOS_X64 -> "watchos-arm64_i386_x86_64-simulator"

KonanTarget.IOS_ARM32,
KonanTarget.WATCHOS_X86,

KonanTarget.ANDROID_ARM32,
KonanTarget.ANDROID_ARM64,
KonanTarget.ANDROID_X64,
KonanTarget.ANDROID_X86,
KonanTarget.LINUX_ARM32_HFP,
KonanTarget.LINUX_ARM64,
KonanTarget.LINUX_MIPS32,
KonanTarget.LINUX_MIPSEL32,
KonanTarget.LINUX_X64,
KonanTarget.MINGW_X64,
KonanTarget.MINGW_X86,
KonanTarget.WASM32,
is KonanTarget.ZEPHYR -> null
}
4 changes: 1 addition & 3 deletions samples/sample-crashlytics/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@
kotlin.code.style=official
android.useAndroidX=true

org.gradle.jvmargs=-Xmx4g

kotlin.native.cacheKind=none
org.gradle.jvmargs=-Xmx4g
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ abstract class SentryFrameworkValueSource : ValueSource<File, ValueSourceParamet

override fun obtain(): File {
val frameworkFile = findFrameworkBinaryFolder(
execOperations,
"https://github.com/getsentry/sentry-cocoa/releases/download/8.21.0/Sentry.xcframework.zip",
"Sentry"
execOperations = execOperations,
zipUrl = "https://github.com/getsentry/sentry-cocoa/releases/download/8.21.0/Sentry.xcframework.zip",
frameworkName = "Sentry",
zipRelativeFrameworkPath = "Carthage/Build/Sentry.xcframework"
)

return frameworkFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SentryLinkPlugin : Plugin<Project> {

project.kotlinExtension.addFrameworkLinkPath(
frameworkFile = sentryFramework,
linkerName = null,
subpathBlock = ::findXcframeworkSubfolder
)
}
Expand Down

0 comments on commit 798278f

Please sign in to comment.