use libcryto.so and libssl.so in an android project?

后端 未结 2 811
礼貌的吻别
礼貌的吻别 2020-12-06 20:14

I\'m beginer to Android NDK. I want to build a RSA example base on openssl libary. First, I built libssl.so and libcrypto.so librairies with ndk-build in the guardianproje

2条回答
  •  执笔经年
    2020-12-06 20:45

    You should build static libraries for libssl and libcrypto in your script. If you can't, rename these libraries (you can do this after build, while copying to your precompiled directory). The reason is that the system comes with its own (probably different) version of these shared libraries, and the loader will use /system/lib/libssl.so and /system/lib/libcrypto.so instead of your private copies.

    Regarding the Android.mk file, I slightly cleaned it up for you (note that I did not change the names of prebuilt LOCAL_MODULEs, but changed the name of LOCAL_MODULE you finally build, because security is, well, too generic and could also happen to match a system library on some device):

    LOCAL_PATH := $(call my-dir)
    
    # Prebuilt libssl
    include $(CLEAR_VARS)
    LOCAL_MODULE := ssl
    LOCAL_SRC_FILES := precompiled/libPrivateSsl.so
    include $(PREBUILT_SHARED_LIBRARY)
    
    # Prebuilt libcrypto
    include $(CLEAR_VARS)
    LOCAL_MODULE := crypto
    LOCAL_SRC_FILES := precompiled/libPrivateCrypto.so
    include $(PREBUILT_SHARED_LIBRARY)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE := PrivateSecurity
    LOCAL_C_INCLUDES := includes
    LOCAL_SRC_FILES := TestJNI2.cpp
    LOCAL_LDLIBS := -llog
    LOCAL_SHARED_LIBRARIES := ssl crypto
    
    include $(BUILD_SHARED_LIBRARY)
    

    Don't forget that your Java should load the dependencies first:

    {
        System.loadLibrary("PrivateSsl");
        System.loadLibrary("PrivateCrypto");
        System.loadLibrary("PrivateSecurity");
    }
    

提交回复
热议问题