android studio裡gradle3.0以上實現jni呼叫
gradle3.0以上進行jni呼叫的時候和之前的版本會略有不同
首先第一步:
首先新建一個工程, 我的工程目錄如下:
我本地使用的gradle版本:
app的build.gradle內容如下所示:
android { apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { applicationId "com.jni.jnidemo5" minSdkVersion 26 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.0.2' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' }
以上就是新建的工程的基本設定, 接下來就是正式開始:
首先在包下新建一個JNINative.java的類用於寫native方法
package com.jni.jnidemo5;
public class JNINative {
public native String getString();
}
很簡單隻有一個獲取字串的方法.
使用javah生成對應的.h檔案
具體方法如下:
開啟Android Studio的Terminal(控制檯)
輸入: cd app/src/main/java
輸入: javah com.jni.jnidemo5.JNINative
會在工程的 app/src/main/java/下生成對應的.h檔案
生成的.h檔案內容如下:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_jni_jnidemo5_JNINative */ #ifndef _Included_com_jni_jnidemo5_JNINative #define _Included_com_jni_jnidemo5_JNINative #ifdef __cplusplus extern "C" { #endif /* * Class: com_jni_jnidemo5_JNINative * Method: getString * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_jni_jnidemo5_JNINative_getString (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
編寫對應的C檔案
在src上右鍵新建一個jni目錄, 目錄結構如下
在jni目錄下右鍵 新建一個hellotest.c檔案
檔案的內容如下(根據生成的.h檔案修改而成):
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jni_jnidemo5_JNINative */
#ifndef _Included_com_jni_jnidemo5_JNINative
#define _Included_com_jni_jnidemo5_JNINative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_jni_jnidemo5_JNINative
* Method: getString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_jni_jnidemo5_JNINative_getString
(JNIEnv *env, jclass jobject){
return (*env)->NewStringUTF(env,"hello world from c!!!");
}
#ifdef __cplusplus
}
#endif
#endif
注意這裡修改的內容:
配置NDK環境
點選project structure 設定工程的ndk目錄
接下來是和低版本的gradle不一致的地方了, 因為gradle3.0以上useDeprecatedNdk這個東西不推薦使用了, 也就是我們不需要在gradle.properties裡新增android.useDeprecatedNdk=true這句話了
如果我們還使用這個東東, studio就會提示我們:
Error:Execution failed for task ':app:compileDebugNdk'. > Error: Flag android.useDeprecatedNdk is no longer supported and will be removed in the next version of Android Studio. Please switch to a supported build system.Consider using CMake or ndk-build integration.
推薦我們使用CMake, 這裡我就選擇CMake方式
CMake方式實現編譯so檔案
- 開啟SDK Manager
勾選箭頭指向的兩個外掛, 安裝
2.在app的build.gradle裡的deaultConfig節點裡新增如下程式碼:
externalNativeBuild {
cmake {
cppFlags ""
//生成多個版本的so檔案
abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'
}
}
如下圖所示:
3.在android節點裡新增如下程式碼
externalNativeBuild {
cmake {
path "CMakeLists.txt" // 該檔案配置了c原始碼位置,以及編譯後so檔案的名字
}
}
如下圖所示
4.在和app裡的build.gradle的同級目錄裡新建一個名為CMakeLists.txt的檔案, 並copy如下程式碼
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
#CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
# 設定so檔名稱.
hello
# Sets the library as a shared library.
SHARED
# 設定這個so檔案為共享.
# Provides a relative path to your source file(s).
# 設定我們的c/c++檔案路徑, 注意檔名要和我們的c檔案相同
src/main/jni/hellotest.c)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
# 制定目標庫.注意要和上面的so檔名稱一致
hello
# Links the target library to the log library
# included in the NDK.
${log-lib} )
注意自己要修改裡面帶註釋的地方, 主要是c檔名和編譯後的so的檔名
5.然後Build工程我們就能看到編譯成功的so庫檔案了, 如下所是:
6.Copy生成的so檔案到lib目錄下:
7.修改MainActivity裡的程式碼讓我們的程式跑起來
package com.jni.jnidemo5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv;
static {
System.loadLibrary("hello");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
tv.setText(new JNINative().getString());
}
}
點選run執行結果如下:
demo下載地址: