Setting up 39dll for android sdk ndk - nterry/39DLL-4-Linux GitHub Wiki

I wanted to share my own experience with setting up 39dll on android ndk, so maybe some newbies like myself, will find this helpful :)

  1. Firstly download android sdk, install it (you can easily find on google how to set it up).

  2. Then download android ndk, set it up also.

  3. Download nterry/39DLL-4-Linux source files.

  4. Now I'm not sure, but I believe you don't need to build this lib right now, because everytime you run this on android emulator, it will automatically build it. But anyway lets do this: Start CMD, and write without (comments): cd **(the path where you set up ndk, like this: C:\Android_SDK\ndk, if you see program ndk-build.cmd, that means you're in correct path :) )ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=(the path where you downloaded source files of this lib, where Application.mk is located)**Application.mk The cmd command will look something like : cd C:\Android_SDK\ndk\ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=C:\Users\Dainius\AndroidWorkspace\jni\Application.mk But I think this step is not necessary.

  5. next step. Open eclipse(android sdk), set it up of course (add emulator and so on..)

  6. Create new android project. (min API version 8, targer API can be 17)

  7. add native support: Eclipse: Android Tools -> Add Native Support. http://s9.postimg.org/cz6qdr64v/natives.png and name it: 39dll-4-android This will create jni folder in android project.

  8. Move some source files to that jni folder. It should contain these files: Android.mk (you will need to change this) Application.mk (add from this library,you will need to change this) android-src (add from this library) 39dll-4-android.cpp (just delete this.) How this file should look like: Android.mk : <LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog LOCAL_MODULE := 39dll-4-android LOCAL_SRC_FILES := android-src/main.cpp android-src/md5.cpp android-src/socket.cpp android-src/tools.cpp android-src/buffer.cpp android-src/list.cpp #INCLUDE_HEADERS := sys/socket.h sys/types.h netinet/in.h arpa/inet.h netinet/tcp.h stdio.h stdlib.h string.h iostream sys/ioctl.h include $(BUILD_SHARED_LIBRARY)> Application.mk : APP_OPTIM := release APP_ABI := armeabi APP_PLATFORM := android-8 APP_BUILD_SCRIPT := C:\Users\Dainius\AndroidWorkspace\BardsGrimoire\jni\Android.mk APP_STL := gnustl_static

  9. Build the project (Right mouse btn>build). now the library, is included. ready for working.

  10. Now for starting using this lib, you will need to change something important: in jni/android-src folder, there is main.cpp file. You need to add this line at start: include <jni.h>

then you will see many methods:

attribute((visibility("default"))) char* lastinIP(); attribute((visibility("default"))) double lastinPort(); attribute((visibility("default"))) double setsync(double sockid, double mode); attribute((visibility("default"))) double closesock(double sockid); attribute((visibility("default"))) double socklasterror(double sockid); attribute((visibility("default"))) char* myhost(); attribute((visibility("default"))) double compareip(charip, charmask);

you won't be able to call them right now, because it is not configured properly for android. You need to change c++ types, to java types, because those are different.

attribute((visibility("default"))) double dllInit(); <- change this to this: attribute((visibility("default"))) jint JNICALL Java_com_straysheep_bardsgrimoire_MainActivity_dllInit(JNIEnv *env, jclass clz);

it looks very ugly :/ basically you need to add this to every method name: Java_com_straysheep_bardsgrimoire_MainActivity_ <- custom name, that you set up for your project.

The most important thing is that java data types, and c++ data types are different, and its a pain in the neck to convert them... for example, java char has 16 bits, and c++ char has 8 bits. and so on...

I have prepared, important methods my self so I will share my edited main.cpp file: http://www.mediafire.com/download/hat8hnqcz9gbhfz/main.cpp

You can remake other methods accordingly to mine.

  1. when this is done, open your MainActivity.java and add this to the end:

public native int dllInit(); public native int tcpconnect(String ip, float port, boolean mode); public native double setnagle(float jsockid, boolean value); public native static float sendmessage(float jsockid, String jip, float jport); public native static int receivemessage(int jsockid); public native static float bufferencrypt(String jpass); public native static int clearbuffer(); public native static float readbyte(); public native static int writebyte(int jval); public native static float writestring(String jstr); public native static String readstring(); public native static String md5string(String str);

static {
    System.loadLibrary("39dll-4-android");
}
  1. **done! ** now you can call dllInit(), and then do tcpconnect("10.0.2.2", (float)1337, true); localhost on android emulator is "10.0.2.2", and 127.0.0.1(on pc server) 1337 (my custom tutorial port) and then make sure to set setnagle(server_socket, false); and so on... :)

  2. well I hope I didn't missed anything. If I will find an error, I'll fix it :) if you get troubles on setting this up, contact me at: [email protected]

⚠️ **GitHub.com Fallback** ⚠️