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(安卓)- View Alpha值
  2. 编译android 之后生成的 img 文件介绍
  3. android HAL 详解
  4. Android入门之Style与Theme
  5. Android入门第十六篇之Style与Theme
  6. Android开发周报:Android将使用OpenJDK、React Native详解
  7. Android入门第十六篇之Style与Theme[转]
  8. Android(安卓)Studio 使用技巧(2)
  9. Android(安卓)Studio 使用技巧(1)

随机推荐

  1. Android(安卓)菜单和对话框等
  2. Android(安卓)SimpleArrayMap源码解析
  3. Android(安卓)在低版本sdk中没有getSuppo
  4. 《老罗Android》监听电量变化、Broadcast
  5. Android(安卓)架构组件之 Lifecycle 使用
  6. Java Nested class
  7. [Android][AlarmManager]
  8. AndroidStudio中使用Kotlin--实现接口方
  9. Android(安卓)Studio 入门:(五) 悬浮按钮增
  10. 知识网站