Android Learning Note (44): Use of third-party Java libraries

Source: Internet
Author: User
Tags java se

Java has many third-party libraries. Android's Dalvik virtual device is not entirely Java, that is, the Android SDK and the traditional SDK is not exactly the same, if the Java library compatible with Android, it can be exploited. The use of Java libraries is limited by the following factors:

    • Target platform: Whether the Java code adaptation version is higher than the Android based Java version. Whether it uses Java SE APIs that are not supported by Android, such as swing, AWT graphics.
    • Size: Java code designed for desktop or server does not need to consider storage and memory space, Android needs, using third-party Java code, may be the application size is intolerable.
    • Performance: Whether Java code consumes the CPU that Android devices can provide.
    • Interface: Does the Java code need the console interface, or can wrap the simple API in our own interface.

One solution is to use open source Java code to make them more suitable for Android. For example, just use a third-party library of 10%, which can be recompiled to remove unnecessary classes.

Not all Java code can run on Android or Dalvik, Android does not provide some API on Java Se/me/ee, and the case-related swing and AWT. In addition, Java code can rely on other Java code, such as a jar that relies on an Apache httpcomponents that is different from Android integration, may cause compatibility issues. In these cases, there is no problem when we introduce a third-party jar package to compile, but there is an error in running the application. So using open source code is the best solution.

There are two ways to integrate third-party code, use source code, or use a pre-packaged jar. If you use source code, put it in our code tree and compile it together. The following describes how to use jars.

Join a third-party jar

If you use an out-of-the-box jar, place the jar in the libs/directory of your Android project, and in the command-line ant compilation, you will find the libs/jar document automatically. If you use IDE development, such as eclipse, you need to add the jar to the Bulid path. In the tree-like list on the left, click the item you want to load the jar in, right-->build path–>add External Archives ..., add the jar package.

In the example, using BeanShell's Bsh-core-2.0b4.jar,beanshell is a way to provide scripting, which can be scripted in the Java statement format. After downloading and installing, the jar document was successfully added to eclipse, as shown in the image on the right. BeanShell is very simple to use and will be built in the example.

Source

After adding the jar, there is no difference between the other and the usual practices, the code is as follows:

... ...
Import BSH.  interpreter; Can be automatically added via Ctrl + Shif + O

public class Chapter24test1 extends activity{
BeanShell Use Step 1: Create an instance of the BeanShell interpreter
Private Interpreter i = new interpreter ();

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.CHAPTER_24_TEST1);
Button button = (button) Findviewbyid (R.id.c24_eval);
Final EditText script = (EditText) Findviewbyid (r.id.c24_script);
Button.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
String src= Script.gettext (). toString ();
try{
BeanShell 2nd Step: Set the global script
I.set ("context", chapter24test1.this);
BeanShell the 3rd step: Set the script, optionally get the last state of the script.
I.eval (SRC);
}catch (BSH. Evalerror e) {
LOG.E ("WEI", "Error:" + e.tostring ());
}
}
});
}

}

There is a limit to the use of BeanShell on Android, not all scripting languages work properly, such as those scripts that have a unique format for JIT, and not all Java APIs are valid in Dalvik. No JIT footstep will be slower than compiling for Dalvik applications, slow-going user experience sluggish, and consume more battery energy, so unless it is very simple, the entire Android app is built in BeanShell. The BeanShell script can handle everything that is running in Android's underlying security mode, such as if the app has read_contacts permissions set, then any BeanShell scripts in the app have the same permissions. In addition, the BeanShell smallest core package also has 140K, and the application handles the matter, the volume is too large, consumes too much storage, the download time is longer.

RELATED links: My Andriod development related articles

Android Learning Note (44): Use of third-party Java libraries

Related Article

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.