Android Studio下编译LatinIME so库CMake版

继上次Android Studio下编译LatinIME so库NDK版,这次简单记录一下使用CMake编译的so库


编译环境

  • Android Studio 3.1.3
  • gradle 4.4-all
  • cmake (sdk目录下)
  • ndk-bundle (sdk目录下)
  • 源码 Android 8.1
    (1) packages目录的LatinIME
    (2) 支持包framework的opt目录下的 inputmethodcommon

确保cmake与ndk-bundle已经下载了并配置好

cmake ndk-bundle.png

ndk-bundle路径.png

导入源码,如图
cmake导入LatinIME源码.png

在源码的native目录下新建 CMakeList.txt文件,加入cmake的配置


cmake_minimum_required(VERSION 3.4.1)

# 二进制码剥除
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")

add_library(jni_latinime SHARED
            jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
            jni/com_android_inputmethod_latin_BinaryDictionary.cpp
            jni/com_android_inputmethod_latin_BinaryDictionaryUtils.cpp
            jni/com_android_inputmethod_latin_DicTraverseSession.cpp
            jni/jni_common.cpp
            jni/src/dictionary/header/header_policy.cpp
            jni/src/dictionary/header/header_read_write_utils.cpp
            jni/src/dictionary/property/ngram_context.cpp
            jni/src/dictionary/structure/dictionary_structure_with_buffer_policy_factory.cpp
            jni/src/dictionary/structure/backward/v402/ver4_dict_buffers.cpp
            jni/src/dictionary/structure/backward/v402/ver4_dict_constants.cpp
            jni/src/dictionary/structure/backward/v402/ver4_patricia_trie_node_reader.cpp
            jni/src/dictionary/structure/backward/v402/ver4_patricia_trie_node_writer.cpp
            jni/src/dictionary/structure/backward/v402/ver4_patricia_trie_policy.cpp
            jni/src/dictionary/structure/backward/v402/ver4_patricia_trie_reading_utils.cpp
            jni/src/dictionary/structure/backward/v402/ver4_patricia_trie_writing_helper.cpp
            jni/src/dictionary/structure/backward/v402/ver4_pt_node_array_reader.cpp
            jni/src/dictionary/structure/backward/v402/bigram/ver4_bigram_list_policy.cpp
            jni/src/dictionary/structure/backward/v402/content/bigram_dict_content.cpp
            jni/src/dictionary/structure/backward/v402/content/probability_dict_content.cpp
            jni/src/dictionary/structure/backward/v402/content/shortcut_dict_content.cpp
            jni/src/dictionary/structure/backward/v402/content/sparse_table_dict_content.cpp
            jni/src/dictionary/structure/backward/v402/content/terminal_position_lookup_table.cpp
            jni/src/dictionary/structure/pt_common/dynamic_pt_gc_event_listeners.cpp
            jni/src/dictionary/structure/pt_common/dynamic_pt_reading_helper.cpp
            jni/src/dictionary/structure/pt_common/dynamic_pt_reading_utils.cpp
            jni/src/dictionary/structure/pt_common/dynamic_pt_updating_helper.cpp
            jni/src/dictionary/structure/pt_common/dynamic_pt_writing_utils.cpp
            jni/src/dictionary/structure/pt_common/patricia_trie_reading_utils.cpp
            jni/src/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.cpp
            jni/src/dictionary/structure/pt_common/shortcut/shortcut_list_reading_utils.cpp
            jni/src/dictionary/structure/v2/patricia_trie_policy.cpp
            jni/src/dictionary/structure/v2/ver2_patricia_trie_node_reader.cpp
            jni/src/dictionary/structure/v2/ver2_pt_node_array_reader.cpp
            jni/src/dictionary/structure/v4/ver4_dict_buffers.cpp
            jni/src/dictionary/structure/v4/ver4_dict_constants.cpp
            jni/src/dictionary/structure/v4/ver4_patricia_trie_node_reader.cpp
            jni/src/dictionary/structure/v4/ver4_patricia_trie_node_writer.cpp
            jni/src/dictionary/structure/v4/ver4_patricia_trie_policy.cpp
            jni/src/dictionary/structure/v4/ver4_patricia_trie_reading_utils.cpp
            jni/src/dictionary/structure/v4/ver4_patricia_trie_writing_helper.cpp
            jni/src/dictionary/structure/v4/ver4_pt_node_array_reader.cpp
            jni/src/dictionary/structure/v4/content/dynamic_language_model_probability_utils.cpp
            jni/src/dictionary/structure/v4/content/language_model_dict_content.cpp
            jni/src/dictionary/structure/v4/content/language_model_dict_content_global_counters.cpp
            jni/src/dictionary/structure/v4/content/shortcut_dict_content.cpp
            jni/src/dictionary/structure/v4/content/sparse_table_dict_content.cpp
            jni/src/dictionary/structure/v4/content/terminal_position_lookup_table.cpp
            jni/src/dictionary/utils/buffer_with_extendable_buffer.cpp
            jni/src/dictionary/utils/byte_array_utils.cpp
            jni/src/dictionary/utils/dict_file_writing_utils.cpp
            jni/src/dictionary/utils/file_utils.cpp
            jni/src/dictionary/utils/forgetting_curve_utils.cpp
            jni/src/dictionary/utils/format_utils.cpp
            jni/src/dictionary/utils/mmapped_buffer.cpp
            jni/src/dictionary/utils/multi_bigram_map.cpp
            jni/src/dictionary/utils/probability_utils.cpp
            jni/src/dictionary/utils/sparse_table.cpp
            jni/src/dictionary/utils/trie_map.cpp
            jni/src/suggest/core/suggest.cpp
            jni/src/suggest/core/dicnode/dic_node.cpp
            jni/src/suggest/core/dicnode/dic_node_utils.cpp
            jni/src/suggest/core/dicnode/dic_nodes_cache.cpp
            jni/src/suggest/core/dictionary/dictionary.cpp
            jni/src/suggest/core/dictionary/dictionary_utils.cpp
            jni/src/suggest/core/dictionary/digraph_utils.cpp
            jni/src/suggest/core/dictionary/error_type_utils.cpp
            jni/src/suggest/core/layout/additional_proximity_chars.cpp
            jni/src/suggest/core/layout/proximity_info.cpp
            jni/src/suggest/core/layout/proximity_info_params.cpp
            jni/src/suggest/core/layout/proximity_info_state.cpp
            jni/src/suggest/core/layout/proximity_info_state_utils.cpp
            jni/src/suggest/core/policy/weighting.cpp
            jni/src/suggest/core/result/suggestion_results.cpp
            jni/src/suggest/core/result/suggestions_output_utils.cpp
            jni/src/suggest/core/session/dic_traverse_session.cpp
            jni/src/suggest/policyimpl/gesture/gesture_suggest_policy_factory.cpp
            jni/src/suggest/policyimpl/typing/scoring_params.cpp
            jni/src/suggest/policyimpl/typing/typing_scoring.cpp
            jni/src/suggest/policyimpl/typing/typing_suggest_policy.cpp
            jni/src/suggest/policyimpl/typing/typing_traversal.cpp
            jni/src/suggest/policyimpl/typing/typing_weighting.cpp
            jni/src/utils/autocorrection_threshold_utils.cpp
            jni/src/utils/char_utils.cpp
            jni/src/utils/jni_data_utils.cpp
            jni/src/utils/log_utils.cpp
            jni/src/utils/time_keeper.cpp)

include_directories(jni/src/)

target_link_libraries(jni_latinime
                      android
                      log
                      z)

在源码根目录下新建build.gradle文件

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 1
        versionName = "1.0"
        
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
            }
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'java/AndroidManifest.xml'
            java.srcDirs = ['java/src', 'java-overridable/src', 'common/src', 'inputmethodcommon/java']
            res.srcDirs = ['java/res']
        }
    }
    externalNativeBuild {
        cmake {
            path 'native/CMakeLists.txt'
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        htmlReport false
        abortOnError false
        disable 'MissingTranslation'
        disable 'ExtraTranslation'
    }
}

dependencies {
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.google.code.findbugs:jsr305:3.0.0'
}

打开Android Studio并导入工程,同步完成之后点击构建

点击构建.png

构建完成后,在目录 build\intermediates\cmake\release\obj下即可找到so库
so库.png

或者直接从生成的apk中用压缩包打开,将lib目录拖出来就是so库了。
源码点这里LatinIME/cmake

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,377评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,390评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,967评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,344评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,441评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,492评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,497评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,274评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,732评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,008评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,184评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,837评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,520评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,156评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,407评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,056评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,074评论 2 352

推荐阅读更多精彩内容