14. JNI in C and C++
#include <jni.h>
C
struct JNINativeInterface {
jclass
(*FindClass)(JNIEnv*, const char*);
}
typedef const struct JNINativeInterface* JNIEnv;
!
JNIEnv* env;
(*env)->FindClass(env, “classname”);
15. JNI in C and C++
C++
struct _JNIEnv {
jclass FindClass(const char* name)
{ return functions->FindClass(this, name); }
}
!
JNIEnv* env;
env->FindClass(“classname”);
17. Mapping native functions
.java
public native static int testNative(int a, int b);
.c
jint Java_com_example_jnibasics_NativeDemo_testNative(JNIEnv *env, jclass obj, jint a, jint b)
package name
javah
> javah com.example.jnibasics.NativeDemo
25. Further reading
•
Attaching Java threads
•
Creating new Java objects from native code
•
Distinguish between virtual and non virtual methods
•
Exception handling
•
Accessing Java arrays
27. Performance
•
Most applications don’t need native code!
•
Only for extensive calculations: games, rich media
•
Take advantage of NEON CPU instructions
•
Avoiding Java Garbage Collection
31. 1.
2.
3.
4.
Register as a developer (£60 per year)
Add device UUID to dev account
Generate Provisioning Profile
Sign APK with developer’s certificate
!
or Submit to Apple Store
or Jailbreak device
Binary is encrypted
Decryption is on OS level
Self-signed APK
or not-signed at all
Decompiled Objective C:
Decompiled Java:
class structures
assembly code
readable code
33. Summary
•
Always obfuscate Java code!
•
Never save passwords, use session key or hash
instead
•
Never keep encryption keys in clear data in memory
•
Keep all critical code in native
35. Tips
•
Debugging native code is tricky
•
•
•
Linux or OSX as dev platform
Use ARM DS-5 Community Edition in Eclipse
Android fragmentation
•
separate .so files for different version