Ubuntu16.04 下 构建 Android(NDK) 应用

NDK Build

Generate .so or .a files using NDK tools:

  1. Edit .java file

  2. Generate header file for the java file using JDK tools javac and javah

    # 在 `src` 目录下
    javac com/ndk/test/OpenCVTest.java
    javah -classpath ./ -jni com.ndk.test.OpenCVTest
    
  3. Edit Android.mk and Application.mk files

  4. Generate .so or .a files using command ndk-build

  5. Load the library and using its methods

NDK Tips

Android NDK 从2013年开始支持了C++11,从2015年开始支持C++14,在 Android.mk 中加入

# c++ 11 标准
LOCAL_CPPFLAGS += -std=c++11
LOCAL_CPPFLAGS += -D__cplusplus=201103L  

# or

# c++ 14 标准
LOCAL_CPPFLAGS += -std=c++1y
LOCAL_CPPFLAGS += -D__cplusplus=201300L

When compiling c++ code with -std=c++11 and using gnustl_shared, many C99 math functions are not provided by the header as they should. At this time,APP_STL := c++_static may help.(from Issue: C++11 cmath functions not in std namespace)

Build Android Project

Build Ant Project

  • generate build.xml for Ant

    android update project -p .
    
  • build ant project

    ant debug/release
    

Build Eclipse Project

Sign APK

  1. Generate keystore file

    keytool -genkey -alias ChenguangCam -keyalg RSA -validity 100000 -keystore AndroidCameraApp.keystore
    
  2. Sign Apk

  • 使用第三方工具:爱加密签名工具
  • 使用命令行
    jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore  
    <path_to_AndroidCameraApp.keystore> -storepass 123456 -keypass 123456 -signedjar <path_to_signed.apk> <path_to_unsign.apk> ChenguangCam
    
  • Eclipse Project: 右键单击项目名称,选择"Android Tools",再选择"Export Signed Application Package…";
  • Ant Project: add key.store and key.alias properties to ant.properties file;

Install APK

  • Install apk file to devices

    adb install <path_to_apk>
    # or
    adb install -r <path_to_apk>
    
  • uninstall apk

    adb uninstall <package-name>
    

Debug

Native C++ Crash Debug

当NDK生成的 .so 运行崩掉时,通过NDK工具查找相关信息。

ndk-stack
1) 实时分析日志

程序运行过程中,执行如下命令:

adb logcat | ndk-stack -sym <so文件所在路径>

当程序崩溃时,可输出崩溃信息。

2) 获取日志再分析

程序运行过程中,执行如下命令:

adb logcat > 1.log

程序崩溃后,通过如下命令获取崩溃信息:

ndk-stack -sym <so文件所在路径> -dump 1.log

或 通过关键字查找

  • NDK logcat crash keywords

    --------- beginning of crash

    backtrace

arm-linux-androideabi-addr2line

获取日志中关键函数指针,例如

#00 pc 00031896 /data/app/com.tools/lib/arm/libtools.so

根据地址,使用如下命令找到对应的函数:

arm-linux-androideabi-addr2line -e libtools.so 00031896 -f

参考:如何定位Android NDK开发中遇到的错误

Check File Dependencies

arm-linux-androideabi-readelf
  • check .so file
    arm-linux-androideabi-readelf -d *.so
    

应用认领

应用认领那些事:
http://droidyue.com/blog/2014/12/14/android-yingyong-renling/?utm_source=tuicool&utm_medium=referral

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晨光ABC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值