windows下 android studio 的NDK開發環境配置(1)(2)
阿新 • • 發佈:2019-01-06
參考博主文章連結:
開發環境:
windows10
AS2.1.2
首先安裝NDK,
安裝完後,新建一個空專案。
點選下一步,知道新建一個新專案。
新建好的專案如下:
檢測local.properties 檔案裡面是否有ndk的路徑,沒有加上。
然後看app\build.gradle
加上這段程式碼
ndk{
moduleName “MyLibrary”
}
sourceSets.main{
jni.srcDirs = []
jniLibs.srcDir “src/main/libs”
}
別加錯位置了。
在gradle.properties中新增 Android.useDeprecatedNdk=true
然後新建JNI資料夾。
下面開始配置工具,
配置這3個工具,
配置工作結束。
可以在tool下面看到自己新增的3個工具。
下面開始新增檔案。
新建 一個class檔案,並加上如下程式碼,
這個是定義JNI函式介面以及載入編譯c/c++ 檔案的庫檔案。
新生成一個.h 標頭檔案。把這個檔案直接拖到jni下面去。
在JNI資料夾下面新增 .c檔案,以及make檔案。
test.c 如下
#include "test_myapplication_myndk.h"
JNIEXPORT jint JNICALL Java_test_myapplication_myndk_getint
(JNIEnv * env, jobject obj){
return 100;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyLibrary
LOCAL_SRC_FILES =: test.c
include $(BUILD_SHARED_LIBRARY)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
application.mk
APP_MODULES := MyLibrary
APP_ABI := all
- 1
- 2
- 1
- 2
開始build ndk
目錄下面多了libs,obj目錄。
然後就開始使用我們的介面。
在xml中新增textview的id
在mainactivity 中新增如下程式碼
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tv);
int r = new myndk().getint();
tv.setText(Integer.toString(r));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14