How to port native (C/C++) library into android.

Android official documents don’t have supports for writing native (C/C++) applications or libraries on android. But after all, android is a new platform, a lots of useful software are unavailable on android, if developing them from scratch using android Java framework, it will cost us much more time, further more, native functions should have better performance than pure Java applications. so, we will often think about porting existing native shared library to Android.

 

First, you have to pass build on android for your native library, write you own Android.mk including $(BUILD_SHARED_LIBRARY).

Second, using Java Api in Android Framework to write the UI for your application, call the native functions in your native library through JNI

 

At last you may encounter a problem about how to publish your software?.

 

All the native libraries come together with android are prelinked into the system.img, they will go to Android phone when the system.img is flashed into phone. This method is suitable for phone manufactures, but if you want to make a single .apk package to let users around the world to download and install, you have to think about put the native shared library into the .apk package. but how to use it in runtime? Here is a workaround method, pack your shared library into the apk as assets and copy the lib out of the package at the first launch time of your application. How to copy? Using AssetManager in Android Framework, see following sample code:

 

 

InputStream instream = getAssets().open( “native lib path in the apk package” );

 if(new File(“new lib path in phone”).exists() == false)

               copyFile(“new lib path in phone”,instream);       

  

If your native lib size is greater than UNCOMPRESS_DATA_MAX( = 1M currently), you will get exception when using instream to read file,  this time you couldn’t use AssetManager any more, instead you could use zip utilities in Java.util.zip, see following sample code:

           ZipFile zip = new ZipFile(APK_PATH);

           ZipEntry zipen = getFileZipEntry(zip,NATIVE_LIBRARY);

           copyFile(libPath, zip.getInputStream(zipen));

Another point, every Java application in android could only write on it’s own private folder, that is /data/data/packagename/, so the only place you could put your native library is here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将QT += core QT -= gui CONFIG += c++11 TARGET = UavRectifyLoadLIb CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS win32{ CONFIG(debug, debug|release){ DESTDIR = $$PWD/../../../../RasterManager/bin/Debug } else{ DESTDIR = $$PWD/../../../../RasterManager/bin/release } INCLUDEPATH += $$PWD/../../../include/gdal1101 DEPENDPATH += $$PWD/../../../include/gdal1101 } else{ CONFIG(debug, debug|release){ DESTDIR = $$PWD/../../../product/release32 } else{ DESTDIR = $$PWD/../../../product/release32 } } # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 unix:!macx: LIBS += -L$$PWD/../../../product/release32/ -lUAVAutoRectifyMt -lUAVAutoRectify -lUAVAutoRectifyFi INCLUDEPATH += $$PWD/../include DEPENDPATH += $$PWD/../include unix:!macx: LIBS += -L$$PWD/../../../lib/opencvf249/ -lopencv_core unix:!macx: LIBS += -L$$PWD/../../../lib/opencvf249/ -lopencv_highgui unix:!macx: LIBS += -L$$PWD/../../../lib/opencvf249/ -lopencv_imgproc INCLUDEPATH += $$PWD/../../../lib/opencvf249 DEPENDPATH += $$PWD/../../../lib/opencvf249 unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_core #unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_highgui unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_imgproc unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_features2d unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_imgcodecs INCLUDEPATH += $$PWD/../../../../../../../usr/local/include DEPENDPATH += $$PWD/../../../../../../../usr/local/include unix:!macx: LIBS += -L$$PWD/../../../product/release32/ -lDEMDriver unix:!macx: LIBS += -L$$PWD/../../../product/release32/ -lProjection unix:!macx: LIBS += -L$$PWD/../../../product/release32/ -lIImage_gC QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO QMAKE_LFLAGS_RELEASE = $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO 转为cmake格式
06-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值