Use of prebuild static Library compiled with Android Toolchain

Just to continue with my last post in which I compiled two simple C++ files and archived as a static library. So make sure to open previous blog  in another tab because I will refer this in current post as well.

Now in this Post i will use the same static library in an Android project. Following are the simple steps.

Step 1: Create an Android Project

  • Create a new android project having name “CrossCompilation”.
  • Set the package name as “com.tmz.cc”
  • Create a default activity name “MainActivity”
  • Update the layout file “main.xml” and xml contains

<?xml version=”1.0″ encoding=”utf-8″?>
<GridLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;

android:layout_width=”match_parent”
android:layout_height=”match_parent”

android:useDefaultMargins=”true”
android:alignmentMode=”alignBounds”
android:columnOrderPreserved=”false”

android:columnCount=”4″
>

<TextView
android:text=”Cross Compiled Library Testing”
android:textSize=”16dip”

android:layout_columnSpan=”4″
android:layout_gravity=”center_horizontal”
/>

<TextView
android:text=”Native methods calls Results:”
android:textSize=”16dip”
android:layout_columnSpan=”4″
android:layout_gravity=”left”
/>

<TextView
android:text=”Test 1:”
android:layout_gravity=”right”
/>

<TextView
android:id = “@+id/test1Result”
android:text=”Press Button to get result”
android:ems=”10″
/>

<TextView
android:text=”Test 2:”
android:layout_column=”0″
android:layout_gravity=”right”
/>

<TextView
android:id = “@+id/test2Result”
android:text=”Press Button to get result”
android:ems=”10″
/>

<Space
android:layout_row=”4″
android:layout_column=”0″
android:layout_columnSpan=”3″
android:layout_gravity=”fill”
/>

<Button
android:text=”Call Native Methods”
android:layout_row=”4″
android:layout_column=”1″
android:id = “@+id/btnCallNative”
/>
</GridLayout>

  • Now Open the MainActivity.java file and following are its contents

package com.tmz.cc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button btnCallNative;
private TextView tvTest1Result;
private TextView tvTest2Result;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCallNative = (Button)findViewById(R.id.btnCallNative);
tvTest1Result = (TextView)findViewById(R.id.test1Result);
tvTest2Result = (TextView)findViewById(R.id.test2Result);

btnCallNative.setOnClickListener(new View.OnClickListener(){
public void onClick(View view)
{
callNativeMethods();

}
});

//setContentView(R.layout.main);

}
private native int test1();
private native int test2();

public void callNativeMethods()
{
System.loadLibrary(“ccSharedLib”);
int a = test1();
int b = test2();
Integer res1= new Integer(a);
Integer res2 = new Integer(b);
tvTest1Result.setText(res1.toString());
tvTest2Result.setText(res2.toString());
System.out.print(a);
System.out.print(b);

}
}

Now we are done with Java side. Its time to add native side in our project.

Step 2: Add native code and pre-build binaries

  • Create a directory on root of project having name “jni”
  • Add two more directories in “jni” directory.

1)”include” (which contains the headers files of pre-build static library)  and put Test1.h and Test2.h in this directory which I created in the previous blog.  See previous blog 
2) “prebuild” (which contains the pre-build static library created in previous blog) and put the ccStaticLib.a in this directory See previous blog 

  • Add two files in “jni” directory

1) “Android.mk” and its contents are

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := ccStaticLibrary
LOCAL_SRC_FILES := prebuild/ccStaticLib.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := ccSharedLib
LOCAL_SRC_FILES := main-jni.cpp
LOCAL_STATIC_LIBRARIES := ccStaticLibrary
include $(BUILD_SHARED_LIBRARY)

2) “main-jni.cpp” and its contents are
#include <jni.h>
#include <stdio.h>

#include “include/test1.h”
#include “include/test2.h”

extern “C” {

jint
Java_com_tmz_cc_MainActivity_test1( JNIEnv*  env,
jobject  thiz)
{
jint temp = 0;
temp = test1(4,5);
return temp;
}

jint
Java_com_tmz_cc_MainActivity_test2( JNIEnv*  env,
jobject  thiz)
{
jint temp = 0;
temp = test2(4,5,6);
return temp;
}

}

We are done with the coding stuff.

Now we need to compile the project both the native side as well as the Java (Android side) and after compilation you can run this and see the native calls results by pressing the button on main layout.

Advertisements
This entry was posted in Android, C-C++, Cross-Compilation, Development, java, Mobile and tagged , , , . Bookmark the permalink.

3 Responses to Use of prebuild static Library compiled with Android Toolchain

  1. Hiep says:

    Thank you for sharing this post. It is very helpful. I followed your instruction from previous post, built lib .a successful, but stucked when integrate .a file to android project. (I used eclipse). I’m facing the error: jni/prebuild/ccStaticLib.a: error adding symbols: File in wrong format. I googled but this issue still not find out the solution. Do you have any advice for me? Thank you.

  2. tariqzubairy says:

    This problem may occur when your static library is build for some other platform/architecture.

    • Phạm Hoàng Hiệp says:

      Hi. Thank you for your reply. I built directly from C++ class to static library (.a) then shared library (.so). I solved this problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s