简体   繁体   English

Android NDK 会加密编译的 c++ 文件吗?

[英]Does the Android NDK encrypt compiled c++ files?

I have a C++ file on my Android project ( native-lib.cpp ):我的 Android 项目 ( native-lib.cpp ) 中有一个 C++ 文件:

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_keysnative_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string api_key = "0123456789abcdef";
    return env->NewStringUTF(api_key.c_str());
}

When I build the project and unpack the apk, this file will be compiled into a binary: lib/x86/libnative-lib.so .当我构建项目并解压 apk 时,此文件将被编译为二进制文件: lib/x86/libnative-lib.so

If I then try importing and use this file into another project I get an error:如果我然后尝试将此文件导入并使用到另一个项目中,则会出现错误:

public class Main {

    static {
        try {
            System.load("/Users/u/Playground/KeysNative/app-debug/lib/x86/libnative-lib.so");
        } catch (UnsatisfiedLinkError e) {
            System.err.println(e);
            System.exit(1);
        }
    }
}

java.lang.UnsatisfiedLinkError: /Users/u/Playground/KeysNative/app-debug/lib/x86/libnative-lib.so: dlopen(/Users/u/Playground/KeysNative/app-debug/lib/x86/libnative-lib.so, 1): no suitable image found. java.lang.UnsatisfiedLinkError: /Users/u/Playground/KeysNative/app-debug/lib/x86/libnative-lib.so: dlopen(/Users/u/Playground/KeysNative/app-debug/lib/x86/libnative- lib.so, 1): 没有找到合适的图像。 Did find: /Users/u/Playground/KeysNative/app-debug/lib/x86/libnative-lib.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00 /Users/u/Playground/KeysNative/app-debug/lib/x86/libnative-lib.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00找到了:/Users/u/Playground/KeysNative/app-debug/lib/x86/libnative-lib.so:未知文件类型,前八个字节:0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00 /Users/u/Playground/KeysNative /app-debug/lib/x86/libnative-lib.so:未知文件类型,前八个字节:0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00

Question: Do these files use the java keystore in the project to be compiled, or any other kind of signature?问题:这些文件是否使用了要编译的项目中的 java keystore 或任何其他类型的签名?

You cannot load native assemble from your computers file-system on a mobile device.您无法从移动设备上的计算机文件系统加载本机汇编。

This should already suffice to link the assembly:这应该足以链接程序集:

static {
    try {
        System.loadLibrary("libnative-lib");
    } catch (UnsatisfiedLinkError e) {
        Log.e(LOG_TAG, "" + e.getMessage());
    }
}

However, if you only have x86 assembly, this will only work on a x86 Android emulator.但是,如果您只有x86程序集,则这仅适用于x86 Android 模拟器。

正如@Michael 所说,因为 .so 是从 Android NDK 生成的,它包含 Android 依赖项,因此只能在 Android 系统上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM