So file call

With the rapid development of Android mobile security, it has become a basic operation for key codes to sink into native layer, whether for execution efficiency or program security. The development of Native layer is the general JNI/NDK development. Through JNI, Java layer and Native layer (mainly C/C++) can be called each other. After the compilation of native layer, so dynamic link library is generated. So the problem comes, how to call so file is extremely important, of course, you can also directly analyze the pseudo-code of so file, using strong programming skills directly simulate key operations, but I think for ordinary people hair is still more important. The current mainstream action for calling the so file should be: 1. Various implementations based on Unicorn (still under study, not listed in the table for now). 2. Just because I have searched why it combines Service, I have learned about the life cycle of Service when LEARNING Android development, and I personally understand that it is better to use Service to create Http Service. Of course, there is also a simple use of Application, because in the formal environment, most so files will have some package name of the context, signed effect, etc., custom Application to get the context pass.

Libyemu. So abstract

This is an SO file compiled by me, which is to do simple string concatenation according to the input parameter (the following is the C code before compiling native layer).

extern "C"
JNIEXPORT jstring JNICALL
Java_com_fw_myapplication_ndktest_NdkTest_stringFromUTF(JNIEnv *env, jobject instance, jstring str_) {
    jclass String_clazz = env->FindClass("java/lang/String");

    jmethodID concat_methodID = env->GetMethodID(String_clazz, "concat"."(Ljava/lang/String;) Ljava/lang/String;");

    jstring str = env->NewStringUTF("From so --[NightTeam night]");

    jobject str1 = env->CallObjectMethod(str_, concat_methodID, str);

    const char *chars = env->GetStringUTFChars((jstring)str1, 0);

    return env->NewStringUTF(chars);
}
Copy the code

This part of the code is still worth Posting, simple static registration using the idea of reflection, reflection is very important in reverse and then Java code defining native functions

package com.fw.myapplication.ndktest;

public class NdkTest {
    public static native String stringFromUTF(String str);

    static {
        System.loadLibrary("yemu"); }}Copy the code

If you’re a little confused here, you might want to catch up on Android development

The Android project tests so

AndroidStudio 3.4 2, Android 6 architecture armeabi-v7a open AndroidStudio new project










AndServer code

AndServer official documentation: yanzhenjie.com/AndServer/ open the official document, look at the introduction, the new Java file

package com.nightteam.httpso;

import android.app.Application;

public class MyApp extends Application {
    private static MyApp myApp;
    public static MyApp getInstance() {
        return myApp;
    }

    @Override
    public void onCreate() { super.onCreate(); myApp = this; }}Copy the code

Then specify the Application to launch in the manifest file






Service coding

So add a button to activity_main. XML and specify the click event

package com.nightteam.httpso.Service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.nightteam.httpso.ServerManager;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class MyService extends Service {
    private static final String TAG = "NigthTeam";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: MyService");
        new Thread() {
            @Override
            public void run() {
                super.run();
                InetAddress inetAddress = null;
                try {
                    inetAddress = InetAddress.getByName("0.0.0.0");
                    Log.d(TAG, "onCreate: " + inetAddress.getHostAddress());
                    ServerManager serverManager = new ServerManager(getApplicationContext(), inetAddress, 8005);
                    serverManager.startServer();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        returnnull; }}Copy the code

Start the Service of the AndServer in a child thread by typing a few logs. The difference between localhost and 0.0.0.0 is in the search engine, and then you pass the context, inetAddress, port to the constructor of the ServerManager for a new object, then you open the Service and finally check the manifest file for the Service declaration

Enable Service and obtain the local IP address

Back to our MainActivity.java operate (button click event) code to start the Service

    public void operate(View view) {
        switch (view.getId()){
            caseR.id.id_bt_index: Intents = new intents (this, myservice.class); intents = new intents (this, myservice.class); Log.d(TAG,"operate: button");
                startService(it1);
                ((Button) view).setText("Service started");
                break; }}Copy the code

By now our service is basically set up, but for the sake of convenience, I want to display our local IP on the APP, so that we do not have to set up to check again. I found a tool class to obtain IP address on the Internet, the source code is as follows:

package com.nightteam.httpso;


import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Pattern;

public class NetUtils {

    private static final Pattern IPV4_PATTERN = Pattern.compile("^" +

            "([0-9] | [1-9] [0-9] [0-9] {2} | 1 | 2 [0 to 4] [0-9] 25 [0-5]) | \ \.) {3}" +

            "([0-9] | [1-9] [0-9] [0-9] {2} | 1 | 2 [0 to 4] [0-9] 25 [0-5]) | $");


    private static boolean isIPv4Address(String input) {

        returnIPV4_PATTERN.matcher(input).matches(); } public static InetAddress public static InetAddressgetLocalIPAddress() {

        Enumeration<NetworkInterface> enumeration = null;

        try {

            enumeration = NetworkInterface.getNetworkInterfaces();

        } catch (SocketException e) {

            e.printStackTrace();

        }

        if(enumeration ! = null) {while (enumeration.hasMoreElements()) {

                NetworkInterface nif = enumeration.nextElement();

                Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();

                if(inetAddresses ! = null)while (inetAddresses.hasMoreElements()) {

                        InetAddress inetAddress = inetAddresses.nextElement();

                        if(! inetAddress.isLoopbackAddress() && isIPv4Address(inetAddress.getHostAddress())) {returninetAddress; }}}}returnnull; }}Copy the code

Copy the utility classes into our Android project and continue coding in mainActivity.java

Apply for permission and start the App

The last step is to apply for network permission for the app





Article by: “NightTeam NightTeam” – Nonsense

Founded in 2019, nightfall team members include Cui Qingcai, Zhou Ziqi, Chen Xiangan, Tang Yifei, Feng Wei, CAI Jin, Dai Huangjin, Zhang Yeqing and Wei Shidong.

My major programming languages are Python, Rust, C++ and Go, and my fields include crawler, deep learning, service development and object storage. The team is neither good nor evil. We only do what we think is right. Please be careful.

This article was automatically published by ArtiPub