Android Apps

Integrate native library to Android gradle project – Sample project

June 11, 2015 by Sarath DR in Android Apps, Featured with 0 Comments

Recently I have tried to integrate native libraries with some project. I could not find any good example or a better blog which explains it properly, especially when you integrate NDK with Gradle, the configuration is always a nightmare.

You can follow the below steps to add an native libraries to your android projects. Before you start make sure that you have downloaded android native development kit(NDK). Please find more details from the below link.

https://developer.android.com/tools/sdk/ndk/index.html

Step 1: Create a project with Android studio and add below line to local.properties file to include the NDK build system.

ndk.dir=/usr/local/android-ndk

Screen Shot 2015 06 11 at 18.05.50 300x131 Integrate native library to Android gradle project   Sample project

Step 2: Create a new JNI folder in the main and then create Android.mk file as specified below.

 
# You can find more detail about this make file from the below link
# http://www.kandroid.org/ndk/docs/ANDROID-MK.html
# It basically describes the shared libraries to the NDK build system
 
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
 
# Specify the module name
LOCAL_MODULE    := MyJni
LOCAL_SRC_FILES := my-jni.c

Screen Shot 2015 06 11 at 18.06.27 300x102 Integrate native library to Android gradle project   Sample project

Screen Shot 2015 06 11 at 18.30.47 300x200 Integrate native library to Android gradle project   Sample project

Step 3: Create my-jni.c inside your JNI folder with a method that returns a static string.

#include <string.h>
#include <jni.h>
 
jstring
Java_com_sarathdr_tech_nativeandroidapptest_MainActivity_stringFromJNI(JNIEnv* env, jobject callingObject)
{
    return (*env)->NewStringUTF(env, "Fine! JNI is working ");
}

Step 4: Add the local module in build.gradle

  defaultConfig {
        applicationId "com.sarathdr.tech.nativeandroidapptest"
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
 
        ndk {
            moduleName "MyJni"
        }
    }

Step 5: For testing, load the static module in main activity

public class MainActivity extends Activity {
 
     // Load native module here
    static {
        System.loadLibrary("MyJni");
    }
 
    // Add interface to the native method
    public native String stringFromJNI();
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
 
        String testString = stringFromJNI();
 
        TextView mainText = (TextView) findViewById(R.id.main_text);
        mainText.setText(testString);
    }
}

Finally: To import native shared libraries, follow below format. The below line imports OpenSL , android, log libraries to your project.

defaultConfig {
        applicationId "com.example.nativeaudio"
        minSdkVersion 14
        targetSdkVersion 14
 
        ndk {
            moduleName "MyJni"
            ldLibs "OpenSLES", "android", "log"
        }
    }

Add below line in the make file to load the shared libraries

  include $(BUILD_SHARED_LIBRARY)

I have shared the sample project in GitHub, download it for more details. Google I/O 2015 announces that Android Studio 1.3 provides support for Native codes. So this will be much easier icon wink Integrate native library to Android gradle project   Sample project

github button e1355486394525 Integrate native library to Android gradle project   Sample projectGit Project Link Download
Sample project – Integrating native code into Android Gradle Project


Tagged , , ,

Share this to

Related Posts

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close