如何使用 JNI android 获取应用程序包名称或 applicationId [英] How to get app package name or applicationId using JNI android

查看:21
本文介绍了如何使用 JNI android 获取应用程序包名称或 applicationId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于共享库的保护问题,我会尝试使用 JNI 获取包名,但它会报错.那么,是否可以使用 JNI 获取包名或 applicationId?如果有人对此问题有示例或参考,则建议.因为没有任何好的教程或解决方案可用.否则任何其他方式都建议保护共享库.

For the protection issue of the shared library I will try to get package name using JNI but it will give errors. So, is it possible to get package name or applicationId using JNI? If anyone have example or references for this problem then suggests. Because not any good tutorial or solution available. Else any other way suggest of the protection of the shared library.

推荐答案

是的,有可能.Android是基于Linux的,我们可以在内核提供的用户空间中获取大量信息.

Yes, it's possible. Android is based on Linux, we can obtain a lot of information in user space provided by kernel.

在您的示例中,此处存储的信息 /proc/${process_id}/cmdline

In your example, the information stored here /proc/${process_id}/cmdline

我们可以读取这个文件,得到应用的id.

We can read this file, and get the application id.

看一个简单的例子

#include <jni.h>
#include <unistd.h>
#include <android/log.h>
#include <stdio.h>

#define TAG "YOURAPPTAG"

extern "C"
JNIEXPORT void JNICALL
Java_com_x_y_MyNative_showApplicationId(JNIEnv *env, jclass type) {

    pid_t pid = getpid();
    __android_log_print(ANDROID_LOG_DEBUG, TAG, "process id %d
", pid);
    char path[64] = { 0 };
    sprintf(path, "/proc/%d/cmdline", pid);
    FILE *cmdline = fopen(path, "r");
    if (cmdline) {
        char application_id[64] = { 0 };
        fread(application_id, sizeof(application_id), 1, cmdline);
        __android_log_print(ANDROID_LOG_DEBUG, TAG, "application id %s
", application_id);
        fclose(cmdline);
    }
}

这篇关于如何使用 JNI android 获取应用程序包名称或 applicationId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆