Android NDK: Link static library to your JNI library

When you’re developing an application that needs to use a custom, native precompiled library (the .a file) together with your gluing JNI interface maybe you’re wondering how to link it.
It’s fairly simple and you can even make it target architecture aware (separate precompiled libraries for arm, arm-v7a, x86).

In your module (I’m using Android Studio) you should have a jni directory, like this:

project /
  + module
    + src
      + main
        + jni           # JNI source files here and other native sources here
          | Android.mk
          | Application.mk
          | Xjni.c
          + x86         # target x86 ABI directory
            | libX.a    # x86 precompiled static library
          + armeabi
            | libX.a
          + armeabi-v7a # and so forth
          + mips

Now for the contents of Android.mk:

LOCAL_PATH := $(call my-dir)
# prepare libX
include $(CLEAR_VARS)
LOCAL_MODULE    := libX
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libX.a
include $(PREBUILT_STATIC_LIBRARY)

# build JNI
include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := X
LOCAL_MODULE    := Xjni
LOCAL_SRC_FILES := Xjni.c
include $(BUILD_SHARED_LIBRARY)

Now from the project root folder issue a command NDK_PROJECT_PATH=module/src/main ndk-build and you’re set. In the module/src/main/libs directory you should have your compiled JNI library that will be copied by gradle assemble to your APK. For all target ABIs.

Have fun!

LinkedIn

Improve Dell e6430 touchpad support in Ubuntu 12.04

Even though Ubuntu works on this Dell there are some quirks involved. One of the major pains is the non-working touchpad – that is touch and clicking works fine but there’s no support for scrolling.

Dell does support Ubuntu, however only for their XPS series that come with Ubuntu preinstalled. You won’t get support for other laptops.

There is a solution though thanks to Dahetral Systems

What you need to do is following:

  1. Go to Dahetral’s download page and grab latest ALPS driver (1.3 as of this post)
  2. Unpack the driver and copy psmouse-alps-1.3 directory to /usr/src
  3. Add this driver to DKMS: sudo dkms add /usr/src/psmouse-alps-1.3
  4. Perform DKMS install: sudo dkms autoinstall
  5. Reload driver: sudo rmmod psmouse && sudo modprobe psmouse

I also recommend installing package gpointing-device-settings package. It offers more options than standard mouse configuration tool.

LinkedIn

Enable foreign keys on Android SQLite

Foreign keys on database serve the purpose to enforce constraints, to maintain database integrity. Another purpose is that way one may implement “table inheritance” in SQL. You place common set of data in one table, specific info in set of tables and binding them via a foreign key. On Android most common way to operate on SQLite is to derive a class from `SQLiteOpenHelper

public class ContentHelper extends SQLiteOpenHelper {
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(some create table string);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

This is basic stuff that correctly extends `SQLiteOpenHelper`. `onCreate()` method is called after database file is established and connection open; database is ready to execute SQL queries. Due to that we need to enable foreign keys before executing any query creating a table. It is sufficient to put following code into beginning of this method:

if (!db.isReadOnly()) {
    // Enable foreign key constraints
    db.execSQL("PRAGMA foreign_keys=ON;");
}

The same code might be used on onConfigure(SQLiteDatabase) but this is available since API 16. This method is called when the database connection is being configured, to enable features such as write-ahead logging or foreign key support.

LinkedIn