swiftlang/swift-android-examples

Build error when introducing new swift code to hello-swift-java hashing-lib

Closed this issue · 8 comments

Steps to reproduce:

  1. Download the project and follow the install instructions
  2. Make sure hello-swift-java-hashing-app builds successfully on Android Studio
  3. Add the following code
public enum CustomerSupportStoreAction: Sendable {
    case fetchProfile
    case setAccountName(String)
}

OR

public struct CustomerSupportState: Sendable {
    public var accountTierName: String?
    public var priorityTierColor: String?
    public var isLoggedIn: Bool

    public init(accountTierName: String?, priorityTierColor: String?, isLoggedIn: Bool) {
        self.accountTierName = accountTierName
        self.priorityTierColor = priorityTierColor
        self.isLoggedIn = isLoggedIn
    }
}
  1. Delete the folder .build inside hashing-lib otherwise Android studio won't pick up the swift changes
  2. Build hello-swift-java-hashing-app again from Android Studio

The following error is thrown

[700/703] Compiling SwiftHashing SwiftHashing+SwiftJava.swift
/Users/gregorio/Downloads/swift-android-examples-main/hello-swift-java/hashing-lib/.build/plugins/outputs/hashing-lib/SwiftHashing/destination/JExtractSwiftPlugin/Sources/SwiftHashing+SwiftJava.swift:63:10: error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
61 |   let method$ = _JNIMethodIDCache.Method(name: "<init>", signature: "(Ljava/lang/String;)V")
62 |   let constructorID$ = cache$[method$]
63 |   return withVaList([_0.getJNIValue(in: environment!) ?? 0]) {
   |          `- error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
64 |     return environment.interface.NewObjectV(environment, class$, constructorID$, $0)
65 |   }

The function where the error is reported

@_cdecl("Java_com_example_swifthashing_CustomerSupportStoreAction__00024getAsSetProfile__J")
func Java_com_example_swifthashing_CustomerSupportStoreAction__00024getAsSetProfile__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jobject? {
  assert(self != 0, "self memory address was null")
  let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
  let self$ = UnsafeMutablePointer<CustomerSupportStoreAction>(bitPattern: selfBits$)
  guard let self$ else {
    fatalError("self memory address was null in call to \(#function)!")
  }
  guard case .setProfile(let _0) = self$.pointee else {
    fatalError("Expected enum case 'setProfile', but was '\(self$.pointee)'!")
  }
  let cache$ = _JNI_CustomerSupportStoreAction.customerSupportStoreActionSetProfileCache
  let class$ = cache$.javaClass
  let method$ = _JNIMethodIDCache.Method(name: "<init>", signature: "(Ljava/lang/String;)V")
  let constructorID$ = cache$[method$]
  return withVaList([_0.getJNIValue(in: environment!) ?? 0]) {
    return environment.interface.NewObjectV(environment, class$, constructorID$, $0)
  }
} // printCDecl(_:javaMethodName:parentName:parameters:resultType:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+SwiftThunkPrinting.swift:439

I wasn't sure if I should open the issue directly on swift-java.
Let me know if that is preferred.

My environment

openjdk 25.0.1 2025-10-21
OpenJDK Runtime Environment Homebrew (build 25.0.1)
OpenJDK 64-Bit Server VM Homebrew (build 25.0.1, mixed mode, sharing)
echo $JAVA_HOME
/Users/gregorio/.sdkman/candidates/java/current
➜  hashing-lib swiftly run swift sdk list
swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a-android-0.1
➜  android-ndk echo $ANDROID_NDK_HOME                       
/Users/gregorio/android-ndk/android-ndk-r27d
ktoso commented

Delete the folder .build inside hashing-lib otherwise Android studio won't pick up the swift changes

This kinda points to an issue in the gradle build, I'll have a look what's up.

We'll eventually offer a gradle plugin so we don't have to ad hoc invent all this stuff anew all the time. I'll check the example though.

ktoso commented

I can't reproduce the not-rebuilding issue actually... Following the same exact steps rebuilds for me properly.

When you say "add following code" were you adding it in the same swift source file or something new?

As for the


63 |   return withVaList([_0.getJNIValue(in: environment!) ?? 0]) {
   |          `- error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)

you didn't list your Swift version; please make sure you have a 6.2 swift; I can't reproduce this issue on 6.2 so I'm curious what you were using.

swiftly install 6.2 --use, what version was the issue on?

ktoso commented

Ok this reproduces with main-snapshot-2025-10-16 which the build would use;

This is a bug in Swift itself and we'll have to fix it over there.

ktoso commented

Blocked by swiftlang/swift#85210

This seems specific to the use of the Android SDK.

Yes, building from terminal or VSCode was successful. I should've mentioned that.
It was only when building with the android sdk.
Thank you!

ktoso commented

We landed a workaround in swift-java, this should work now it you pull latest swift-java 🖖