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

查看:535
本文介绍了如何使用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\n", 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\n", application_id);
        fclose(cmdline);
    }
}

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

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