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

你可能感兴趣的:(Android Studio下编译LatinIME so库CMake版)