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.


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wzshuoshuo/archive/2008/12/29/3636657.aspx

更多相关文章

  1. Android TestView文本文字修改实例
  2. Android 系统中 gps Location Service 的实现与架构,本文可以帮助
  3. Android 点击按钮,文本文字改变
  4. 分析脚本文件AndroidInitProcess分析心得(1)
  5. Android通知栏消息(基本文字通知)
  6. Android判断网络状态是否断开+Android完全关闭应用程序+ 本文讲
  7. Android下载文本文件和mp3文件
  8. Android-TextView多行本文滚动轻松实现
  9. 解析PHP的基本文本输出

随机推荐

  1. android 按百分比设置布局
  2. Android系统启动流程(nougat7.1.1_r6)
  3. Android进程调度
  4. android编写Service入门用法与教程 (Local
  5. android ratingbar星星大小设定
  6. android 远程调用.NET WCF服务
  7. 【Android Dev Guide - 03】 - Content P
  8. 解开Android应用程序组件Activity的"sing
  9. Binder机制揭秘
  10. Android泡泡聊天界面的实现