NDK (二)

 在ndk 里进行多模块配置 (也就是makefile )
1. 在jni 下面建立3个文件夹 分别是 bar,foo,zoo;详细如下
a. bar 下面
    bar.c


#include <android/log.h>
#include "bar.h"
#ifndef BAR
#error BARis not defined here !
#endif
#if BAR != 2
#error BARis incorrectly defined here !
#endif
int bar(int nParam)
{
 LOGI("entry bar");
 nParam += 10;
 return nParam;
}
 
bar.h
 
#ifndef BAR_H
#define BAR_H
#include <android/log.h>
extern int foo(int);
#define LOG_TAG  "libbar"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#endif
 
b .foo下面
 
foo.c
#include "..//bar//bar.h"
#include <android/log.h>
extern int bar(int);
#if BAR== 3
#error  test
#endif
int foo(int nParam)
{
 LOGI("entry foo");
 return bar(nParam);
}
foo.h
#ifndef FOO_H
#define FOO_H
extern int foo(int nParam);
#endif
c . zoo 下面
zoo.h 
#ifndef ZOO_H
#define ZOO_H


extern int zoo(int nParam);


#endif


#include "foo.h"
#include "bar.h"


int MyTest(void)
{
 LOGI("entry zoo");
 return foo(10);
}
 


d. JniZoo.c


#include <stdio.h>
#include <jni.h>
#include "com_example_ndktest2_NDKUtil.h"
#include "zoo.h"
jint Java_com_example_ndktest2_NDKUtil_Jni_1GetInt(JNIEnv *env, jclass obj)
{
return zoo(5);
}




2. 3个.C 文件建立好了,在JNI下面建立 Android.mk


LOCAL_PATH := $(call my-dir)        //
include $(CLEAR_VARS)
LOCAL_MODULE := bar                // bar 模块
LOCAL_SRC_FILES := bar/bar.c // 源文件
LOCAL_CFLAGS := -DBAR=2      //定义bar里面 BAR 的值
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar    // 设置头文件目录
LOCAL_EXPORT_CFLAGS := -DBAR=1    // 设置其它文件调用 BAR 的数值
LOCAL_EXPORT_LDLIBS := -llog              // 设置LOG
(如果设置只本模块用是:LOCAL_LDLIBS := -llog)
include $(BUILD_STATIC_LIBRARY)       // 设置该bar模块将生成静态库,库中的函数JAVA不能直接调用


include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo/foo.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/foo
LOCAL_STATIC_LIBRARIES := bar      //调用静态库 bar
include $(BUILD_SHARED_LIBRARY) //生成动态库


include $(CLEAR_VARS)
LOCAL_MODULE := zoo
LOCAL_SRC_FILES := zoo/zoo.c
LOCAL_SHARED_LIBRARIES := foo  //调用动态库
include $(BUILD_SHARED_LIBRARY) //生成动态库
 include $(CLEAR_VARS)
LOCAL_MODULE := JniZoo
LOCAL_SRC_FILES := JniZoo.c
LOCAL_SHARED_LIBRARIES := zoo
include $(BUILD_SHARED_LIBRARY)
 
3.创建 NDKUtil.java
package com.example.ndktest2;


public class NDKUtil {


public native static String Jni_GetString();
public native static int Jni_GetInt();
static
{
// 这里的顺序是 有关的
System.loadLibrary("foo");   
System.loadLibrary("zoo");
System.loadLibrary("JniZoo");
}
}
4. 创建 Activity
 
package com.example.jnitest01;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
 TextView tv = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
  textView = (TextView)findViewById(R.id.tv);
textView.setText("" + NDKUtil.Jni_GetInt());
    }
}
 
 
这个例子是NDK自带的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值