最近忙着面试 ,基本上每个面试官都会问一句 会不会jni?会不会ndk?像我这么厉害的c和c++,难道真的要荒废了吗,说了你会别人又不听,那么 我就得写个jni来试试了,说了这么多,要不是一本正经的胡言乱语 我差点都信了 话不多说 开始撸码
1:先建一个as project 这个就省略了,太难了 你们自己摸索
2: 建一个类 名字叫myJNI
package com.bril.jni;
/**
* Created by brill on 2016/9/8.
*/
public class myJNI {
static {
System.loadLibrary("JniTest");
}
public static native String sayHello();
}
对了 先看下我的目录结构
我们继续
3:打开terminal 定位到 这个位置输入 javac生成class文件
C:\android\TestWork\Jni\app\src\main\java\com\bril\jni>javac myJNI.java
4:使用javah生成.h文件 cd.. 到com的上层目录
C:\android\TestWork\Jni\app\src\main\java>javah -jni com.bril.jni.myJNI
发现在java目录下生成了.h文件
然后 new ->folder->jnifolder 然后勾点点 选择finish
4:然后new -> c/c++ source file 输入main 生成main.c
5:把那个生成的文件内容全部拷进来 实现里面的
JNIEXPORT jstring JNICALL Java_com_bril_jni_myJNI_sayHello
(JNIEnv *, jclass);
Java_com_bril_jni_myJNI_sayHello根据每个人的项目路径名不同 实现这个方法为
JNIEXPORT jstring JNICALL Java_com_bril_jni_myJNI_sayHello
(JNIEnv *env, jclass jobj){
return (*env)->NewStringUTF(env,"vvvv lq");
}
6:再新建一个c/c++ source file 因为android studio的bug 必须下面放两个文件 所以。。。。
7 在app 下的gradle文件下添加
ndk {
moduleName "JniTest"
ldLibs "log", "z", "m"
abiFilters "armeabi", "armeabi-v7a", "x86"
}
我的gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.bril.jni"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
ndk {
moduleName "JniTest"
ldLibs "log", "z", "m"
abiFilters "armeabi", "armeabi-v7a", "x86"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jni/'] } }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
}
8:rebuild生成.so文件 build->rebuild project
9在main文件夹下建立 jinLIB文件夹 把.so按文件夹复制进去
10调用jni
package com.bril.jni;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.txt);
text.setText(myJNI.sayHello());
}
}
如果其中报错的话看看 local.properties 和 gradle.properties
local.properties 文件
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Thu Sep 08 10:12:30 SGT 2016
ndk.dir=C\:\\android\\sdk\\ndk-bundle
sdk.dir=C\:\\android\\sdk
gradle.properties 文件
## Project-wide Gradle settings.
#
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Thu Sep 08 10:34:51 SGT 2016
android.useDeprecatedNdk=true