DatL4g / Sekret

🔒 Deeply hide secrets with Kotlin Multiplatform
Apache License 2.0
41 stars 0 forks source link

iOS native target support #2

Closed maicol07 closed 8 months ago

maicol07 commented 8 months ago

Hi, is it possible to generate iOS native files? I've added

kotlin {
  // ....
  iosArm64 {
    binaries {
      sharedLib()
    }
  }
  iosSimulatorArm64 {
    binaries {
      sharedLib()
    }
  }
  iosX64 {
    binaries {
      sharedLib()
    }
  }
// ....
}
val jniNativeMain by creating {
        // ....
      iosMain.orNull?.dependsOn(this)
    }

    val jniMain by creating {
    // ....
      iosMain.orNull?.dependsOn(this)
    }

However, this gives me a lot of undefined references errors. Can you help me? Thanks

DatL4g commented 8 months ago

Yes it's possible, just edit the build.gradle.kts to your needs.

I'm not a iOS developer myself, however I think you have to configure it like this:

iosX64{
    binaries { 
        sharedLib() // choose one
        staticLib() // choose one
        framework { 
            baseName = "sekret"
            isStatic = // depends on the chosen at top
        }
    }
}

As I said I'm not a iOS developer so I can't test or make sure all references are resolved, but the library is fully native compatible.

maicol07 commented 8 months ago

🤔 I don't think it works... I get the same errors on two machines (Windows and MacOS) when generating native libraries... These are all the not-found references: image

DatL4g commented 8 months ago

Which version do you use?

maicol07 commented 8 months ago

1.0.1

DatL4g commented 8 months ago

Please update to version 2.0.0-alpha-01. It's safe to use for your purpose. It's important to bump the version in sekret/build.gradle.kts (the file we discussed before) as well.

Make sure to follow the "upgrade guide": https://github.com/DatL4g/Sekret/releases

DatL4g commented 8 months ago

After updating the gradle plugin you could also use the new generateSekretBuildScript gradle task, to make sure everything is setup correctly.

maicol07 commented 8 months ago

Same issue even after upgrading to 2.0a1. The build script command actually removes iosMain from the jni blocks

DatL4g commented 8 months ago

iOS doesn't work in the jni build, since iOS doesn't run on the JVM. Only android, linux, macos and windows is supported there.

Removing iosMain from jniNativeMain and jniMain will resolve all issues. iOS runs completely native so all you need are the ios targets, native bindings are generated accordingly.

maicol07 commented 8 months ago

Ok, thank you. However, doing this I can't find the Sekret and NativeLoader classes in iosMain. How can I access my secrets?

DatL4g commented 8 months ago

The NativeLoader is needed on android and jvm only.

iOS (and other native targets) have direct bindings. Please take a look here: https://kotlinlang.org/docs/native-objc-interop.html#top-level-functions-and-properties

maicol07 commented 8 months ago

Works! Thank you! For future readers, if you have a secret like this:

MY_SECRET="123"

Just use it as a function in your iosMain code:

mySecret()
DatL4g commented 8 months ago

This may change in the next version to Sekret.mySecret()

maicol07 commented 8 months ago

Yeah, that may be better, so it can be used without extra code in the common module

DatL4g commented 6 months ago

New release has a common Sekret class now

https://github.com/DatL4g/Sekret/releases/tag/v2.0.0-alpha-04