6

I am trying to build the AOSP Latin IME (source code: https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master) without downloading the entire AOSP source code. Ideally, I would like to build the project as a Gradle project, so I can easily integrate it with my existing Android apps.

I have already made some progress by

1] creating a blank project in Android Studio

2] copy-pasting the "java" and "java-overridable" folders and copy-pasting the contents of the "res" folder into my project

However, while the project compiles, the keyboard crashes when opened because of the following error:

Could not load native library jni_latinime

This error makes sense because I have not built and included the C++ native library (found here: https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/native/jni/) that is required for the keyboard to function.

How can I build the native library found at the above link and include it in my Gradle project? Is there any way to compile these C++ files without downloading the entire AOSP source?

The project comes with an "Android.bp" file that seems to specify how to compile the C++ files. Unfortunately, I can't figure out how to use the Soong build system. https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/native/jni/Android.bp

Foobar
  • 7,458
  • 16
  • 81
  • 161
  • Quick check, you've [enabled C++ in your project, right](https://developer.android.com/studio/projects/add-native-code#new-project)? – Jake Lee Jan 28 '19 at 15:15
  • @JakeSteam I assumed I wouldn't be able to compile the C++ files in the project itself because AOSP uses a different build system than the normal Gradle-based build system in Android Studio. Did I just over-estimate the complexity of my task? Is it really as simple as copy-pasting in all the C++ files into my `cpp` directory and just setting up a CMakeLists.txt? – Foobar Jan 28 '19 at 18:11
  • I honestly have no idea, but you might as well try? – Jake Lee Jan 28 '19 at 18:30
  • @Roymunson Did you ever figure out how to generate that .so file? – Starwave Jan 15 '20 at 17:25

2 Answers2

0

According to me, you can't use the soong build system, outside of AOSP tree.so to compile native files with Android.bp you will have to build inside AOSP tree.

But can you just checkout to a stable branch, instead of using master branch code.

Soong is the replacement for the old Android make-based build system.
It replaces Android.mk files with Android.bp files,
which are JSON-like simple declarative descriptions of modules to build.



This Makefile-based system is in the process of being replaced with Soong, 
a new build system written in Go. 

During the transition, all of these makefiles are read by Kati, and generate a ninja file instead of being executed directly. 
That's combined with a ninja file read by Soong so that the build graph of the two systems can be combined and run as one.

If you checkout code to a stable branch like pie-release or oreo-r6-release, you will get the source code without this soong build system, and you will have old build system files like Android.mk

But if you still want the latest source code you can read this Android.bp file and create a module native module, and build that with CMake.

You will have to convert this Android.bp file to CMakeLists.txt for building it with cmake. I think that will not be very difficult

Main segement that you want to build is : libjni_latinime
cc_library
{
name: "libjni_latinime",
host_supported: true,
product_specific: true,

sdk_version: "14",
cflags: [
    "-Werror",
    "-Wall",
    "-Wextra",
    "-Weffc++",
    "-Wformat=2",
    "-Wcast-qual",
    "-Wcast-align",
    "-Wwrite-strings",
    "-Wfloat-equal",
    "-Wpointer-arith",
    "-Winit-self",
    "-Wredundant-decls",
    "-Woverloaded-virtual",
    "-Wsign-promo",
    "-Wno-system-headers",

    // To suppress compiler warnings for unused variables/functions used for debug features etc.
    "-Wno-unused-parameter",
    "-Wno-unused-function",
],
local_include_dirs: ["src"],

srcs: [
    "com_android_inputmethod_keyboard_ProximityInfo.cpp",
    "com_android_inputmethod_latin_BinaryDictionary.cpp",
    "com_android_inputmethod_latin_BinaryDictionaryUtils.cpp",
    "com_android_inputmethod_latin_DicTraverseSession.cpp",
    "jni_common.cpp",

    ":LATIN_IME_CORE_SRC_FILES",
],

target: {
android_x86: {
        // HACK: -mstackrealign is required for x86 builds running on pre-KitKat devices to avoid crashes
        // with SSE instructions.
        cflags: ["-mstackrealign"],
    },
    android: {
        stl: "libc++_static",
    },
    host: {
        cflags: ["-DHOST_TOOL"],
    },
}

You have to pass these CFLAGS and include dir, and source file list and from target just pass android libc++_static as other things are not useful for you.

If you want to read about soong: https://android.googlesource.com/platform/build/soong/

nkalra0123
  • 2,281
  • 16
  • 17
  • While I think I can translate the `srcs` directory and `cflags`. I'm not sure how to translate items such as `host_supported` or `product_specific` I also am not sure how to translate any of the targets (e.g--`android_x86`, `android`, or `host`). I guess I'll just have to bite the bullet and figure it out. – Foobar Jan 30 '19 at 15:44
  • android_x86 is for x86 product, most of phones are arm based if you are planning to build libjni_latinime for x86 arch, then you need to use it. Android.bp files require unique names for every module, but a single module can be built in multiple variants, for example by adding host_supported: true. So,i think you can skip these. – nkalra0123 Jan 30 '19 at 16:17
  • But the question is why do you want to use the latest code if you are comfortable using Android.mk In my opinion, pie/oreo code can also be used without any issues. If you checkout code to a stable branch like pie-release or oreo-r6-release, you will get the source code without this soong build system, and you will have old build system files like Android.mk – nkalra0123 Jan 31 '19 at 06:13
  • Well, the issue is also that the `Android.mk` build files aren't actually directly compatible with the ndk-build based system (I think). So either way, I'm going to need to do translations. – Foobar Jan 31 '19 at 07:11
  • 1
    I have been busy but I'm sure this is a simple fix. I will try to find some time, if not I will still try but give the points to nkalra0123 (pity to waste them) My solution would work on 32 bit Windows XP and above build system. ARM apk. – Jon Goodwin Feb 01 '19 at 21:39
  • 1
    I have been busy but I'm sure this is a simple fix. I will try to find some time, if not I will still try but give the points to nkalra0123 (pity to waste them) My solution would work on 32 bit Windows XP and above build system. ARM apk, older build system (Android.mk) **android-soong**, trivial conversion. – Jon Goodwin Feb 01 '19 at 21:47
0

Colin have created a great gradle build for LatinIme. I made a modified latinime for my diploma thesis and i really wished a project like this existed back then.

If you want to change the application package name you will also need to make changes to the cpp files accordingly, because with those files the native libraries are build.

remi0s
  • 87
  • 10