How to install apk under android system using c / c ++ code?

now i am writing an application that runs android (linux) i am facing problem: i use libcurl, download apk in my code and use execvp to install it? the error code is -1 (perror print: Permission denied) as shown below:

the error is: Permission denied

      

file upload permission:

----rwxr-x system   sdcard_rw  3240080 2014-12-08 07:48 barcode.apk

      

example code:

curl_easy_reset(curl);
    FILE *fp;
    fp = fopen("/sdcard/download/barcode.apk", "wb+");
    curl_easy_setopt(curl, CURLOPT_URL, "http://apk.r1.market.hiapk.com/data/upload/apkres/2014/9_22/21/com.youba.barcode_093937.apk");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_apk_file);

    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

    returnvalue = curl_easy_perform(curl);

    if(CURLE_OK != returnvalue)
    {
        /* we failed */
        fprintf(stderr, "curl told us %d\n", returnvalue);
    }
    else
    {
        int value =  execvp("/sdcard/download/barcode.apk", NULL);
        if (value == -1)
        {
            perror("the error is");
        }
    }

      

How can I install apk in this application, I found similar questions that are all in java? (what would I not know)?

+3


source to share


1 answer


An Android package is not executable binary, it cannot be executed by a system call exec

even if you add execute permission to it.

You can create a callback in Java and then call it from JNI to install the downloaded package. I am using an example file /data/local/tmp/test.apk

, you can replace it with the downloaded file in your implementation.

JNI method

void __attribute__((visibility("default"))) Java_com_example_test_MainActivity_installFromJNI(
    JNIEnv* env, jobject obj) {

__android_log_print(ANDROID_LOG_DEBUG, "test", "jni call");
jclass clzMainActivity = env->GetObjectClass(obj);
jclass clzFile = env->FindClass("java/io/File");

if (env->ExceptionCheck()) {
    __android_log_print(ANDROID_LOG_DEBUG, "test", "find class error");
    env->ExceptionDescribe();
}

if (env->ExceptionCheck()) {
    __android_log_print(ANDROID_LOG_DEBUG, "test", "get method error");
    env->ExceptionDescribe();
}

jobject filePath = env->NewStringUTF("/data/local/tmp/test.apk");
jobject file = env->NewObject(clzFile,
        env->GetMethodID(clzFile, "<init>",
                "(Ljava/lang/String;)V"), filePath);
if (env->ExceptionCheck()) {
    env->ExceptionDescribe();
    __android_log_print(ANDROID_LOG_DEBUG, "test",
            "create file object error");
}

jmethodID installCallback = env->GetMethodID(clzMainActivity,
        "installCallback", "(Ljava/io/File;)V");

env->CallVoidMethod(obj, installCallback, file);

__android_log_print(ANDROID_LOG_DEBUG, "test", "jni call end");
}

      



Java method

public void installCallback(File file) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    startActivity(intent);
}

private native void installFromJNI();

      

Function call chain: installFromJNI

(Java) -> Java_com_example_test_MainActivity_installFromJNI

(Native) -> InstallCallback

(Java).

+1


source







All Articles