native development
DESCRIPTION
Presentation dedicated to native development of android applications.TRANSCRIPT
![Page 1: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/1.jpg)
Native DevelopmentAndroid
![Page 2: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/2.jpg)
Для чего?
Игры, в которых много графики Ресурсоемкие алгоритмы
обработки image/audio processing
Подключение библиотек на C/C++ FAAC, LAME, FFMPEG…
Выйти за пределы кучи в Java (≈24MB) android.os.Debug.getNativeHeapAllocate
dSize()
![Page 3: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/3.jpg)
Подготовка
Скачать Android NDK (Revision 5b Jan 10) При распаковке zip избегайте путей с
пробелами. (всегда и везде избегайте путей с пробелами)
Добавить в PATH путь к распакованному NDK
Cygwin for Win (with gcc/make/smth else)
gcc/make/smth else for *nix
![Page 4: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/4.jpg)
NDK
ndk-build скрипт в корне lib и include – либы и h-
ники, которые можно подключить
samples – примеры docs - хелп
![Page 5: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/5.jpg)
Native project
Находится в папке jni в корне Android-проекта
Содержит android.mk — make-файлы для ndk-build
![Page 6: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/6.jpg)
Root Android.mk
include $(call all-subdir-makefiles)
![Page 7: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/7.jpg)
Single project’s Android.mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)
LOCAL_ARM_MODE := armLOCAL_CFLAGS := $(LOCAL_CFLAGS) -O3LOCAL_CPPFLAGS := $(LOCAL_CPPFLAGS) -O3
LOCAL_MODULE := coreLOCAL_SRC_FILES := AacEncoder.cpp \
AudioEncodingController.cpp \AudioEncodingJni.cpp \EncoderConfiguration.cpp \LameEncoder.cpp
LOCAL_LDLIBS := -ldl -llog -lcLOCAL_STATIC_LIBRARIES := faac lame
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../faac-1.28/include/ \$(LOCAL_PATH)/../lame-3.98.4/include/ \$(LOCAL_PATH)/../libogg-1.2.2/include/ogg/
include $(BUILD_SHARED_LIBRARY)
required
optimization
required
dependencies
required
good style
![Page 8: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/8.jpg)
Связка с Java-кодом (JNI)
Хороший стиль — хранить нативные вызовы в отдельном классе
public class AudioCoreJNI { static { System.loadLibrary("core"); Log.d(AudioCoreJNI.class.getSimpleName(), "library loaded"); } ... private native void nativeSetLogging(boolean on); private native boolean nativeInitialize(int[] configuration); private native int nativeGetOutputBufferSize(int samplesCount); private native byte[] nativeFlush(); private native void nativeClose();}
![Page 9: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/9.jpg)
Связка с Java-кодом (JNI)
#include <jni.h>
JNIEXPORTjboolean initialize(JNIEnv* env, jobject thiz, jintArray configuration) {
void Java_com_example_nativeaudio_NativeAudio_createEngine(JNIEnv* env, jclass clazz){
![Page 10: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/10.jpg)
OnLoad()
JNINativeMethod methods[] = {{"nativeInitialize", "([I)Z", (bool*)initialize },{"nativeEncode", "([BII[B)I", (void*)encode}};
int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) {
jclass clazz; clazz = env->FindClass("com/shaubert/android/aaf/jni/AudioCoreJNI"); if (clazz != NULL) {
if (env->RegisterNatives(clazz, gMethods, 2) >= 0) { return 0; }
} return -1;}
jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env = NULL; if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) {
if (jniRegisterNativeMethods(env, classPathName, methods, 2) > 0) { return JNI_VERSION_1_4; }
} return -1;}
![Page 11: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/11.jpg)
Native logging
#include <android/log.h>
if (logging) { LOGD(“test logging %s %d...“, “hello”);}
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, “LOG_TAG", __VA_ARGS__)
![Page 12: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/12.jpg)
Compiling
![Page 13: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/13.jpg)
Compiling
![Page 14: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/14.jpg)
Running Android project
![Page 15: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/15.jpg)
Fixing, Compiling, Running
![Page 16: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/16.jpg)
Reading crash logs
http://code.google.com/p/android-ndk-stacktrace-analyzer/ logcat from your android device, containing a stacktrace asm file of the library being debugged. You generate this
file by using the NDK's objdump tool:...android-ndk/toolchains/arm-linux-androideabi-4.4.3/
prebuilt/…/arm-eabi-objdump -S mylib.so > mylib.asm
Use the script by running:python parse_stack.py <asm-file> <logcat-file>
Output:
![Page 17: Native Development](https://reader033.vdocuments.us/reader033/viewer/2022052601/5590cde11a28abf8388b4593/html5/thumbnails/17.jpg)
Спасибо за внимание
Презентацию подготовил Шауберт Александр
Специально для Android May Days 2011