Swig-wrapped Rust code called by Android Java expects uncoded symbol "log" - java

I'm trying to write an Android app which calls some Rust code via swig. I have written a simple test for the linkages. A version that has an ordinary Java main program works fine. My Android version fails with an error:
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol
"log" referenced by
"/data/app/hk.jennyemily.work.try3-1/lib/x86_64/libsimpleswig.so"...
at java.lang.Runtime.loadLibrary0(Runtime.java:989)
My Rust code does not import "log".
So what do I do to fix this?
The Android Java basically only does the following so far (I have not yet coded the calls to the library):
static {
System.loadLibrary("simpleswig");
}
Here is my Rust code:
use std::ffi::CString;
use std::os::raw::c_char;
pub struct SimpleData {
pub str: CString,
}
#[no_mangle]
pub extern "C" fn newSimpleData() -> *mut SimpleData {
Box::into_raw(Box::new(SimpleData {
str: CString::new("hello").unwrap(),
}))
}
#[no_mangle]
pub extern "C" fn say(ptr: *mut SimpleData) -> *const c_char {
let sd = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
sd.str.as_ptr()
}
#[no_mangle]
pub extern "C" fn release(ptr: *mut SimpleData) {
if ptr.is_null() {
return;
}
unsafe {
let _ = Box::from_raw(ptr);
}
}
There is also the basic Swig code for wrapping the Rust code.
The Swig and Rust code is linked into a dynamic library (.so).
Edit: the actual C compile and link line is:
$CC -shared $SIMPDIR/simple_wrap.c $SIMPDIR/libsimple.a -o $SIMPDIR/libsimpleswig.so -I"${JAVALOC}/include" -I"${JAVALOC}/include/linux"
Here is the stand-alone Java main program, which works without any problems with the same Rust and Swig code.
public class test {
static {
System.loadLibrary("simpleswig");
}
public static void main(String argv[]) {
System.out.println("starting...");
simpleswig.SWIGTYPE_p_SimpleData sd = simpleswig.simpleswig.newSimpleData();
System.out.println( simpleswig.simpleswig.say(sd));
System.out.println("releasing...");
simpleswig.simpleswig.release(sd);
System.out.println("done.");
}
}

Related

Build webassembly sorting functions from C++ and Java source code

I have a problem that I was going to work on arrays in WebAssembly and I wanted to use Java and C ++, and trying to do this, I ran into the following problems and I would like to ask for help:
Java: I'm using JWebAssembly
And we have a class that works on tables
import de.inetsoftware.jwebassembly.api.annotation.Export;
public class Add {
#Export
public static int[] add( int a[], int b ) {
for(int i = 0;i<b-1;i++){
a[i] += b ;
}
return a;
}
}
we convert it to wasm
import de.inetsoftware.jwebassembly.JWebAssembly;
import java.io.File;
import java.net.URL;
public class Wasm {
public static void main(String[] args) {
File wasmFile = new File("testTable.wasm");
JWebAssembly wasm = new JWebAssembly();
Class clazz = Add.class;
URL url = clazz.getResource(
'/' +
clazz.getName().replace('.', '/') +
".class");
wasm.addFile(url);
String txt = wasm.compileToText();
System.out.println(txt);
wasm.compileToBinary(wasmFile);
}
}
and such an error comes out
Exception in thread "main" de.inetsoftware.jwebassembly.WasmException: Unimplemented Java byte code operation: 42 at Add.java:11
https://en.wikipedia.org/wiki/List_of_Java_bytecode_instructions
And I don't understand why because in this guy's presentation https://www.youtube.com/watch?v=93z9SaLQVVw (40 min+) you can see that it works and compiles
Now C++
I use emscripten, I wanted to do a bubble sort but for the sake of simplicity an example showing the problem
#include <emscripten.h>
using namespace std;
EMSCRIPTEN_KEEPALIVE
int* mainFunction(int table[], int length)
{
int* outTable = new int[length];
for(int i = 0;i<length; i++){
outTable[i] = table[i];
}
return table;
}
By running it with this command test.cpp -s WASM=1 -o test.html
after compiling it, files appear to me from which I extract the appropriate data and in javascript I set and import everything
let wasmExports = null;
let asmLibraryArg = {
abort: () => {},
emscripten_resize_heap: () => {},
};
let info = {
env: asmLibraryArg,
wasi_snapshot_preview1: asmLibraryArg,
};
async function loadWasm() {
let response = await fetch("test.wasm");
let bytes = await response.arrayBuffer();
let wasmObj = await WebAssembly.instantiate(bytes, info);
wasmExports = wasmObj.instance.exports;
}
loadWasm();
and later in the code I use
console.log(wasmExports);
console.log(wasmExports._Z12mainFunctionPii(table, table.length));
results
and when I throw in some array of integers it only throws me the number 0 and I have no idea how to get out of it. Or maybe someone knows another language in which it is possible to compile for wasm and then run it on the website?

Connecting with a third party C++ header file using JNA

I have got a third party dll file with header like below.
class MyClass
{
public:
MyClass() {};
virtual ~MyClass() {};
int Double(int x);
};
I cannot connect with the dll file using JNA (as I cannot extern here). So I have created a wrapper dll. From that wrapper dll , I am trying to connect with the third party dll. wrapper dll header file (Sample.h)
extern "C" {
int __declspec(dllexport) Double(int x);
}
wrapper dll's cpp file
typedef int (__cdecl *MYPROC)(int);
int func(int x)
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
int result;
printf("inside the function");
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("example_dll.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "Double");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
result = ProcAdd(x);
printf("Result: %d", result);
} else {
printf("function not found");
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");
return result;
}
Using a C program(compiled by g++) I can connect with both the dll and get the data.
But from a Java program(using JNA), I can connect with the first dll but first dll cannot connect with the second dll.
Could anybody please enlighten me how to fix the problem? Of if there are any other way to fix it?

Calling java code from C++: exception java.lang.NoSuchMethodError

I am trying to call some java code from a JNI C++ function and I get an exception: java.lang.NoSuchMethodError
The C++ code where the java code is called is a callback function called from another native library.
This code was working perfectly fine when I used the setup described in
here:
Basically an APK that was using a JNI and that JNI was making calls to a native libray compiled elsewhere).
But then I wanted to compile all my code in the AOSP, so I put the code of my native library, the JNI and the APK code in vendor/MyCode/MyApp. It compiles fine, native methods called from the java work OK but java code being called from the JNI now crashes all.
Here is the java code of the method I want to call from the JNI:
package com.android.mycode.myapp;
import android.app.Activity;
public class MainActivity extends Activity implements OnClickListener
{
private native int powerOn();
...
public void ndefRead(int tech, int protocol, byte[] ndef)
{
Log.d(tag, "ndefRead()");
Message msg = mHandler.obtainMessage();
msg.what = MSG_NDEF_READ_RECEIVED;
msg.obj = ndef;
msg.arg1 = tech;
msg.arg2 = protocol;
mHandler.sendMessage(msg);
}
...
}
My JNI code where I register the java data:
JNIEXPORT jint JNICALL
Java_com_android_mycode_myapp_MainActivity_powerOn(JNIEnv * env, jobject obj)
{
LOGD("calling powerOn()"); //Or ANDROID_LOG_INFO, ...
env->GetJavaVM(&javaVM);
jclass cls = env->GetObjectClass(obj);
activityClass = (jclass) env->NewGlobalRef(cls);
activityObj = env->NewGlobalRef(obj);
...
return status;
}
The JNI code of the callback function where the java code is called:
void EventCallback(UINT8 event, tEVT_CBACK_DATA* eventData)
{
LOGD("EventCallback() - event: 0x%x", event);
switch (event)
{
case NDEF_READ_EVT:
{
LOGD("EventCallback() - NDEF_READ_EVT - data length: 0x%x", eventData->ndefReadEvt.length);
JNIEnv *env;
javaVM->AttachCurrentThread(&env, NULL);
jmethodID ndefReadID = env->GetMethodID(activityClass, "ndefRead", "(II[B)V");
if (ndefReadID == 0)
{
LOGD("Function ndefRead() not found");
return;
}
jbyteArray result = env->NewByteArray(eventData->ndefReadEvt.length);
if (result != NULL)
{
env->SetByteArrayRegion(result, 0, eventData->ndefReadEvt.length, (jbyte *) eventData->ndefReadEvt.p_ndef);
}
env->CallVoidMethod(activityObj, ndefReadID, eventData->ndefReadEvt.tech, eventData->ndefReadEvt.protocol, result);
javaVM->DetachCurrentThread();
}
break;
}
The makefile of the JNI looks like this:
LOCAL_MODULE := libinterface
LOCAL_SRC_FILES := interface.cpp
LOCAL_LDLIBS := -llog
LOCAL_SHARED_LIBRARIES := libnfc-nci
And when executing this code I get the following exception/error message:
Pending exception java.lang.NoSuchMethodError thrown by 'unknown throw location'
java.lang.NoSuchMethodError: no non-static method "Lcom/android/mycode/myapp/MainActivity;.ndefRead(II[B)V"
I have looked at several other problems of this type reported on this site but could not quite found something that is similar to mine. If someone has an idea it would be greatly appreciated.
Edited to add code of ndefRead() function and makefile of JNI.
These errors are usually caused by the make file or wrong file naming.
I didn't see any static { System.loadLibrary("your-c-module"); }, as defined in android MK, e.g:
#android.mk
include $(CLEAR_VARS)
LOCAL_MODULE := your-c-module
LOCAL_SRC_FILES := main.cpp
LOCAL_STATIC_LIBRARIES += fancy-library
LOCAL_LDLIBS += -llog -ldl
Also, I don't know if you filtered it out or not, but if Java_com_android_st_nfcnintendothread_MainActivity_powerOn is your c function, then your package name should be: com.android.st.nfcnintendothread and it's function: MainActivity.powerOn.
I am just spit-balling here, hopefully you'll find something useful in the above.

Java use JNI to import shared library in C that uses 3rd party functionality (Python.h)

I have a problem with my JNI integration of "lib.so" that is compiled from "lib.c" that looks like:
#include <jni.h>
#include "messageService.h"
#include <Python.h>
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
JNIEXPORT jboolean JNICALL Java_Test_test
(JNIEnv * pEnv, jclass clazz)
{
Py_Initialize();
pName = PyString_FromString("mylib");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule,"test");
if (pFunc && PyCallable_Check(pFunc)) {
PyObject_CallObject(pFunc, NULL);
Py_DECREF(pFunc);
if (PyErr_Occurred()){
PyErr_Print();
jclass exc = (*pEnv)->FindClass( pEnv, "java/lang/Exception" );
if ( exc != NULL )
(*pEnv)->ThrowNew( pEnv, exc, "in C code; Error while executing \"test\"" );
return (jboolean) 0;
}
return (jboolean) 1;
}
else {
if (PyErr_Occurred())
PyErr_Print();
jclass exc = (*pEnv)->FindClass( pEnv, "java/lang/Exception" );
if ( exc != NULL )
(*pEnv)->ThrowNew( pEnv, exc, "in C code; Cannot find function \"test\"" );
return (jboolean) 0;
}
Py_XDECREF(pFunc);
return (jboolean) 0;
}
else {
PyErr_Print();
jclass exc = (*pEnv)->FindClass( pEnv, "java/lang/Exception" );
if ( exc != NULL )
(*pEnv)->ThrowNew( pEnv, exc, "in C code; Failed to load \"mylib\"" );
}
return (jboolean) 0;
}
and the header file generated with javah -jni ...
Creating the lib.so file works fine.
My java programm:
public class Test {
static{
System.load("/pathtomyso/lib.so")
}
public static native boolean test();
public static void main(String[] args){
boolean result = false;
try{
result = Test.test();
System.out.println(result);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
The result when I run my programm:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /pathtomyso/lib.so: /pathtomyso/lib.so: undefined symbol: Py_Initialize
Do you know why the functionality Py_Initialize from Python.h is unknown?
EDIT:1
I googled and got a wrong answer it seems.
Now I use:
cc lib.c -o lib.so -I/usr/lib/jvm/java-7-openjdk-i386/include/ -I /usr/include/python2.7 -lpython2.7 -shared -lc
But still I have some errors. My python module uses python wrappers for blueZ Bluetooth-Stack. But somehow it cannot import the shared library integrated in this module:
File "/usr/local/lib/python2.7/dist-packages/mylib.egg/mylib/test.py", line 10, in <module>
import bluetooth
File "/usr/local/lib/python2.7/dist-packages/bluetooth/__init__.py", line 34, in <module>
from bluez import *
File "/usr/local/lib/python2.7/dist-packages/bluetooth/bluez.py", line 6, in <module>
import _bluetooth as _bt
/usr/local/lib/python2.7/dist-packages/bluetooth/_bluetooth.so: undefined symbol: PyExc_ValueError
I met the similar problem and had solved it. My problem and solution:
Java call liba.so via JNA, liba.so call libb.so, libb.so call python script. In the liba.so, you should use RTLD_NOW | RTLD_GLOBAL in the dlopen. That's all.

Issues with SHA1 hash implementation in Android

I have two small snippets for calculating SHA1.
One is very fast but it seems that it isn't correct and the other is very slow but correct.
I think the FileInputStream conversion to ByteArrayInputStream is the problem.
Fast version:
MessageDigest md = MessageDigest.getInstance("SHA1");
FileInputStream fis = new FileInputStream("path/to/file.exe");
ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(fis.toString().getBytes());
DigestInputStream dis = new DigestInputStream(byteArrayInputStream, md);
BufferedInputStream bis = new BufferedInputStream(fis);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int ch;
while ((ch = dis.read()) != -1) {
byteArrayOutputStream.write(ch);
}
byte[] newInput = byteArrayOutputStream.toByteArray();
System.out.println("in digest : " +
byteArray2Hex(dis.getMessageDigest().digest()));
byteArrayOutputStream = new ByteArrayOutputStream();
DigestOutputStream digestOutputStream =
new DigestOutputStream(byteArrayOutputStream, md);
digestOutputStream.write(newInput);
System.out.println("out digest: " +
byteArray2Hex(digestOutputStream.getMessageDigest().digest()));
System.out.println("length: " +
new String(
byteArray2Hex(digestOutputStream.getMessageDigest().digest())).length());
digestOutputStream.close();
byteArrayOutputStream.close();
dis.close();
Slow version:
MessageDigest algorithm = MessageDigest.getInstance("SHA1");
FileInputStream fis = new FileInputStream("path/to/file.exe");
BufferedInputStream bis = new BufferedInputStream(fis);
DigestInputStream dis = new DigestInputStream(bis, algorithm);
// read the file and update the hash calculation
while (dis.read() != -1);
// get the hash value as byte array
byte[] hash = algorithm.digest();
Conversion method:
private static String byteArray2Hex(byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash) {
formatter.format("%02x", b);
}
return formatter.toString();
}
I hope there is another possibility to get it running because I need the performance.
I used a high performance c++ implementation which I load with JNI.
For more details write a comment, please.
EDIT:
Requirements for JNI is the Android NDK. For Windows is needed in addition cygwin or something similar.
If you decided for cygwin, I give you some little instructions how to get it working with the NDK:
Download the setup.exe from cygwin and execute it.
Click on Next and choice Install from Internet confirm with Next.
The next two steps adjust the settings as desired and as always click Next.
Select your internet connection and the same procedure as in the final stages.
A download page will catch the eye select it or take just a download page, which is in your country. There is nothing more to say.
We need the packages make and gcc-g++. You can find them using the search in the left upper corner, click on the Skip til a version is displayed and the first field is selected. Do that what we have always done after a selection.
You will get the information, that there are dependencies, which must be resolved. It is usually not necessary to do it yourself and confirm it.
The download and installation started.
If you need you can create shortcuts otherwise click on exceptional Finish.
Download the zip file and extract the NDK to a non space containing path.
You can start now cygwin.
Navigate to the NDK. The path /cydrive gives you all available drives f.e. cd /cygdrive/d navigates to the drive with the letter D.
In the root folder of the NDK you can execute the file ndk-build with ./ndk-build. There should be an error occurs like Android NDK: Could not find application project directory !.
You have to navigate in an Android project to execute the command. So let's start with a project.
Before we can start with the project search for a C/C++ implementation of the hash algorithm. I took the code from this site CSHA1.
You should edit the source code for your requirements.
Now we can start with JNI.
You create a folder called jni in your Android project. It contains all native source files and the Android.mk (more about that file later), too.
Copy your downloaded (and edited) source files in that folder.
My java package is called de.dhbw.file.sha1, so I named my source files similar to find them easily.
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
# How the lib is called?
LOCAL_MODULE := SHA1Calc
# Which is your main SOURCE(!) file?
LOCAL_SRC_FILES := de_dhbw_file_sha1_SHA1Calc.cpp
include $(BUILD_SHARED_LIBRARY)
Java code:
I used the AsyncTask with a ProgressDialog to give the user some feedback about the action.
package de.dhbw.file.sha1;
// TODO: Add imports
public class SHA1HashFileAsyncTask extends AsyncTask<String, Integer, String> {
// [...]
static {
// loads a native library
System.loadLibrary("SHA1Calc");
}
// [...]
// native is the indicator for native written methods
protected native void calcFileSha1(String filePath);
protected native int getProgress();
protected native void unlockMutex();
protected native String getHash();
// [...]
}
Native code (C++):
Remember accessing variables inside native code or other way around using threads needs synchronizing or you will get a segmentation fault soon!
For JNI usage you have to add #include <jni.h>.
For logging insert following include #include <android/log.h>.
Now you can log with __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Version [%s]", "19");.
The first argument is the type of message and the second the causing library.
You can see I had a version number in my code. It is very helpful because sometimes the apk builder doesn't use the new native libraries. Troubleshooting can be extremely shortened, if the wrong version is online.
The naming conventions in the native code are a little bit crasier: Java_[package name]_[class name]_[method name].
The first to arguments are always given, but depending on the application you should distinguish:
func(JNIEnv * env, jobject jobj) -> JNI call is an instance method
func(JNIEnv * env, jclass jclazz) -> JNI call is a static method
The header for the method calcFileSha1(...):
JNIEXPORT void JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_calcFileSha1(JNIEnv * env, jobject jobj, jstring file)
The JDK delivers the binary javah.exe, which generates the header file for the native code. The usage is very simple, simply call it with the full qualified class:
javah de.dhbw.file.sha1.SHA1HashFileAsyncTask
In my case I have to give the bootclasspath additionally, because I use Android classes:
javah -bootclasspath <path_to_the_used_android_api> de.dhbw.file.sha1.SHA1HashFileAsyncTask
That would be the generated file:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class de_dhbw_file_sha1_SHA1HashFileAsyncTask */
#ifndef _Included_de_dhbw_file_sha1_SHA1HashFileAsyncTask
#define _Included_de_dhbw_file_sha1_SHA1HashFileAsyncTask
#ifdef __cplusplus
extern "C" {
#endif
#undef de_dhbw_file_sha1_SHA1HashFileAsyncTask_ERROR_CODE
#define de_dhbw_file_sha1_SHA1HashFileAsyncTask_ERROR_CODE -1L
#undef de_dhbw_file_sha1_SHA1HashFileAsyncTask_PROGRESS_CODE
#define de_dhbw_file_sha1_SHA1HashFileAsyncTask_PROGRESS_CODE 1L
/*
* Class: de_dhbw_file_sha1_SHA1HashFileAsyncTask
* Method: calcFileSha1
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_calcFileSha1
(JNIEnv *, jobject, jstring);
/*
* Class: de_dhbw_file_sha1_SHA1HashFileAsyncTask
* Method: getProgress
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_getProgress
(JNIEnv *, jobject);
/*
* Class: de_dhbw_file_sha1_SHA1HashFileAsyncTask
* Method: unlockMutex
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_unlockMutex
(JNIEnv *, jobject);
/*
* Class: de_dhbw_file_sha1_SHA1HashFileAsyncTask
* Method: getHash
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_de_dhbw_file_sha1_SHA1HashFileAsyncTask_getHash
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
You can change the file without further notice. But do not use javah again!
Class and methods
To get a class instance you can use jclass clz = callEnv->FindClass(CALL_CLASS);. In this case is CALL_CLASS the full qualified path to the class de/dhbw/file/sha1/SHA1HashFileAsyncTask.
To find a method you need the JNIEnv and an instance of the class:
jmethodID midSet = callEnv->GetMethodID(callClass, "setFileSize", "(J)V");
The first argument is the instance of the class, the second the name of the method and the third is the signature of the method.
The signature you can get with the from JDK given binary javap.exe. Simply call it with the full qualified path of the class f.e. javap -s de.dhbw.file.sha1.SHA1HashFileAsyncTask.
You will get an result like:
Compiled from "SHA1HashFileAsyncTask.java"
public class de.dhbw.file.sha1.SHA1HashFileAsyncTask extends android.os.AsyncTas
k<java.lang.String, java.lang.Integer, java.lang.String> {
[...]
static {};
Signature: ()V
public de.dhbw.file.sha1.SHA1HashFileAsyncTask(android.content.Context, de.dhb
w.file.sha1.SHA1HashFileAsyncTask$SHA1AsyncTaskListener);
Signature: (Landroid/content/Context;Lde/dhbw/file/sha1/SHA1HashFileAsyncTas
k$SHA1AsyncTaskListener;)V
protected native void calcFileSha1(java.lang.String);
Signature: (Ljava/lang/String;)V
protected native int getProgress();
Signature: ()I
protected native void unlockMutex();
Signature: ()V
protected native java.lang.String getHash();
Signature: ()Ljava/lang/String;
[...]
public void setFileSize(long);
Signature: (J)V
[...]
}
If the method is found the variable is not equal 0.
Calling the method is very easy:
callEnv->CallVoidMethod(callObj, midSet, size);
The first argument is the given jobject from the "main" method and I think the others are clear.
Remember that you can call from native code although private methods of the class, because the native code is part of it!
Strings
The given string would be converted with following code:
jboolean jbol;
const char *fileName = env->GetStringUTFChars(file, &jbol);
And the other way:
TCHAR* szReport = new TCHAR;
jstring result = callEnv->NewStringUTF(szReport);
It can be every char* variable.
Exceptions
Can be thrown with the JNIEnv:
callEnv->ThrowNew(callEnv->FindClass("java/lang/Exception"),
"Hash generation failed");
You can also check if there is an exception occurred also with JNIEnv:
if (callEnv->ExceptionOccurred()) {
callEnv->ExceptionDescribe();
callEnv->ExceptionClear();
}
Specifications
Java Native Interface Specifications
Build/Clean
Build
After we have created all files and filled them with content, we can build it.
Open cygwin, navigate to the project root and execute from there the ndk-build, which is in the NDK root.
This start the compile, if it is success you will get an output like that:
$ /cygdrive/d/android-ndk-r5c/ndk-build
Compile++ thumb : SHA1Calc <= SHA1Calc.cpp
SharedLibrary : libSHA1Calc.so
Install : libSHA1Calc.so => libs/armeabi/libSHA1Calc.so
If there is any error, you will get the typical output from the compiler.
Clean
Open cygwin, switch in your Android project and execute the command /cygdrive/d/android-ndk-r5c/ndk-build clean.
Build apk
After you have build the native libraries you can build your project. I've found clean, it is advantageous to use the eclipse feature clean project.
Debugging
Debugging of java code isn't different as before.
The debugging of c++ code will follow in the next time.
Do this:
MessageDigest md = MessageDigest.getInstance("SHA1");
InputStream in = new FileInputStream("hereyourinputfilename");
byte[] buf = new byte[8192];
for (;;) {
int len = in.read(buf);
if (len < 0)
break;
md.update(buf, 0, len);
}
in.close();
byte[] hash = md.digest();
Performance comes from handling data by blocks. An 8 kB buffer, as here, ought to be blocky enough. You do not have to use a BufferedInputStream since the 8 kB buffer also serves as I/O buffer.
The reason the fast one is fast and incorrect is (I think) that it is not hashing the file contents!
FileInputStream fis = new FileInputStream("C:/Users/Ich/Downloads/srware_iron.exe");
ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(fis.toString().getBytes());
The fis.toString() call does not read the contents of the file. Rather it gives you a string that (I suspect) looks something like this:
"java.io.FileInputStream#xxxxxxxx"
which you are then proceeding to calculate the SHA1 hash for. FileInputStream and its superclasses do not override Object::toString ...
The simple way to read the entire contents of an InputStream to a byte[] is to use an Apache Commons I/O helper method - IOUtils.toByteArray(InputStream).
public void computeSHAHash(String path)// path to your file
{
String SHAHash = null;
try
{
MessageDigest md = MessageDigest.getInstance("SHA1");
InputStream in = new FileInputStream(path);
byte[] buf = new byte[8192];
int len = -1;
while((len = in.read(buf)) > 0)
{
md.update(buf, 0, len);
}
in.close();
byte[] data = md.digest();
try
{
SHAHash = convertToHex(data);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeToast(getApplicationContext(),"Generated Hash ="+SHAHash,Toast.LENGTH_SHORT).show();
}
private static String convertToHex(byte[] data) throws java.io.IOException
{
StringBuffer sb = new StringBuffer();
String hex = null;
hex = Base64.encodeToString(data, 0, data.length, NO_OPTIONS);
sb.append(hex);
return sb.toString();
}

Categories

Resources