Keyboard SDK Android

To start working with Virtual Keyboard SDK, we first need to sync the required dependency, set required flags, and finally create an input service extending the KeyboardService class.

To use the SDK, please note that you first need a license key which can be requested in the developer’s dashboard.
The below steps assume that you already have an existing project to add Virtual Keyboard SDK support. If you lack a current project, follow the steps from official Android documentation to create a new project.

Setup

Let’s set up your Sample keyboard, which takes just a few minutes. Follow the instructions below to add the keyboard SDK to an existing application.

Step 1

Add Fleksy SDK repositories to the settings.gradle file.

17
18
19
20
21
22
23
24
25
26
27
...
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
            url = "https://maven.fleksy.com/maven"
        }
    }
}...

Step 2

Set android.enableJetifier to true in your gradle.properties file:

android.enableJetifier=true

Step 3

Edit the app’s module build.gradle file, and add dependencies:

17
18
19
20
21
22
23
...
dependencies {
...       
  // Keyboard Dependencies
  implementation "co.thingthing.fleksycore:fleksycore-release:3.5.25"
}
...

Step 4

Ensure Java 1.8 (or higher) compatibility is enabled in the app’s module build.gradle file:

17
18
19
20
21
22
23
24
25
26
27
android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toString()
  }
}

Step 5

Disable compression for json and wav files in your app’s module build.gradle file:

17
18
19
20
21
22
android {
  ...
  aaptOptions {
    noCompress '.json', '.wav'
  }
}

Step 6

  1. Sync project with Gradle Files to load the keyboard SDK dependencies.
  2. Create a kotlin class for the keyboard service and inherit from the KeyboardService class.
  3. Override the method createConfiguration to return a configuration as shown below.
  4. Replace <your-license-key> and <your-license-secret> with your license.
It is recommended NOT to commit your license keys in the code but to store them as variables in the environment and access them during the compilation.

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import co.thingthing.fleksy.core.keyboard.KeyboardConfiguration
import co.thingthing.fleksy.core.keyboard.KeyboardService

class SampleKeyboardService : KeyboardService() {

    override fun createConfiguration(): KeyboardConfiguration {
        return KeyboardConfiguration(
            license = KeyboardConfiguration.LicenseConfiguration(
                licenseKey = "<your-license-key>",
                licenseSecret = "<your-license-secret>"
            )
        )
    }

}

Step 7

Create a new input-method file named sample_input_method.xml in the res/xml folder of your app’s module, and copy the following contents:

<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android"
    android:icon="@mipmap/ic_launcher"
    android:isDefault="true">
    <subtype
        android:imeSubtypeExtraValue="EmojiCapable,AsciiCapable,TrySuppressingImeSwitcher"
        android:imeSubtypeMode="keyboard"
        android:label="%s"
        android:overridesImplicitlyEnabledSubtype="true" />
    <subtype
        android:imeSubtypeExtraValue="AsciiCapable"
        android:imeSubtypeLocale="en_US"
        android:imeSubtypeMode="keyboard"
        android:isAsciiCapable="true" />
</input-method>

Step 8

  1. Add an input method service to your AndroidManifest.xml file, as shown below.
  2. Update android:name of the service to the class name that was created in step 6.

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="co.thingthing.samplesdk">
   <application
    ...
    <service
        android:name=".SampleKeyboardService"
        android:directBootAware="true"
        android:exported="true"
        android:permission="android.permission.BIND_INPUT_METHOD">
        <intent-filter>
            <action android:name="android.view.InputMethod" />
        </intent-filter>
        <meta-data
            android:name="android.view.im"
            android:resource="@xml/sample_input_method" />
     </service>
  </application>
</manifest>

Step 9

Finally, download & copy the English language pack resourceArchive-en-US.jet to the assets/encrypted folder of the main app module.

The assets/encrypted directory is absent by default on new projects unless created manually.

For example:

With a terminal open at the root of the android project, and with the provided language pack stored in the ~/Downloads folder:

  1. Create the encrypted folder inside the assets folder:

    $ mkdir -p app/src/main/assets/encrypted
    

  2. Copy the language pack to the assets/encrypted folder:

    $ cp ~/Downloads/resourceArchive-en-US.jet app/src/main/assets/encrypted
    

Your project is now ready to be built and run on a device of your choice.

Optional Steps

Reduce APKs size

Add ABI split to the app’s module build.gradle file to build APKs with only the necessary dynamic libraries for each architecture. This reduces the APK size delivered to users.

17
18
19
20
21
22
23
24
25
26
27
android {
  ...
  splits {
    abi {
      enable true
      reset()
      include "x86", "armeabi-v7a", "arm64-v8a", "x86_64"
      universalApk false
    }
  }
}

Additional Resources


If something needs to be added or if you find an error in our documentation, please let us know either on our GitHub or Discord.

Last updated on January 15, 2024