Android NDK開發_C++多文件

//我本身測試的一個例子
//nativecpp.h
#ifndef NATIVECPP_H_
#define NATIVECPP_H_

int nativeAdd(int x,int y); //C++本地函數聲明

#endif /* NATIVECPP_H_ */

//====================================

//nativecpp.cpp
#include "nativecpp.h"

int nativeAdd(int x,int y) //C++本地函數實現
{
    return (x*x+y*y);
}

//====================================

//jnicpp.h
#ifndef JNICPP_H_
#define JNICPP_H_

#include <string.h>
#include <jni.h>

extern "C"
{
JNIEXPORT jint JNICALL Java_com_yprnet_myndkapp1_NDKApp1Activity_Toadd(JNIEnv *env,jobject obj,jint value1,jint value2); //標準的JNI調用函數聲明
}

#endif /* JNICPP_H_ */

//====================================

//MyNDKApp1.cpp
#include "jnicpp.h"
#include "nativecpp.h"

jint JNICALL Java_com_yprnet_myndkapp1_NDKApp1Activity_Toadd(JNIEnv *env,jobject obj,jint value1,jint value2) //標準的JNI調用函數實現
{
    value1=value1*10;
    value2=value2*10;
    return nativeAdd(value1,value2); //JNI函數調用C++本地函數
}

//====================================

//Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := MyNDKApp1
### Add all source file names to be included in lib separated by a whitespace
LOCAL_SRC_FILES := MyNDKApp1.cpp nativecpp.cpp

include $(BUILD_SHARED_LIBRARY)

//====================================

//NDKApp1Activity.java
package com.yprnet.myndkapp1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

public class NDKApp1Activity extends Activity {

    static
    {
        System.loadLibrary("MyNDKApp1"); //加載庫,庫名爲去掉前綴lib和後綴so
    }

    public native int Toadd(int x,int y); //定義原生方法
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ndkapp1);
        
        Button btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            int num=Toadd(3,5); //調用NDK方法相加        
            String str=String.valueOf(num);
            TextView myTextView=null;
            myTextView=(TextView)findViewById(R.id.textView1);
            myTextView.setText((CharSequence)str);
            myTextView=null;
        }
        });
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_ndkapp1, menu);
        return true;
    }
}



//======================================
//======================================
//======================================

//========================
//Android NDK開發包上的一個例子
//android-ndk-r8b\samples\two-libs
//========================

//first.h
#ifndef FIRST_H
#define FIRST_H

extern int first(int  x, int  y);

#endif /* FIRST_H */

//========================

//first.c
#include "first.h"

int  first(int  x, int  y)
{
    return x + y;
}

//========================

//second.c
#include "first.h"
#include <jni.h>

jint
Java_com_example_twolibs_TwoLibs_add( JNIEnv*  env,
                                      jobject  this,
                                      jint     x,
                                      jint     y )
{
    return first(x, y);
}

//========================

//Android.mk
LOCAL_PATH:= $(call my-dir)

# first lib, which will be built statically
#
include $(CLEAR_VARS)

LOCAL_MODULE    := libtwolib-first
LOCAL_SRC_FILES := first.c

include $(BUILD_STATIC_LIBRARY)

# second lib, which will depend on and include the first one
#
include $(CLEAR_VARS)

LOCAL_MODULE    := libtwolib-second
LOCAL_SRC_FILES := second.c

LOCAL_STATIC_LIBRARIES := libtwolib-first

include $(BUILD_SHARED_LIBRARY)

//========================

//TwoLibs.java
package com.example.twolibs;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;

public class TwoLibs extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        TextView  tv = new TextView(this);
        int       x  = 1000;
        int       y  = 42;

        // here, we dynamically load the library at runtime
        // before calling the native method.
        //
        System.loadLibrary("twolib-second");

        int  z = add(x, y);

        tv.setText( "The sum of " + x + " and " + y + " is " + z );
        setContentView(tv);
    }

    public native int add(int  x, int  y);
}
相關文章
相關標籤/搜索