negoose
negoose

Reputation: 177

Using native code in eclipse android application

I have an android application project in eclipse and I need to be able to create gif files from bitmaps. So after searching around, I found out that there is some C/C++ code to do exactly this. My problem is that I don't know how to import this code into my project. I've looked around and I believe I'm supposed to add a JAR file to the library build path, but I have no JAR file, only a few C/C++ files (see link).

I have zero experience with C/C++ and am only using it for creating gif files. Basically I'm wondering if someone can guide me in being able to use the C/C++ code. I've tried installing NDK and cygwin, but don't know what else to do. Here's the code I've tried, but it failed because it cannot locate gifflen.

static {
    System.loadLibrary("gifflen");
}

public native int Init(String gifName, int w, int h, int numColors,
        int quality, int frameDelay);

public native void Close();

public native int AddFrame(int[] inArray);

public void download(DrawCanvas canvas) {
    String path = DOWNLOADS_PATH + File.separator + mName + ".gif";
    int width = canvas.getWidth();
    int height = canvas.getHeight();
    int result = Init(path, width, height, 256, 100, 4);
    if (result == 0) {
        List<AbstractShape> oldShapes = canvas.getShapes();
        for (Page page : mPages) {
            canvas.setShapes(page.getShapes());
            Bitmap bitmap = canvas.getBitmap();
            int[] pixels = new int[width * height];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            AddFrame(pixels);
        }
        canvas.setShapes(oldShapes);
        Close();
    } else {
        Log.d("DEBUG", "init failed");
    }
}

I've created a jni folder in my project folder and placed all the files from the link inside it. The files are:

Any help would be greatly appreciated. Thanks!!

Edit:

I've tried what you said, I replaced the Android.mk code with what you provided and created a Source and Include folder. I placed all .c and .cpp files in Source and .h in Include and left the Android.mk outside, in the jni directory. But I still got this error in logcat when I tried to run:

08-12 02:33:14.834: E/AndroidRuntime(8563): FATAL EXCEPTION: main
08-12 02:33:14.834: E/AndroidRuntime(8563): java.lang.ExceptionInInitializerError
08-12 02:33:14.834: E/AndroidRuntime(8563):     at edu.foothill.myflipbook.MainActivity.onCreate(MainActivity.java:35)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at android.app.Activity.performCreate(Activity.java:4524)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2189)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at android.app.ActivityThread.access$600(ActivityThread.java:139)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at android.os.Looper.loop(Looper.java:154)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at android.app.ActivityThread.main(ActivityThread.java:4894)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at java.lang.reflect.Method.invokeNative(Native Method)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at java.lang.reflect.Method.invoke(Method.java:511)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at dalvik.system.NativeStart.main(Native Method)
08-12 02:33:14.834: E/AndroidRuntime(8563): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load gifflen: findLibrary returned null
08-12 02:33:14.834: E/AndroidRuntime(8563):     at java.lang.Runtime.loadLibrary(Runtime.java:365)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at java.lang.System.loadLibrary(System.java:535)
08-12 02:33:14.834: E/AndroidRuntime(8563):     at edu.foothill.myflipbook.Flipbook.<clinit>(Flipbook.java:33)
08-12 02:33:14.834: E/AndroidRuntime(8563):     ... 15 more

I tried ndk-build in cmd, but got this error instead:

Android NDK: WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersi
on 11 in ./AndroidManifest.xml
Android NDK: jni/Android.mk: Cannot import module with spaces in tag: ''
Android NDK: jni/Android.mk: Cannot find module with tag '' in import path
Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ?

Android NDK: The following directories were searched:
Android NDK:
jni/Android.mk:9: *** Android NDK: Aborting.    .  Stop.

Upvotes: 0

Views: 3355

Answers (1)

Zax
Zax

Reputation: 3010

You first need to learn about JNI (Java native Interfaces) using which you can bridge your c/c++ code with the java/android code. I have previously worked on ndk so i suggest you to do some readup from android-ndk readme.

Requirements:

*Download Android-ndk from Android official website.

*Install cygwin for windows, i recommend you to use linux for development in ndk as you have a readymade terminal available there.

*set environment variable with ndk-path.

*Inside your Android project create a directior "jni"

*This folder should contain all your c/c++ files. and an android makefile "Android.mk" using which the android ndk-build will compile your c/c++ program you need to use in you android application.

*Now how to use JNI to glue your android app with your c/c++ code you can see in the videos of "Marakana for android ndk". Link for the same is given below:

Tutorial

The contents of makefile for your requirements are:

    LOCAL_PATH := $(call my-dir)
    TARGET_ARCH_ABI := armeabi-v7a
    include $(CLEAR_VARS)
    LOCAL_MODULE    := gifflen
    LOCAL_ARM_MODE := arm
    LOCAL_C_INCLUDES := \./Include/
    LOCAL_SRC_FILES := \./Source/dib.c \./Source/gifflen.c
    include $(BUILD_SHARED_LIBRARY)
    $(call import-module)

Under jni, put your Create a source foler and Include folder. Addyour .c/cpp files in Source directory and .h files in Include directory

Upvotes: 2

Related Questions