NDK JNI이용하여 C/C++ 라이브러리 호출하기
- gradle 설정 : http://ghj1001020.tistory.com/761 참고
- calculator 라는 C파일(라이브러리)을 만들어서 JNI를 통한 호출
MainActivity.java : Jni 로드및 호출
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.ghj.jnitwo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView txtResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int add = getCalculator(12, 3, '+'); int minus = getCalculator(12, 3, '-'); int multiply = getCalculator(12, 3, '*'); int divide = getCalculator(12, 3, '/'); txtResult.append("덧셈 : "+add+"\n"); txtResult.append("뺄셈 : "+minus+"\n"); txtResult.append("곱셈 : "+multiply+"\n"); txtResult.append("나눗셈 : "+divide+"\n"); } static { //JNI 로드 System.loadLibrary("jni-calculator"); } public native int getCalculator(int a, int b, char type); } | cs |
jni-calculator.c : Jni 파일
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <jni.h> #include "calculator.h" /** * @desc : JNI 를 이용하여 c/c++ 로 된 라이브러리를 호출할 수 있다 */ JNIEXPORT jint JNICALL Java_com_ghj_jnitwo_MainActivity_getCalculator(JNIEnv *env, jobject instance, jint a, jint b, jchar type) { if(type == '+'){ add(a, b); }else if(type == '-'){ minus(a, b); }else if(type == '*'){ multiply(a, b); }else if(type == '/'){ divide(a, b); } } | cs |
calculator.h : 임의의 라이브러리 헤더파일
1 2 3 4 5 6 7 8 9 10 | #ifndef JNITWO_CALCULATOR_H #define JNITWO_CALCULATOR_H extern int add(int a, int b); extern int minus(int a, int b); extern int multiply(int a, int b); extern int divide(int a, int b); #endif //JNITWO_CALCULATOR_H | cs |
calculator.c : 임의의 라이브러리 소스파일
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include "calculator.h" //덧셈 int add(int a, int b){ return a + b; } //뺄셈 int minus(int a, int b){ return a - b; } //곱셈 int multiply(int a, int b){ return a * b; } //나눗셈 int divide(int a, int b){ return a / b; } | cs |
main_activity.xml : 화면
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="utf-8"?> <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="com.ghj.jnitwo.MainActivity"> <TextView android:id="@+id/txtResult" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> | cs |
build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | //apply plugin: 'com.android.application' apply plugin: 'com.android.model.application' model { android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.ghj.jnitwo" minSdkVersion.apiLevel 19 targetSdkVersion.apiLevel 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false // proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles.add(file('proguard-android.txt')) } } ndk { moduleName 'jni-calculator' } } } ... | cs |
결과
c라이브러리를 이용하여 사칙연산을 실행
'IT > - 프로그래밍' 카테고리의 다른 글
Android 화면 캡처막기 (0) | 2018.06.27 |
---|---|
Android ProgressBar 이용하여 로딩바 애니메이션 구현하기 (0) | 2018.06.09 |
IOS (Swift) HTTP 통신하기2 (0) | 2017.10.09 |
IOS (Swift) HTTP 통신하기1 - 특정 도메인과 HTTP 통신 허용하기 (0) | 2017.10.09 |
Android NDK HelloWorld (0) | 2017.10.07 |