發表文章

目前顯示的是 5月, 2019的文章

Android NDK (使用Android Studio 3.4)

圖片
Hi 大家好,我是Taco. 想不到過一陣子又回來弄NDK了。 但是看看Google NDK的文件發現好像跟以前有點不同, 所以來做個小筆記哩。 以下範例是為~ 原專案加上全新的native library為例(非匯入現有的c/c++ libraray) 參考資料如下: Create Hello-CMake with Android Studio Getting Started with the NDK JNI tips 重要推薦!: 直接看 codelabs 的快速範例,可以直接幫新專案加入下面提到的這些東西。 概略步驟: 下載NDK & tools 到Tools -> SDK Manager的SDK Tools,把這些東西下載好: NDK: 讓你可以在Android上執行C/C++ code CMake: 讓Gradle可以build你的 native library. LLDB: dubug 工具 新增native library資料夾& .cpp檔案 ( 請照這個做 ) 新增.cpp檔案後,可以參考這個來寫JNI interface (JNI參考資料) // native-lib.cpp #include #include extern "C" JNIEXPORT jstring JNICALL Java_com_stan_taco_testnative_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); } // MainActivity.kt external fun stringFromJNI(): String companion object { // Used to load the 'native-lib' library on application startup. init {