Commit 6f830481 by Fear1ess

4/2

parent 14ce6561
...@@ -14,6 +14,10 @@ android { ...@@ -14,6 +14,10 @@ android {
versionName "1.0" versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
} }
buildTypes { buildTypes {
...@@ -27,6 +31,12 @@ android { ...@@ -27,6 +31,12 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8
} }
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
}
}
} }
dependencies { dependencies {
......
cmake_minimum_required(VERSION 3.10.2)
project(Demo)
add_library(
demo
SHARED
main.c
)
target_link_libraries(demo)
\ No newline at end of file
//
// Created by Fear1ess on 2021/4/2.
//
int xx(int a, int b) {
return a + b;
}
\ No newline at end of file
...@@ -7,16 +7,12 @@ import android.widget.TextView; ...@@ -7,16 +7,12 @@ import android.widget.TextView;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
// Example of a call to a native method // Example of a call to a native method
TextView tv = findViewById(R.id.sample_text); TextView tv = findViewById(R.id.sample_text);
tv.setText("hello world!"); tv.setText("hello world!");
......
...@@ -13,17 +13,23 @@ elseif (${CMAKE_ANDROID_ARCH_ABI} STREQUAL "armeabi-v7a") ...@@ -13,17 +13,23 @@ elseif (${CMAKE_ANDROID_ARCH_ABI} STREQUAL "armeabi-v7a")
wd_syscall32.s) wd_syscall32.s)
endif() endif()
set(CMAKE_CXX_FLAGS_RELEASE "-fvisibility=hidden -O3 -fno-unwind-tables -flto")
add_library( add_library(
wdun wdun
SHARED SHARED
core.c core.c
collect.c
utils.c
wd_syscall.c wd_syscall.c
jni_helper.c jni_helper.c
${ARCH_DEPENDED_SRC} ${ARCH_DEPENDED_SRC}
) )
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} \
-fvisibility=hidden \
-fno-unwind-tables \
-Wl,--exclude-libs,ALL \
-Wl,--gc-sections")
add_library(cJson SHARED IMPORTED) add_library(cJson SHARED IMPORTED)
set_target_properties( set_target_properties(
cJson cJson
......
//
// Created by Fear1ess on 2021/4/2.
//
#include "collect.h"
#include "jni_helper.h"
#include "utils.h"
extern jobject g_app_context;
cJSON *collect_init() {
return cJSON_CreateObject();
}
void do_collect(JNIEnv* env) {
cJSON* json = collect_init();
collect_app_info(env, json);
}
void collect_app_info(JNIEnv* env, cJSON *json) {
//package_name
jobject pkgName = wdCallObjectMethod(env, g_app_context, "getPackageName", "()Ljava/lang/String;");
addJniStringToJson(env, json, "package_name", pkgName);
//get packagemanager and pkginfo
jobject pkgManager = wdCallObjectMethod(env, g_app_context, "getPackageManager", "()Landroid/content/pm/PackageManager;");
jobject pkgInfo = wdCallObjectMethod(env, pkgManager, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;", pkgName, 0);
//last update time
jlong lastUpdateTime = wdGetLongField(env, pkgInfo, "lastUpdateTime", "J");
cJSON_AddNumberToObject(json, "last_update_time", lastUpdateTime);
const char* info = cJSON_Print(json);
(*env)->DeleteLocalRef(env, pkgName);
return;
}
...@@ -6,25 +6,39 @@ ...@@ -6,25 +6,39 @@
#include <syscall.h> #include <syscall.h>
#include "wdun.h" #include "wdun.h"
#include "cJSON.h" #include "cJSON.h"
#include "jni_helper.h"
#include "collect.h"
//import asm symbol //import asm symbol
IMPORTWDSYSCALL IMPORTWDSYSCALL
#define WDMAIN_CLASS_NAME "com/reyun/wandun/WdMain" #define WDMAIN_CLASS_NAME "com/reyun/wandun/WdMain"
#define WDMAIN_GETWDID_METHOD_NAME "getWdIdNative" #define WDMAIN_GETWDID_METHOD_NAME "getWdIdNative"
#define WDMAIN_GETWDID_METHOD_SIG "()Ljava/lang/String;" #define WDMAIN_GETWDID_METHOD_SIG "()Ljava/lang/String;"
#define WDMAIN_INIT_METHOD_NAME "initNative"
#define WDMAIN_INIT_METHOD_SIG "(Landroid/content/Context;)V"
jstring jni_get_wdid(JNIEnv* env, jobject thiz) { jobject g_app_context = NULL;
JNIEXPORT jstring jni_get_wdid(JNIEnv* env, jobject thiz) {
do_collect(env);
}
JNIEXPORT void jni_init(JNIEnv* env, jobject thiz, jobject context) {
if(!g_app_context) {
g_app_context = (*env)->NewGlobalRef(env, context);
}
} }
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
int pid = WDSYSCALL(SYS_getpid); int pid = WDSYSCALL(SYS_getpid);
JNIEnv* env = NULL; JNIEnv* env = NULL;
if((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) == JNI_TRUE) { if((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) == JNI_OK) {
jclass cls = (*env)->FindClass(env, WDMAIN_CLASS_NAME); jclass cls = (*env)->FindClass(env, WDMAIN_CLASS_NAME);
JNINativeMethod methods[] = {{WDMAIN_GETWDID_METHOD_NAME, WDMAIN_GETWDID_METHOD_SIG, (void*)jni_get_wdid}}; JNINativeMethod methods[] = {{WDMAIN_GETWDID_METHOD_NAME, WDMAIN_GETWDID_METHOD_SIG, (void*)jni_get_wdid},
{WDMAIN_INIT_METHOD_NAME, WDMAIN_INIT_METHOD_SIG, (void*)jni_init}};
(*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(JNINativeMethod)); (*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(JNINativeMethod));
(*env)->DeleteLocalRef(env, cls);
} }
cJSON* json = cJSON_CreateObject(); cJSON* json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "name", "zhanglei"); cJSON_AddStringToObject(json, "name", "zhanglei");
......
//
// Created by Fear1ess on 2021/4/2.
//
#ifndef REYUNSDK_COLLECT_H
#define REYUNSDK_COLLECT_H
#include <jni.h>
#include "cJSON.h"
void do_collect(JNIEnv* env);
cJSON* collect_init();
void collect_app_info(JNIEnv* env, cJSON* json);
#endif //REYUNSDK_COLLECT_H
//
// Created by Fear1ess on 2021/4/2.
//
#ifndef REYUNSDK_UTILS_H
#define REYUNSDK_UTILS_H
#include "cJSON.h"
void addJniStringToJson(JNIEnv* env, cJSON* json, const char* key, jobject jstr);
#endif //REYUNSDK_UTILS_H
//
// Created by Fear1ess on 2021/4/2.
//
#include <jni.h>
#include "cJSON.h"
#include "utils.h"
void addJniStringToJson(JNIEnv *env, cJSON *json, const char *key, jobject jstr) {
jboolean value = 0;
const char* package_name = (*env)->GetStringUTFChars(env, jstr, &value);
cJSON_AddStringToObject(json, key, package_name);
(*env)->ReleaseStringUTFChars(env, jstr, package_name);
}
...@@ -26,6 +26,7 @@ public class WdMain { ...@@ -26,6 +26,7 @@ public class WdMain {
//load native lib //load native lib
System.loadLibrary("wdun"); System.loadLibrary("wdun");
initNative(context);
//todo 监控传感器数据变化 //todo 监控传感器数据变化
//todo 监控手机信号强度变化 //todo 监控手机信号强度变化
...@@ -44,6 +45,10 @@ public class WdMain { ...@@ -44,6 +45,10 @@ public class WdMain {
cb.onWdId(wdId); cb.onWdId(wdId);
} }
public native void initNative(Context context);
//native层获取wdId //native层获取wdId
public native String getWdIdNative(); public native String getWdIdNative();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment