Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Make an Android so with ndk

Shulou Source: shulou.com Published: 2022-06-03 05:20:05 11月24日 Update

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.

Tags: Files selections directories build run Android next features projects methods permissions work compilation success no things code content functions only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information Linux Shulou Tech Info MariaDB