我正在尝试在Android应用中使用C ++代码,但我不断收到此错误(无需提及这是我第一次使用NDK):
com.example.nimashahbazi.mooshak E/art: No implementation found for int com.example.nimashahbazi.mooshak.EncryptingActivity.encrypt(java.lang.String, java.lang.String, java.lang.String) (tried Java_com_example_nimashahbazi_mooshak_EncryptingActivity_encrypt and Java_com_example_nimashahbazi_mooshak_EncryptingActivity_encrypt__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2)
com.example.nimashahbazi.mooshak D/AndroidRuntime: Shutting down VM
com.example.nimashahbazi.mooshak E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nimashahbazi.mooshak, PID: 17797
java.lang.UnsatisfiedLinkError: No implementation found for int com.example.nimashahbazi.mooshak.EncryptingActivity.encrypt(java.lang.String, java.lang.String, java.lang.String) (tried Java_com_example_nimashahbazi_mooshak_EncryptingActivity_encrypt and Java_com_example_nimashahbazi_mooshak_EncryptingActivity_encrypt__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2)
at com.example.nimashahbazi.mooshak.EncryptingActivity.encrypt(Native Method)
at com.example.nimashahbazi.mooshak.EncryptingActivity.onCreate(EncryptingActivity.java:35)
...
这是我的EncryprtingActivity.java:
package com.example.nimashahbazi.mooshak;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class EncryptingActivity extends AppCompatActivity {
static {
try
{
System.loadLibrary("native-lib");
System.loadLibrary("aes256");
}
catch (UnsatisfiedLinkError use)
{
Log.e("JNI", "WARNING: Could not load native library");
}
}
public native int encrypt(String encryptionKey, String inputFile, String outputFile);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_encrypting);
String input= Environment.getExternalStoragePublicDirectory("Android/data/"+ getApplicationContext().getPackageName()+"/Files/Download").toString()+"DSC_0001.jpg";
String output= Environment.getExternalStoragePublicDirectory("Android/data/"+ getApplicationContext().getPackageName()+"/Files/Download").toString()+"DSC_0001.jpg.encr";
encrypt("12345678",input,output);
}
public void onBackPressed() {
Intent mainIntent = new Intent(EncryptingActivity.this, HomeActivity.class);
EncryptingActivity.this.startActivity(mainIntent);
EncryptingActivity.this.finish();
}
}
这是我的native-lib.cpp:
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include "aes256.cpp"
using namespace std;
#define BUFFER_SIZE 1024*1024
JNIEXPORT jint JNICALL
Java_com_example_nimashahbazi_mooshak_EncryptingActivity_encrypt(JNIEnv *env, jobject obj,
jstring encryptionKey,
jstring inputFile,
jstring outputFile);
JNIEXPORT jint JNICALL
Java_com_example_nimashahbazi_mooshak_DecryptingActivity_decrypt(JNIEnv *env, jobject obj,
jstring encryptionKey,
jstring inputFile,
jstring outputFile);
JNIEXPORT jint JNICALL
Java_com_example_nimashahbazi_mooshak_EncryptingActivity_encrypt(JNIEnv *env, jobject obj,
jstring encryptionKey,
jstring inputFile,
jstring outputFile) {
const char *_encryptionKey = env->GetStringUTFChars(encryptionKey, NULL);
const char *_inputFile = env->GetStringUTFChars(inputFile, NULL);
const char *_outputFile = env->GetStringUTFChars(outputFile, NULL);
ByteArray key, enc;
size_t file_len;
FILE *input, *output;
srand(time(0));
size_t key_len = 0;
while (_encryptionKey[key_len] != 0)
key.push_back(_encryptionKey[key_len++]);
input = fopen(_inputFile, "rb");
if (input == 0) {
fprintf(stderr, "Cannot read file '%s'\n", _inputFile);
return 1;
}
output = fopen(_outputFile, "wb");
if (output == 0) {
fprintf(stderr, "Cannot write file '%s'\n", _outputFile);
return 1;
}
Aes256 aes(key);
fseek(input, 0, SEEK_END);
file_len = ftell(input);
fseek(input, 0, SEEK_SET);
printf("File is %zd bytes\n", file_len);
enc.clear();
aes.encrypt_start(file_len, enc);
fwrite(enc.data(), enc.size(), 1, output);
while (!feof(input)) {
unsigned char buffer[BUFFER_SIZE];
size_t buffer_len;
buffer_len = fread(buffer, 1, BUFFER_SIZE, input);
printf("Read %zd bytes\n", buffer_len);
if (buffer_len > 0) {
enc.clear();
aes.encrypt_continue(buffer, buffer_len, enc);
fwrite(enc.data(), enc.size(), 1, output);
}
}
enc.clear();
aes.encrypt_end(enc);
fwrite(enc.data(), enc.size(), 1, output);
fclose(input);
fclose(output);
return 0;
}
JNIEXPORT jint JNICALL
Java_com_example_nimashahbazi_mooshak_DecryptingActivity_decrypt(JNIEnv *env, jobject obj,
jstring encryptionKey,
jstring inputFile,
jstring outputFile) {
const char *_encryptionKey = env->GetStringUTFChars(encryptionKey, NULL);
const char *_inputFile = env->GetStringUTFChars(inputFile, NULL);
const char *_outputFile = env->GetStringUTFChars(outputFile, NULL);
ByteArray key, dec;
size_t file_len;
FILE *input, *output;
srand(time(0));
size_t key_len = 0;
while (_encryptionKey[key_len] != 0)
key.push_back(_encryptionKey[key_len++]);
input = fopen(_inputFile, "rb");
if (input == 0) {
fprintf(stderr, "Cannot read file '%s'\n", inputFile);
return 1;
}
output = fopen(_outputFile, "wb");
if (output == 0) {
fprintf(stderr, "Cannot write file '%s'\n", outputFile);
return 1;
}
Aes256 aes(key);
fseek(input, 0, SEEK_END);
file_len = ftell(input);
fseek(input, 0, SEEK_SET);
printf("File is %zd bytes\n", file_len);
aes.decrypt_start(file_len);
while (!feof(input)) {
unsigned char buffer[BUFFER_SIZE];
size_t buffer_len;
buffer_len = fread(buffer, 1, BUFFER_SIZE, input);
printf("Read %zd bytes\n", buffer_len);
if (buffer_len > 0) {
dec.clear();
aes.decrypt_continue(buffer, buffer_len, dec);
fwrite(dec.data(), dec.size(), 1, output);
}
}
dec.clear();
aes.decrypt_end(dec);
fwrite(dec.data(), dec.size(), 1, output);
fclose(input);
fclose(output);
return 0;
}
这是我的android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := native-lib
LOCAL_SRC_FILES := native-lib.cpp\
aes256.cpp
LOCAL_C_INCLUDES := aes256.hpp
include $(BUILD_SHARED_LIBRARY)
这是我的CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
add_library(native-lib SHARED src/main/cpp/native-lib.cpp)
add_library(aes256 SHARED src/main/cpp/aes256.cpp)
find_library(log-libd log )
target_link_libraries(native-lib ${log-lib})
target_link_libraries(aes256 ${log-lib})
这是我的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.example.nimashahbazi.mooshak"
minSdkVersion 16
targetSdkVersion 20
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.github.angads25:filepicker:1.1.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'de.hdodenhof:circleimageview:2.1.0'
compile 'com.daimajia.numberprogressbar:library:1.4@aar'
compile 'com.theartofdev.edmodo:android-image-cropper:2.4.+'
compile 'com.github.johnkil.android-appmsg:appmsg:1.2.0'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.google.code.gson:gson:2.7'
compile group: 'net.lingala.zip4j', name: 'zip4j', version: '1.2.4'
testCompile 'junit:junit:4.12'
}
还有一些名为aes256.cpp和aes256.hpp的文件来自here。
我已经检查了关于该主题的每个提议的解决方案,它们主要是关于C ++函数的名称和格式,但我在这里找不到问题。有人可以帮我吗?提前谢谢。
答案 0 :(得分:0)
您似乎正在尝试加载错误的库 - 您的Android.mk声明您的库名称为Mooshak
(通过LOCAL_MODULE),但您使用的是System.loadLibrary("native-lib");
您应该使用:System.loadLibrary("Mooshak");
代替。
答案 1 :(得分:0)
我正在写这篇文章,因为Alex已经在评论中解释了接受的答案。我不得不添加&#34; extern C&#34;到native-lib中函数的定义:
./emulator64-x86 -avd Android_Wear_Square_API_23