实现加减乘除
Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
EditText editText;
EditText editText1;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.b1).setOnClickListener(this);
findViewById(R.id.b2).setOnClickListener(this);
findViewById(R.id.b3).setOnClickListener(this);
findViewById(R.id.b4).setOnClickListener(this);
editText = findViewById(R.id.ed1);
editText1 = findViewById(R.id.ed2);
textView = findViewById(R.id.t1);
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native int jia(int a,int b);
public native int jian(int a,int b);
public native int cheng(int a,int b);
public native int chu(int a,int b);
@Override
public void onClick(View v) {
double num=0;
String s = editText.getText().toString();
String s1 = editText1.getText().toString();
int a = Integer.parseInt(s);
int b = Integer.parseInt(s1);
switch (v.getId()){
case R.id.b1:
num = jia(a,b);
break;
case R.id.b2:
num = jian(a,b);
break;
case R.id.b3:
num=cheng(a,b);
break;
case R.id.b4:
num=chu(a,b);
break;
}
textView.setText(num+"");
}
}
native-lib.cpp
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_day15lianxi1_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_day15lianxi1_MainActivity_jia(JNIEnv *env, jobject instance, jint a, jint b) {
// TODO
return a+b;
}extern "C"
JNIEXPORT jint JNICALL
Java_com_example_day15lianxi1_MainActivity_jian(JNIEnv *env, jobject instance, jint a, jint b) {
// TODO
return a-b;
}extern "C"
JNIEXPORT jint JNICALL
Java_com_example_day15lianxi1_MainActivity_cheng(JNIEnv *env, jobject instance, jint a, jint b) {
// TODO
return a*b;
}extern "C"
JNIEXPORT jint JNICALL
Java_com_example_day15lianxi1_MainActivity_chu(JNIEnv *env, jobject instance, jint a, jint b) {
// TODO
return a/b;
}
xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_weight="1"
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_weight="1"
android:id="@+id/ed2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/b1"
android:text="加"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/b2"
android:text="减"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/b3"
android:text="乘"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/b4"
android:text="除"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/t1"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
效果
下载ndk:
创建 JNI 文件夹
直接在项目右键,选择 New - Folder - JNI Folder ,对话框直接点击 Finish 即可方便地在默认位置创建 jni 文件夹用于存放 c 源码。默认位置在 app/src/main/jni.
创建 Java 类
首先创建一个 Java 类用于调用 c 代码。
生成头文件 (.h) 命令行
最直接的方式就是通过命令行生成。
首先使用 javac 编译 java 文件。
小技巧。使用右键按住拖动文件夹到终端面板,可以快速进入对应目录。
进入到文件所在目录后执行 javac JniTest.java 编译。成功后会出现 JniTest.class 文件。
然后退回到包外目录执行 javah -jni 生成头文件。
注意路径不要写错了,最后也不需要加文件扩展名。成功后会生成一个 .h 文件,把它手动移到 jni 目录。之前编译出的 .class 文件可以删掉了。最终目录结构如下:
编写 c 代码
在 jni 目录新建 一个 c 语言源码,这里叫做 JinLib.cpp. 然后实现头文件中所定义的函数,别忘引入头文件。这里简单地返回一个字符串
创建 mk 文件
mk 文件用于告诉 ndk-build 该如何编译 c 源码,详情见官方指南。
在 jni 目录下创建 Android.mk:
其中 LOCAL_SRC_FILES 列出了所有要编译的 c 源码文件。
然后创建 Application.mk:
gradle 配置:
在 module 的 build.gradle 里,android.defaultConfig 下加入下面配置:
编译:
首先配置环境,在Path里设置路径,可以直接使用ndk-build。
在cmd里进入jni目录里,使用ndk-build编译
NDK介绍
定义:Native Development Kit,是 Android的一个工具开发包
NDK是属于 Android 的,与Java并无直接关系
作用:快速开发C、 C++的动态库,并自动将so和应用一起打包成 APK。
可通过 NDK在 Android中使用 JNI与本地代码(如C、C++)交互
步骤
1, 配置 Android NDK环境
A, Android 原生开发工具包:就是ndk工具包。
B, LLDB:一种调试程序,Android Studio 使用它来调试原生代码。
C, 下载这些组件:
D, 在打开的Android Studio项目中,从菜单栏选择 Tools > Android > SDK Manager。
E, 点击 SDK Tools 标签。
F, 选中 LLDB、CMake 和 NDK 旁的复选框