是否可以动态地从一个Android应用程序在运行时加载一个库? [英] Is it possible to dynamically load a library at runtime from an Android application?

查看:174
本文介绍了是否可以动态地从一个Android应用程序在运行时加载一个库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法让一个Android应用程序下载和使用Java库在运行时?

Is there any way to make an Android application to download and use a Java library at runtime?

下面是一个例子:

试想一下,应用程序需要做一些计算,根据输入值。该应用程序会询问这些输入值,然后检查是否需要 CLASSE s或方法是可用的。

Imagine that the application needs to make some calculations depending on the input values. The application asks for these input values and then checks if the required Classes or Methods are available.

如果不,它连接到一台服务器,下载所需的库,并将其装入在运行时使用反射技术的呼叫所需的方法。实施可以根据varous条件改变,如谁正在下载该库的用户。

If not, it connects to a server, downloads the needed library, and loads it at runtime to calls the required methods using reflection techniques. The implementation could change depending on varous criteria such as the user who is downloading the library.

推荐答案

对不起,我来晚了,问题已经有一个公认的答案,而,您可以下载并执行外部库。这是我做的方式:

Sorry, I'm late and the question has already an accepted answer, but yes, you can download and execute external libraries. Here is the way I did:

我不知道这是否是可行的,所以我写了下面的类:

I was wondering whether this was feasible so I wrote the following class:

package org.shlublu.android.sandbox;

import android.util.Log;

public class MyClass {
    public MyClass() {
        Log.d(MyClass.class.getName(), "MyClass: constructor called.");
    }

    public void doSomething() {
        Log.d(MyClass.class.getName(), "MyClass: doSomething() called.");
    }
}

和我在我保存在我的设备的SD卡作为 /sdcard/shlublu.jar A DEX文件打包吧。

And I packaged it in a DEX file that I saved on my device's SD card as /sdcard/shlublu.jar.

然后我写了下面的愚蠢的计划,已经消除后 MyClass的从我的Eclipse项目,并清理了:

Then I wrote the "stupid program" below, after having removed MyClass from my Eclipse project and cleaned it:

public class Main extends Activity {

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            final String libPath = Environment.getExternalStorageDirectory() + "/shlublu.jar";
            final File tmpDir = getDir("dex", 0);

            final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
            final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("org.shlublu.android.sandbox.MyClass");

            final Object myInstance  = classToLoad.newInstance();
            final Method doSomething = classToLoad.getMethod("doSomething");

            doSomething.invoke(myInstance);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

基本上,它加载类 MyClass的这种方式:

  • 创建一个<一个href="http://developer.android.com/reference/dalvik/system/DexClassLoader.html"><$c$c>DexClassLoader

用它来提取类 MyClass的/ SD卡/ shlublu.jar

和存储此类应用程序的地塞米松私有目录(在手机的内置存储)。

and store this class to the application's "dex" private directory (internal storage of the phone).

然后,它创建 MyClass的的调用实例和 DoSomething的()上创建的实例。

Then, it creates an instance of MyClass and invokes doSomething() on the created instance.

和它的工作原理......我看到 MyClass的定义的痕迹在我的LogCat中:

And it works... I see the traces defined in MyClass in my LogCat:

我已经试过两个仿真器2.1和我的身体的HTC手机(这是运行Android 2.2和它的根源并非是)。

I've tried on both an emulator 2.1 and on my physical HTC cellphone (which is running Android 2.2 and which is NOT rooted).

这意味着你可以为应用程序下载和执行他们创造外部DEX文件。在这里,它是用硬办法(难看的对象蒙上, Method.invoke()丑陋的电话...)但它必须能够与接口 s至使一些清洁剂。

This means you can create external DEX files for the application to download and execute them. Here it was made the hard way (ugly Object casts, Method.invoke() ugly calls...), but it must be possible to play with Interfaces to make something cleaner.

哇。我是第一个惊讶。我期待一个 SecurityException异常

Wow. I'm the first surprised. I was expecting a SecurityException.

一些事实,以帮助研究更多:

Some facts to help investigating more:

  • 在我的DEX shlublu.jar签了字,但不是我的应用程序
  • 在我的应用程序从Eclipse中/ USB连接执行。因此,这是在调试模式下编译一个无符号的APK

这篇关于是否可以动态地从一个Android应用程序在运行时加载一个库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆