use prebuild static lib on another android project

转载 2016年05月30日 22:16:52

前言

在NDK EXE工程中, 使用另外一个android静态库工程编译出来的静态库.a.
在android-ndk-r10e\samples中找了下,并没有这种用法。
上网找了下,有人做过了. 主要是EXE工程的.mk写法不同.

试验记录

静态库工程

// hw\lib\StaticLib1\jni\StaticLib1.cpp

#include <jni.h>

extern "C" int fn_test_add(int x, int y) {
    return (x + y);
}
# hw\lib\StaticLib1\jni\Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := StaticLib1
LOCAL_SRC_FILES := StaticLib1.cpp

include $(BUILD_STATIC_LIBRARY)
# hw\lib\StaticLib1\jni\Application.mk
# release for arm
APP_ABI := armeabi-v7a

# HM1SC is 4.4.4 android-19
APP_PLATFORM := android-19

在 hw\lib\StaticLib1\jni\ 中运行ndk-build
编译出来的静态库在 hw\lib\StaticLib1\obj\local\armeabi-v7a\libStaticLib1.a
将 libStaticLib1.a 拷贝到 forRelease备用, 其它工程要用libStaticLib1.a时,就从forRelease目录直接拷贝,不用再编译了.

EXE工程

// hw\exe\MainTest\jni\MainTest.cpp
#include <jni.h>
#include <stdlib.h>
#include <stdio.h>

extern "C" int fn_test_add(int x, int y);

extern "C" int main(int argc, char** argv) {
    int x = 1;
    int y = 2;
    int sum = fn_test_add(1, 2);

    printf("%d = fn_test_add(%d,%d)\r\n", sum, x, y);

    return 0;
}
# hw\exe\MainTest\jni\Android.mk

LOCAL_PATH := $(call my-dir)

# prepare libX
include $(CLEAR_VARS)
LOCAL_MODULE    := libStaticLib1
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libStaticLib1.a
include $(PREBUILT_STATIC_LIBRARY)

# main prog
include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := libStaticLib1

LOCAL_MODULE    := MainTest
LOCAL_SRC_FILES := MainTest.cpp

include $(BUILD_EXECUTABLE)
# hw\exe\MainTest\jni\Application.mk

# release for arm
APP_ABI := armeabi-v7a

# HM1SC is 4.4.4 android-19
APP_PLATFORM := android-19

先编译一下EXE工程,看看静态库要放哪个位置才合适
在 hw\exe\MainTest\jni 运行 ndk-build

D:\ls\hw\exe\MainTest\jni>ndk-build
Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 8 in D:/ls/hw/exe
/MainTest/AndroidManifest.xml
Android NDK: ERROR:D:/ls/hw/exe/MainTest/jni/Android.mk:StaticLib1: LOCAL_SRC_FILES points to a miss
ing file
Android NDK: Check that D:/ls/hw/exe/MainTest/jni/armeabi-v7a/libStaticLib1.a exists  or that its pa
th is correct
D:/android-ndk-r10e/build/core/prebuilt-library.mk:45: *** Android NDK: Aborting    .  Stop.

将开始备份(或者别人给的, 没有源码)的libStaticLib1.a拷贝出来, 根据报错提示, 将libStaticLib1.a放到/jni/armeabi-v7a下,再次运行ndk-build

D:\ls\hw\exe\MainTest\jni>ndk-build
Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 8 in D:/ls//hw/exe
/MainTest/AndroidManifest.xml
[armeabi-v7a] Compile++ thumb: MainTest <= MainTest.cpp
[armeabi-v7a] Executable     : MainTest
[armeabi-v7a] Install        : MainTest => libs/armeabi-v7a/MainTest

测试

编译成功了, 将编译出的MainTest用adb放到真机上跑一下,看看正常不?

d:\tmp>adb devices
List of devices attached
e73c0da device

d:\tmp>adb -s e73c0da push test.txt /data/local/tmp
0 KB/s (12 bytes in 0.015s)

d:\tmp>adb -s e73c0da shell
shell@armani:/ $ cd /data/local/tmp
shell@armani:/data/local/tmp $ ls
MainTest
text.txt
shell@armani:/data/local/tmp $ chmod 700 ./MainTest
shell@armani:/data/local/tmp $ ls -l
-rwx------ shell    shell        9432 2016-05-30 21:48 MainTest
-rw-rw-rw- shell    shell          12 2016-05-21 16:08 test.txt
shell@armani:/data/local/tmp $ ./MainTest
3 = fn_test_add(1,2)
shell@armani:/data/local/tmp $

根据执行EXE的测试结果,可以看出EXE调用.a成功.

举报

相关文章推荐

how-to-install-ganglia-from-prebuild-rpm-on-centos6.6-x86_64

how-to-install-ganglia-from-prebuild-rpm-on-centos6.6-x86_64 写作缘由:之前对 ganglia 监控做过一些测试,部署使用简单;简单整理一下...

How to Use Android ADB Command Line Tool on mac

http://www.londatiga.net/it/how-to-use-android-adb-command-line-tool/ How to Use Android AD...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

How to use Google Test on Android-4.0.3_r1

AOSP has Google Test project in extarnal/gtest directory. I try to use Google Test in my Applicat...

use ffmpeg to setup streaming server on android

ffmpeg is a powerful media library. It provides ffserver tool that can be used to setup a streami...

Mac Error Create Android Project & HAX is not installed on this machine

Mac Error Create Android Project - “Errors running builder 'Android Resource Manager' on project” ...

android makefile prebuild

如果把source code放到Android下编译,可以参照其它Andorid.mk写对应的Makefile。   如果在编译Android之前已经有编译好的库或者配置文件时,如何加入到Andr...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)