Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get app package name or applicationId using JNI android

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.

like image 341
Kishan Donga Avatar asked Mar 21 '17 04:03

Kishan Donga


People also ask

How do I find my Android package name?

You can find an app's package name in the URL of your app's Google Play Store listing. For example, the URL of an app page is play.google.com/store/apps/details? id=com. example.

What is applicationId in Android?

Every Android app has a unique application ID that looks like a Java or Kotlin package name, such as com. example. myapp. This ID uniquely identifies your app on the device and in the Google Play Store.

Is application ID and package name same?

apk's manifest, and is the package your app is known as on your device and in the Google Play store, is the "application id". The package that is used in your source code to refer to your R class, and to resolve any relative activity/service registrations, continues to be called the "package".

How do I launch an app using package name?

Just use these following two lines, so you can launch any installed application whose package name is known: Intent launchIntent = getPackageManager(). getLaunchIntentForPackage("com. example.


1 Answers

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

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

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

See a simple example

#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);
    }
}
like image 128
alijandro Avatar answered Oct 26 '22 14:10

alijandro