swift-klib-plugin icon indicating copy to clipboard operation
swift-klib-plugin copied to clipboard

getting error : could not build Objective-C module 'AVFoundation' error: fatalError

Open Vaibhav-214 opened this issue 2 years ago • 2 comments

Issue Description

I'm encountering an issue while building my Swift file AudioRecorderImpl.swift, which requires AVFoundation. The error message I'm getting is:

could not build Objective-C module 'AVFoundation' error: fatalError

My swift code

// AudioRecorderImpl.swift

import Foundation
import AVFoundation

@objc class AudioRecorderImpl: NSObject, AVAudioRecorderDelegate {
    private var audioRecorder: AVAudioRecorder?
    private var audioFileURL: URL?

    @objc func startRecording() {
        // ... (your existing code)
    }

    @objc func stopRecording() -> NSData {
        // ... (your existing code)
    }
}

##My directory structure

Mirror (root package)
|-- Compose App
|      |-- native
|      |     |-- AudioRecorderImpl.swift
|      |-- src
|           |-- androidmain
|           |-- commonmain
|           |-- iosmain

##inside gradle.kts


import org.jetbrains.compose.ExperimentalComposeLibrary

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.jetbrainsCompose)
    
    alias(libs.plugins.kotlinxSerialization)
    alias(libs.plugins.sqlDelight)
    alias(libs.plugins.swiftklib)

}

sqldelight {
    databases {
        create("MirrorLocalDatabase") {
            packageName.set("org.example.mirror.database")
        }
    }
}

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }
    
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach { iosTarget ->
        iosTarget.binaries.framework {
            baseName = "ComposeApp"
            isStatic = true
        }
        iosTarget.compilations {
            val main by getting {
                cinterops {
                    create("AudioRecorderImpl")
                }
            }
        }
    }
    
    sourceSets {
        
        androidMain.dependencies {
            //android specific dependencies
        }
        iosMain.dependencies {
            //ios  specific dependencies
        }

        commonMain.dependencies {
            //common dependencies
        }
    }
}

android {
    //android section
}

swiftklib {
    create("AudioRecorderImpl") {
        path = file("native")
        packageName("org.example.mirror")
    }
}

Vaibhav-214 avatar Jan 25 '24 11:01 Vaibhav-214

I'm facing the same error, have you solved it @Vaibhav-214 ?

jcaiqueoliveira avatar Feb 11 '24 00:02 jcaiqueoliveira

@jcaiqueoliveira Nopes couldn't get it to work, I used a different method for my usecase.

Created an interface in iosMain and implemented that in Swift and with some DI, it is working for me.

What exactly use your usecase?

Vaibhav-214 avatar Feb 11 '24 03:02 Vaibhav-214

@Vaibhav-214 try wrapping in compiler directives:

#if canImport(RadioPlayer)

or

#if os(iOS)

markst avatar Jun 19 '24 01:06 markst

@Vaibhav-214 This issue should be fixed in https://github.com/ttypic/swift-klib-plugin/releases/tag/v0.6.2

IlyaGulya avatar Jul 21 '24 10:07 IlyaGulya

Please reopen if issue persists

IlyaGulya avatar Aug 02 '24 07:08 IlyaGulya