Gryphon logo
Home Tutorial GitHub Twitter

Adding Gryphon to an existing app

Note: This guide assumes a certain level of familiarity with the iOS and Android ecosystem; for a more beginner-friendly introduction, check out Translating a new iOS app to Android. This guide also focuses on Xcode integration, which is only available on macOS; however, Linux and Docker users might still find important information here that they can use with the command line interface.

Step 1: Setting up the project

Start by navigating to your iOS app’s directory and running gryphon init:

$ gryphon init myAwesomeiOSApp.xcodeproj/

If your Xcode project has more than one target that compiles Swift code, it might be necessary to specify the target you want Gryphon to translate:

$ gryphon init myAwesomeiOSApp.xcodeproj/ --target=<target name>

It is important to call gryphon init in the folder that contains the Xcode project, otherwise the new Xcode targets might not work.

Step 2: Adding the libraries

Gryphon uses two library files for its translations: one for the iOS app, and one for the Android app. You can generate them by running:

$ gryphon generate-libraries

The two files will be created in the current directory. Add the GryphonSwiftLibrary.swift file to the iOS app by dragging it to Xcode, and add the GryphonKotlinLibrary.kt file to the Android app by dragging it to Android Studio.

Then, replace the placeholder package statement at the beginning of the GryphonKotlinLibrary.kt file with your app’s package identifier.

package com.example.myawesomeandroidapp

Make sure both files are being compiled in their respective apps - files translated with Gryphon will need access to their code.

Step 3: Setting up the shared files

Start by selecting which of your app’s files you want to translate. It’s likely the selected files will need to be adapted to be used with Gryphon, so it’s recommended you start with only a few files and increase their number gradually.

You can also choose not to translate some parts of a file by commenting declarations with // gryphon ignore:

// gryphon ignore
class MyClass {
	// ...
}

Any declarations marked with this comment will not be translated. This works for most supported declarations, such as extensions, protocols, functions, etc. Just make sure the comment goes before the declaration. For more information, check out the translation comments guide.

Once you decide which files will be translated, add them to the new gryphonInputFiles.xcfilelist file, separated by newlines:

MyAwesomeiOSApp/SharedFile.swift
MyAwesomeiOSApp/AnotherSharedFile.swift
...

Now open each of these files and add a // gryphon output: comment saying where you want its Kotlin translation to be placed. Note that the paths should be relative to the location of the Xcode project, not the location of the files themselves.

// gryphon output: ../MyAwesomeAndroidApp/app/src/main/java/com/example/myawesomeandroidapp/SharedFile.kt

You might also want to add a package statement to the start of each file using a gryphon insert comment:

// gryphon insert: package com.example.myawesomeandroidapp

Step 4: Adapting the shared files

Switch to the Gryphon target and hit build (⌘+B).

Xcode's target switcher

Gryphon will probably raise some warnings. It’s recommended that you fix these warnings, as they usually indicate potential bugs in the translations. If you have problems, try reading the guides on translation comments, templates and collections for more information.

A few tips to handle common warnings:

If you know the code is being translated correctly, you can also mute any warning with a // gryphon mute comment.

Step 5: Building the Android app with Xcode

On Xcode, click on your Xcode project in the navigator, then click on the new Kotlin target. Open its Build Settings and search for a setting called ANDROID_ROOT. Change its value so that it points to your Android app’s directory - specifically, the directory containing your gradlew file. This path should be relative to the location of the Xcode project.

The Kotlin target's Build Settings in Xcode

Once that’s done, you can switch to the Kotlin target and hit build (⌘+B). Xcode will compile your Android app and show any Kotlin warnings or errors in the Swift lines that originated them, so you can solve the issues at the source.