Android load remote jar, APK

Source: Internet
Author: User

Reference Links:

1, http://blog.csdn.net/bboyfeiyu/article/details/11710497
2\ http://www.cnblogs.com/LittleRedPoint/p/3429709.html

First step: Writing interfaces

Package COM.LPL;

Public interface Iloader {

Public String SayHelloWorld ();
}

Step two: Implement the interface

Package COM.LPL;  Public class Remote implements iloader{    @Override    public  String sayhelloworld () {         return"sayhelloworld I am a jar";            }    }

Step Three: Package jar

Export the specified package or Java file using the export feature of Eclipse

Fourth step: Working with the jar file using the DX command

DX--dex--output=g:\remote_dex.jar G:\remote.jar

Note: The DX command may be in platform-tools or in a subdirectory in Build-tools

Fifth step: Copy Dex to the Android project under the package directory

1, directly into the sdcard may be reported abnormal, because the Android system to SDcard read and write access restrictions
2. Need to add read and write permissions in Androidmanifest
3. The jar needs to be put into the android/data/com.*** directory

Sixth step:

File File =NewFile (Environment.getexternalstoragedirectory (). toString () +file.separator+"Android/data/com.example.test/remote_dex.jar");Class Lib=NULL; Try {//Basedexclassloader cl = new Basedexclassloader (Environment.getexternalstoragedirectory (). ToString (), F Ile, File.getabsolutepath (), getClassLoader ());Dexclassloader cl =NewDexclassloader (File.getabsolutepath (), File.getpath (),NULL, getClassLoader ()); Lib= Cl.loadclass ("Com.lpl.Remote");Iloader Loader=(Iloader) lib.newinstance (); Toast.maketext (Getapplicationcontext (),""+loader.sayhelloworld (), the). Show (); } Catch(ClassNotFoundException e) {e.printstacktrace ();        }        Catch(instantiationexception e) {e.printstacktrace ();        } Catch(illegalaccessexception e) {E.printstacktrace (); }

Note here that the APK must also have an interface file, the remote jar is just the implementation of the interface file

Android load remote jar, APK

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.