Android so lib Library Remote http download and dynamic registration



In the development of Android applications, sometimes it is necessary to introduce third-party so lib libraries, but third-party so libraries are larger, such as open source third-party playback component ffmpeg libraries. If the apk package is packaged directly, the whole application will be much larger. After consulting data and experiments, it is found that it is feasible to download so files remotely and register so files dynamically. It is necessary to solve the storage location of downloaded so files and the permission of reading and writing files.


II. Main Ideas


1. First, put so on the network, such as testing:

2. When the application starts up, start the asynchronous thread to download so file and write it to / data/data/packageName/app_libs directory

3. Call System.load to register so file. Because the path must have execution privileges, we cannot load so on the SD card, but we can write so files to the private directory of the application / data/data/packageName/app_libs by calling context.getDir("libs", Context.MODE_PRIVATE).


3. Code Implementation


1. Download so files on the network and write them to the private directory of the application/data/data/PackageName/app_libs

     * Download the file to / data/data/PackageName/app_libs
     * @param context
     * @param url
     * @param fileName
     * @return
    public static File downloadHttpFileToLib(Context context, String url, String fileName) {
        long start = System.currentTimeMillis();
        FileOutputStream outStream = null;
        InputStream is = null;
        File soFile = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            File dir = context.getDir("libs", Context.MODE_PRIVATE);
            soFile = new File(dir, fileName);
            outStream = new FileOutputStream(soFile);
            is = entity.getContent();
            if (is != null) {
                byte[] buf = new byte[1024];
                int ch = -1;
                while ((ch = > 0) {
                    outStream.write(buf, 0, ch);
                    //Log.d (">> http Download File:", "Download in progress...");
            long end = System.currentTimeMillis();
            Log.d(">>>httpDownloadFile cost time:",  (end-start)/1000 + "s");
            Log.d(">>>httpDownloadFile:", "download success");
            return soFile;
        } catch (IOException e) {
            Log.d(">>>httpDownloadFile:", "download failed" + e.toString());
            return null;
        } finally {
            if (outStream != null) {
                try {
                } catch (IOException e) {
            if (is != null) {
                try {
                } catch (IOException e) {


2. Call System.load to register so file

new Thread(new Runnable() {
      public void run() {
          File soFile = FileUtils.downloadHttpFileToLib(getApplicationContext(), "", "");
          if (soFile != null) {
            try {
                   Log.d(">>>loadAppFile load path:", soFile.getAbsolutePath());
             } catch (Exception e) {
                 Log.e(">>>loadAppFile load error:", "so load failed:" + e.toString());




1. Download of so files and timing of registration. The test found that the single-thread download of 8M files required about 10-13s.

2. How to deal with the failure of so download or registration? For example, do so playback components attempt to use android native MediaPlayer for playback?

3. When so has not been downloaded and registered successfully, when entering the playback page, users need friendly prompts, such as loading video is loading, and so on.

4. No network, etc.


V. Explanation


The demo above can be used normally after 3 (2.3/4.2/4.4) actual model tests, and then according to the fourth point to enumerate the problems below, it can be used.


Reprinted at:

Tags: network Android MediaPlayer

Posted on Wed, 26 Jun 2019 16:59:50 -0700 by feri_soft