- 在Java类中添加Native方法
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private native String HelloFromJNI();
}
- 创建JNI文件夹
3.添加javah和ndk-build为 External Tools
4.执行javah生成.h文件
5.配置gradle
android {
...
defaultConfig {
...
ndk {
moduleName "HelloJNI"
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir "src/main/libs"
}
...
}
...
}
参考:http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/