Android so Lib library remote HTTP download and dynamic registration

Source: Internet
Author: User

I. BACKGROUND

In the development of Android application implementation, sometimes need to introduce third party so Lib library, but third party so cubby larger, such as open source third party play component FFmpeg library, if directly packaged APK package inside, The entire application will be much larger. After reviewing the data and experiments, it is possible to download so files by remote and then dynamically register so files. The main need to solve the download so file storage location and file read and write permissions issues.

Second, the main ideas

1, first put so on the network above, such as Test put to: http://codestudy.sinaapp.com/lib/test.so

2. When the application starts, the asynchronous thread is started to download so file and write to the/data/data/packagename/app_libs directory

3, call System.load Register so file. Because the path must have execute permission, we cannot load so on the SD card, but can write the so file to the application's private directory by calling Context.getdir ("Libs", Context.mode_private)/data/data/ Packagename/app_libs.

Third, the code implementation

1, network download so file, and write to the application's private directory/data/data/packagename/app_libs

/** * Download files to/data/data/packagename/app_libs below * @param context * @param URL * @param fileName * @return */public static File Downloadhttpfiletolib (context context, string URL, string fileName) {Long start = Sy        Stem.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 = is.read (buf)) > 0) {outstream.write (buf, 0, ch);             LOG.D (">>>httpdownloadfile:", "Download in progress ....");            }} outstream.flush ();            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 {outstream.close ();                } catch (IOException e) {e.printstacktrace ();                }} if (is = = null) {try {is.close ();                } catch (IOException e) {e.printstacktrace (); }            }        }    }

2. Call System.load to register so file

New Thread (New Runnable () {      @Override public      void Run () {          File sofile = Fileutils.downloadhttpfiletolib ( Getapplicationcontext (), "http://codestudy.sinaapp.com//lib/test.so", "test.so");          if (sofile! = null) {            try {                   log.d (">>>loadappfile Load Path:", Sofile.getabsolutepath ());                   System.load (Sofile.getabsolutepath ());             } catch (Exception e) {                 log.e (">>>loadappfile Load Error:", "So load failed:" + e.tostring ());             }           }      }}). Start ();

  

Iv. issues that need to be addressed

1, so file download and registration time. Test found libffmpeg.so 8M file single-threaded download needs 10-13s around

2, so download failure or registration failure how to deal with it. For example, if the so playback component tries to play with Android native MediaPlayer

3, when the initial so has not completed the successful registration, enter the playback page, need to be friendly prompt users, such as loading video is loading and so on

4. No network situation, etc.

V. Description

The above demo after 3 (2.3/4.2/4.4) actual model test can be used normally, and then according to the 4th list of questions to complete the following, you can use.

Android so Lib library remote HTTP download and dynamic registration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.