android开发 如何调用SO

java

package com.example.callsodemo;

import android.R.integer;
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {
    public native int Add(int x, int y);
    
    public native void WriteLog(String str);

    static {
        // 加载动态库
        System.loadLibrary("zhentry");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int i = Add(5, 8);
        WriteLog("14521452");
        int j = i;
    }

c

#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <jni.h>

JNIEXPORT jint JNICALL Java_com_example_callsodemo_MainActivity_Add(JNIEnv *env, jobject obj,
        jint x, jint y) {

    return x+y;
}
JNIEXPORT void JNICALL Java_com_example_callsodemo_MainActivity_WriteLog(JNIEnv *env, jobject obj, jstring prompt) {

    //int d = system("touch /sdcard/zhang.txt");
    //int d = system("/data/local/tmp/screencap -p /sdcard/zzz.png");
    char *args[] = {"sh","-c","/data/local/tmp/screencap -p /sdcard/zzz.png",NULL};
    execv("/system/bin/sh",args);
    int d=223;
    //int e = seteuid(2000);
    //int d = getuid();
    char *str = (*env)->GetStringUTFChars(env, prompt, 0);
    FILE *stream;
    stream = fopen("/sdcard/zhentry.log", "a+,ccs=utf-8");
    fprintf(stream, "%s|%d
", str,d);
    fclose(stream);
}

注意:

1 C中函数的命名

2 C中使用JAVA参数时需要转型,如char *str = (*env)->GetStringUTFChars(env, prompt, 0);

原文地址:https://www.cnblogs.com/bloodofhero/p/4793621.html