1

I have lib's source codes, I want to create static libs for android. How can I do this? Is there a way to do this with experimental gradle?. Also I want to build for different archs (arm7,x86 etc)

Vardan95
  • 602
  • 1
  • 7
  • 15
  • The latest Android Studio (2.2 preview 5) supports the Android NDK (Native Development Kit) and is available from here: http://tools.android.com/ . – Richard Critten Jul 10 '16 at 21:49
  • Thanks @Richard for info, but can you please specify how should I build the lib? – Vardan95 Jul 11 '16 at 04:44
  • Please note that you cannot directly use static libs for you android app. You will need a shared library (or libraries) that will wrap these libs, and load these shared libs. – Alex Cohn Jul 11 '16 at 10:01
  • Thanks @Alex for comment, are you sure that there is no other way? I want to keep lib's files seperated from jni files. – Vardan95 Jul 11 '16 at 17:31
  • I don't understand your concern. This is how it works: you have one or more static libs (you can get them prebuilt, or build them from sources as a custom gradle task, or with the help of *experimental* plugin). You have the JNI wrapper that uses these static libs and produces a shared lib, and it's worthwhile to make this step part of the *experimental* gradle plugin. The JNI source files may be completely separate from the static libs, but in the end the linker must produce a shared lib, Java simply cannot load static lib alone. – Alex Cohn Jul 12 '16 at 09:18
  • @AlexCohn thanks for explanation, one more question: is it possible to create static lib that includes other static lib? And if yes, should I put second lib's .a file together with first lib's file. – Vardan95 Jul 12 '16 at 19:52
  • If "includes" means "uses", the answer is definitely positive, and in this case your shared library will link both **.a** files – Alex Cohn Jul 13 '16 at 04:11
  • If you create **libb.a** which contains some **b** objects and also all objects from **liba.a**, it will be enough to pass only `-lb` argument to the linker. There are few ways to create such **libb.a**, see e.g. http://stackoverflow.com/questions/3821916/how-to-merge-two-ar-static-libraries-into-one. I don't know why you should bother to do this. – Alex Cohn Jul 13 '16 at 04:18
  • I have libb.a which uses liba.a I have included libb.a into my project, after build I am getting linker error. – Vardan95 Jul 13 '16 at 04:32

1 Answers1

0

The easiest way to built native binaries for Android is to use ndk-build tool. Here is complete docs about how to setup build for your project.

To be brief you need to create two extra files:

Application.mk:

# an example setup
APP_ABI := armeabi-v7a x86
APP_PLATFORM := android-16
APP_STL := stlport_static
NDK_TOOLCHAIN_VERSION := 4.8

And Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := <lib_name> # put 'foo' to built libfoo.a
LOCAL_LDLIBS := -llog -lm # whatever libs you need
LOCAL_SRC_FILES := <source_files>
include $(BUILD_STATIC_LIBRARY)

Then you can launch build with next command:

$ ndk-build NDK_PROJECT_PATH=path/to/intermediate/results/dir NDK_LIBS_OUT=path/to/output/dir APP_BUILD_SCRIPT=path/to/Android.mk NDK_APPLICATION_MK=path/to/Application.mk

P.S. Regardless that Android Studio has improved NDK support, using of ndk-build along with hand-written makefiles gives more flexibility to your project IMHO. Anyway you can easily embed ndk-build based build via gradle's Exec task.

Sergio
  • 8,099
  • 2
  • 26
  • 52
  • Thanks for answer, but by this way will I get header files? I am trying to build dlib for android and it's has more than 1500 files, and I need header files to use in my c++ code. – Vardan95 Jul 11 '16 at 17:27