在Android Studio中创建新的C++文件,命名为"Crypto.cpp"。
在"Crypto.cpp"文件中,添加以下代码:
#include
#include
#include
#include
#include
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, "Crypto", __VA_ARGS__)
extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_myapp_MainActivity_getSecretKey(JNIEnv* env, jobject instance);
};
JNIEXPORT jstring JNICALL Java_com_example_myapp_MainActivity_getSecretKey(JNIEnv* env, jobject instance) {
std::string key = "my_secret_key";
std::string encryptedKey = "";
// Perform your encryption algorithm here
return env->NewStringUTF(encryptedKey.c_str());
}
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -O0 -Wall -Werror"
arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=c++_shared"
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
...
implementation 'com.android.support:support-annotations:28.0.0'
}
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC or SHARED,
# and provides the relative paths to its source code.
include_directories(src/main/cpp)
add_library(
native-lib
SHARED
src/main/cpp/Crypto.cpp)
find_library(
log-lib
log)
# Links your native library against one or more other native libraries.
target_link_libraries(native