Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Make an Android so with ndk

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Make a note.

Open Eclips

File- > New- > Project

Select Android App

Fill in the name mytest2ndk

Then click all the way down, do not have to select anything, be careful not to check Mark this project as a library, the generation of so will be dealt with elsewhere.

Then the project is completed. Next, right-click the project, select Android Tools, and then select Add Native Support.

Give the so file a name.

Next, start writing the native method of java. There are two ways.

One is to write directly in MainActivity, which is not recommended here, because there will be holes in the back.

Therefore, it is recommended to generate a new file and choose NEW as a CLASS

Modify NativeClass.java content

Package com.example.my2testndk;public class NativeClass {static {System.loadLibrary ("mytest2ndk");} public native String getTraceIP (String strace_ip);}

Then enter the working directory

Run javah-classpath bin/classes-d jni com.example.my2testndk.NativeClass from the cmd command line of win

The header file can be generated.

Note that if written in the MainActivity file, the error will not be reported here, only to change the bin/classes to src or absolute path, which is very troublesome, it is better to create a new file.

Then create a .cpp file under the jni directory

# include # include "com_example_my2testndk_NativeClass.h" # include # include char buftext [100] = {0}; const char* getRawSocket (const char* domainIP) {int recvsockfd =-1 If ((recvsockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW) / * socket (PF_PACKET, SOCK_DGRAM, htons (ETH_P_ALL)) * /) =-1) / / ipdata recv and send {sprintf (buftext, "error!% s\ n", errno,strerror (errno)); return buftext;} sprintf (buftext, "SOCK_RAW ok!\ n"); return buftext } JNIEXPORT jstring JNICALL Java_com_example_my2testndk_NativeClass_getTraceIP (JNIEnv * env, jobject thiz,jstring domainIP) {const char* strdomainIP = env- > GetStringUTFChars (domainIP, 0); return env- > NewStringUTF (getRawSocket (strdomainIP));}

Then open Cygwin-Terminal and enter the working directory

Run $NDK/ndk-build DK_DEBUG=1-B Vista 1

Sometimes multiple target patterns appears. Stop.

Just delete and compile what is under obj.

After the compilation is successful, so appears under libs\ armeabi.

Then modify the MainActivity.java

Package com.example.my2testndk;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity {NativeClass nativeClass; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); TextView tv = new TextView (this); setContentView (tv); nativeClass = new NativeClass (); tv.setText (nativeClass.getTraceIP ("8.8.8.8"));}}

This is when we run run as/Android application.

Display permission errors because Android does not allow calls to low-level functions such as runsocket.

Look at the code can understand, here is to do a trace function, the function does not have permission, this way is blocked.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report