Android NDK开发_C++多文件

  1 //我自己测试的一个例子
  2 //nativecpp.h
  3 #ifndef NATIVECPP_H_
  4 #define NATIVECPP_H_
  5 
  6 int nativeAdd(int x,int y); //C++本地函数声明
  7 
  8 #endif /* NATIVECPP_H_ */
  9 
 10 //====================================
 11 
 12 //nativecpp.cpp
 13 #include "nativecpp.h"
 14 
 15 int nativeAdd(int x,int y) //C++本地函数实现
 16 {
 17     return (x*x+y*y);
 18 }
 19 
 20 //====================================
 21 
 22 //jnicpp.h
 23 #ifndef JNICPP_H_
 24 #define JNICPP_H_
 25 
 26 #include <string.h>
 27 #include <jni.h>
 28 
 29 extern "C"
 30 {
 31 JNIEXPORT jint JNICALL Java_com_yprnet_myndkapp1_NDKApp1Activity_Toadd(JNIEnv *env,jobject obj,jint value1,jint value2); //标准的JNI调用函数声明
 32 }
 33 
 34 #endif /* JNICPP_H_ */
 35 
 36 //====================================
 37 
 38 //MyNDKApp1.cpp
 39 #include "jnicpp.h"
 40 #include "nativecpp.h"
 41 
 42 jint JNICALL Java_com_yprnet_myndkapp1_NDKApp1Activity_Toadd(JNIEnv *env,jobject obj,jint value1,jint value2) //标准的JNI调用函数实现
 43 {
 44     value1=value1*10;
 45     value2=value2*10;
 46     return nativeAdd(value1,value2); //JNI函数调用C++本地函数
 47 }
 48 
 49 //====================================
 50 
 51 //Android.mk
 52 LOCAL_PATH := $(call my-dir)
 53 
 54 include $(CLEAR_VARS)
 55 
 56 LOCAL_MODULE    := MyNDKApp1
 57 ### Add all source file names to be included in lib separated by a whitespace
 58 LOCAL_SRC_FILES := MyNDKApp1.cpp nativecpp.cpp
 59 
 60 include $(BUILD_SHARED_LIBRARY)
 61 
 62 //====================================
 63 
 64 //NDKApp1Activity.java
 65 package com.yprnet.myndkapp1;
 66 
 67 import android.os.Bundle;
 68 import android.app.Activity;
 69 import android.view.Menu;
 70 import android.view.View;
 71 import android.widget.TextView;
 72 import android.widget.Button;
 73 
 74 public class NDKApp1Activity extends Activity {
 75 
 76     static
 77     {
 78         System.loadLibrary("MyNDKApp1"); //加载库,库名为去掉前缀lib和后缀so
 79     }
 80 
 81     public native int Toadd(int x,int y); //定义原生方法
 82     
 83     @Override
 84     public void onCreate(Bundle savedInstanceState) {
 85         super.onCreate(savedInstanceState);
 86         setContentView(R.layout.activity_ndkapp1);
 87         
 88         Button btn=(Button)findViewById(R.id.button1);
 89         btn.setOnClickListener(new View.OnClickListener(){
 90         public void onClick(View v){
 91             int num=Toadd(3,5); //调用NDK方法相加        
 92             String str=String.valueOf(num);
 93             TextView myTextView=null;
 94             myTextView=(TextView)findViewById(R.id.textView1);
 95             myTextView.setText((CharSequence)str);
 96             myTextView=null;
 97         }
 98         });
 99         
100     }
101 
102     @Override
103     public boolean onCreateOptionsMenu(Menu menu) {
104         getMenuInflater().inflate(R.menu.activity_ndkapp1, menu);
105         return true;
106     }
107 }
108 
109 
110 
111 //======================================
112 //======================================
113 //======================================
114 
115 //========================
116 //Android NDK开发包上的一个例子
117 //android-ndk-r8b\samples\two-libs
118 //========================
119 
120 //first.h
121 #ifndef FIRST_H
122 #define FIRST_H
123 
124 extern int first(int  x, int  y);
125 
126 #endif /* FIRST_H */
127 
128 //========================
129 
130 //first.c
131 #include "first.h"
132 
133 int  first(int  x, int  y)
134 {
135     return x + y;
136 }
137 
138 //========================
139 
140 //second.c
141 #include "first.h"
142 #include <jni.h>
143 
144 jint
145 Java_com_example_twolibs_TwoLibs_add( JNIEnv*  env,
146                                       jobject  this,
147                                       jint     x,
148                                       jint     y )
149 {
150     return first(x, y);
151 }
152 
153 //========================
154 
155 //Android.mk
156 LOCAL_PATH:= $(call my-dir)
157 
158 # first lib, which will be built statically
159 #
160 include $(CLEAR_VARS)
161 
162 LOCAL_MODULE    := libtwolib-first
163 LOCAL_SRC_FILES := first.c
164 
165 include $(BUILD_STATIC_LIBRARY)
166 
167 # second lib, which will depend on and include the first one
168 #
169 include $(CLEAR_VARS)
170 
171 LOCAL_MODULE    := libtwolib-second
172 LOCAL_SRC_FILES := second.c
173 
174 LOCAL_STATIC_LIBRARIES := libtwolib-first
175 
176 include $(BUILD_SHARED_LIBRARY)
177 
178 //========================
179 
180 //TwoLibs.java
181 package com.example.twolibs;
182 
183 import android.app.Activity;
184 import android.widget.TextView;
185 import android.os.Bundle;
186 
187 public class TwoLibs extends Activity
188 {
189     /** Called when the activity is first created. */
190     @Override
191     public void onCreate(Bundle savedInstanceState)
192     {
193         super.onCreate(savedInstanceState);
194 
195         TextView  tv = new TextView(this);
196         int       x  = 1000;
197         int       y  = 42;
198 
199         // here, we dynamically load the library at runtime
200         // before calling the native method.
201         //
202         System.loadLibrary("twolib-second");
203 
204         int  z = add(x, y);
205 
206         tv.setText( "The sum of " + x + " and " + y + " is " + z );
207         setContentView(tv);
208     }
209 
210     public native int add(int  x, int  y);
211 }

 

posted @ 2012-10-27 23:56  天远  阅读(382)  评论(0)    收藏  举报

版权所有 © 2010-2020 YuanPeirong TianYuan All Rights Reserved. Powered By 天远