android简单实现调用c语言代码
ndk等环境配置略过
Activity实现如下
package com.xl.qiu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MyTwoLibsActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button add;
Button minus;
Button ppi;
Button mmi;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button)findViewById(R.id.addition);
minus = (Button)findViewById(R.id.subtraction);
ppi = (Button)findViewById(R.id.ppi);
mmi = (Button)findViewById(R.id.mmi);
text = (TextView)findViewById(R.id.text);
text.setText("0");
add.setOnClickListener(this);
minus.setOnClickListener(this);
ppi.setOnClickListener(this);
mmi.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.addition:
text.setText(""+add(23, 11));
break;
case R.id.subtraction:
text.setText(""+minus(23, 11));
break;
case R.id.ppi:
text.setText(""+ppi(Integer.parseInt(text.getText().toString())));
break;
case R.id.mmi:
text.setText(""+mm_i(Integer.parseInt(text.getText().toString())));
break;
}
}
public native int add(int x, int y);
public native int minus(int x, int y);
public native int ppi(int x);
public native int mm_i(int x);
static {
System.loadLibrary("mytwolibsecond");
}
}
4个native函数add`minus`ppi`mm_i分别实现整数相加`相减`自加++i`自减--i;
项目build过后,可以见到成功生成了com.xl.qiu.MyTwoLibsActivity.class
Next:项目目录下建立jni文件夹...此时目录形如 src/ gen/ jni/ res/ ......
shell执行javah -classpath bin -d jni com.xl.qiu.MyTwoLibsActivity
成功执行后在jni目录下生成了com_xl_qiu_MyTwoLibsActivity.h
内容如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_xl_qiu_MyTwoLibsActivity */
#ifndef _Included_com_xl_qiu_MyTwoLibsActivity
#define _Included_com_xl_qiu_MyTwoLibsActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_xl_qiu_MyTwoLibsActivity
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_xl_qiu_MyTwoLibsActivity_add
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_xl_qiu_MyTwoLibsActivity
* Method: minus
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_xl_qiu_MyTwoLibsActivity_minus
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_xl_qiu_MyTwoLibsActivity
* Method: ppi
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_xl_qiu_MyTwoLibsActivity_ppi
(JNIEnv *, jobject, jint);
/*
* Class: com_xl_qiu_MyTwoLibsActivity
* Method: mm_i
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_xl_qiu_MyTwoLibsActivity_mm_1i
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
把已存在的c代码及其头文件代码也放入jni目录中
purec.h代码如下:
#ifndef PUREC_H
#define PRUEC_H
extern int add(int x, int y);
extern int minus(int x, int y);
extern int ppi(int x);
extern int mmi(int x);
#endif
purec.c代码如下:
#include "purec.h"
int add(int x, int y)
{
return x + y;
}
int minus(int x, int y)
{
return x - y;
}
int ppi(int x)
{
return ++x;
}
int mmi(int x)
{
return --x;
}
编写com_xl_qiu_MyTwoLibsActivity.c代码如下:
#include <jni.h>
#include "com_xl_qiu_MyTwoLibsActivity.h"
#include "purec.h"
jint Java_com_xl_qiu_MyTwoLibsActivity_add(
JNIEnv *env,
jobject this,
jint x,
jint y
)
{
return add(x, y);
}
jint Java_com_xl_qiu_MyTwoLibsActivity_minus(
JNIEnv *env,
jobject this,
jint x,
jint y
)
{
return minus(x, y);
}
jint Java_com_xl_qiu_MyTwoLibsActivity_ppi(
JNIEnv *env,
jobject this,
jint x
)
{
return ppi(x);
}
jint Java_com_xl_qiu_MyTwoLibsActivity_mm_1i(
JNIEnv *env,
jobject this,
jint x
)
{
return mmi(x);
}
jni源文件完毕...下面编写Android.mk同样放在jni目录下
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmytwolibfirst
LOCAL_SRC_FILES := purec.c
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libmytwolibsecond
LOCAL_SRC_FILES := com_xl_qiu_MyTwoLibsActivity.c
LOCAL_STATIC_LIBRARIES :=libmytwolibfirst
include $(BUILD_SHARED_LIBRARY)
mk文件写完毕...
执行$NDK/ndk-build($NDK已经设置的情况下)
可以看到.so类库已经生成,可以Run Android Application了
注意:本地方法自加(ppi)自减(mm_i)命名不一样(mm_i多了一个_)
在生成.h文件的时候请注意区别
Activity实现如下
package com.xl.qiu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MyTwoLibsActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button add;
Button minus;
Button ppi;
Button mmi;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button)findViewById(R.id.addition);
minus = (Button)findViewById(R.id.subtraction);
ppi = (Button)findViewById(R.id.ppi);
mmi = (Button)findViewById(R.id.mmi);
text = (TextView)findViewById(R.id.text);
text.setText("0");
add.setOnClickListener(this);
minus.setOnClickListener(this);
ppi.setOnClickListener(this);
mmi.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.addition:
text.setText(""+add(23, 11));
break;
case R.id.subtraction:
text.setText(""+minus(23, 11));
break;
case R.id.ppi:
text.setText(""+ppi(Integer.parseInt(text.getText().toString())));
break;
case R.id.mmi:
text.setText(""+mm_i(Integer.parseInt(text.getText().toString())));
break;
}
}
public native int add(int x, int y);
public native int minus(int x, int y);
public native int ppi(int x);
public native int mm_i(int x);
static {
System.loadLibrary("mytwolibsecond");
}
}
4个native函数add`minus`ppi`mm_i分别实现整数相加`相减`自加++i`自减--i;
项目build过后,可以见到成功生成了com.xl.qiu.MyTwoLibsActivity.class
Next:项目目录下建立jni文件夹...此时目录形如 src/ gen/ jni/ res/ ......
shell执行javah -classpath bin -d jni com.xl.qiu.MyTwoLibsActivity
成功执行后在jni目录下生成了com_xl_qiu_MyTwoLibsActivity.h
内容如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_xl_qiu_MyTwoLibsActivity */
#ifndef _Included_com_xl_qiu_MyTwoLibsActivity
#define _Included_com_xl_qiu_MyTwoLibsActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_xl_qiu_MyTwoLibsActivity
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_xl_qiu_MyTwoLibsActivity_add
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_xl_qiu_MyTwoLibsActivity
* Method: minus
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_xl_qiu_MyTwoLibsActivity_minus
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_xl_qiu_MyTwoLibsActivity
* Method: ppi
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_xl_qiu_MyTwoLibsActivity_ppi
(JNIEnv *, jobject, jint);
/*
* Class: com_xl_qiu_MyTwoLibsActivity
* Method: mm_i
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_xl_qiu_MyTwoLibsActivity_mm_1i
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
把已存在的c代码及其头文件代码也放入jni目录中
purec.h代码如下:
#ifndef PUREC_H
#define PRUEC_H
extern int add(int x, int y);
extern int minus(int x, int y);
extern int ppi(int x);
extern int mmi(int x);
#endif
purec.c代码如下:
#include "purec.h"
int add(int x, int y)
{
return x + y;
}
int minus(int x, int y)
{
return x - y;
}
int ppi(int x)
{
return ++x;
}
int mmi(int x)
{
return --x;
}
编写com_xl_qiu_MyTwoLibsActivity.c代码如下:
#include <jni.h>
#include "com_xl_qiu_MyTwoLibsActivity.h"
#include "purec.h"
jint Java_com_xl_qiu_MyTwoLibsActivity_add(
JNIEnv *env,
jobject this,
jint x,
jint y
)
{
return add(x, y);
}
jint Java_com_xl_qiu_MyTwoLibsActivity_minus(
JNIEnv *env,
jobject this,
jint x,
jint y
)
{
return minus(x, y);
}
jint Java_com_xl_qiu_MyTwoLibsActivity_ppi(
JNIEnv *env,
jobject this,
jint x
)
{
return ppi(x);
}
jint Java_com_xl_qiu_MyTwoLibsActivity_mm_1i(
JNIEnv *env,
jobject this,
jint x
)
{
return mmi(x);
}
jni源文件完毕...下面编写Android.mk同样放在jni目录下
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmytwolibfirst
LOCAL_SRC_FILES := purec.c
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libmytwolibsecond
LOCAL_SRC_FILES := com_xl_qiu_MyTwoLibsActivity.c
LOCAL_STATIC_LIBRARIES :=libmytwolibfirst
include $(BUILD_SHARED_LIBRARY)
mk文件写完毕...
执行$NDK/ndk-build($NDK已经设置的情况下)
可以看到.so类库已经生成,可以Run Android Application了
注意:本地方法自加(ppi)自减(mm_i)命名不一样(mm_i多了一个_)
在生成.h文件的时候请注意区别