如何向Android添加动态库?

Mal*_*olm 12 java android shared-libraries

我想向Android添加一个非本机共享库,以便设备上的每个应用程序都可以使用它.我的意思是使用打包类就像核心库一样,就像它们存在于应用程序本身中一样.

我研究了Android源代码,找出了一种向应用程序添加新路径的方法,ClassLoader并发现它是在启动时创建的,之后无法更改路径.我可以使用自己的ClassLoader,但加载一个类后我将得到的所有内容都将是Class对象的引用.这样我就会通过反射机制来工作,这比本机执行系统慢.

有没有办法在Android上组织共享库?

更新:这里要清楚,我不需要应用程序之间的交互.我需要可以在任何应用程序中重用的类.静态库也不能满足我的需求,因为这会使应用程序膨胀.

小智 10

在Android中创建动态加载库有两个技巧

  1. 使用sharedUserId在AndroidManifest为您的应用程序和库项目

  2. 使用dalvik.system.DexClassLoader加载库

图书馆代码:

它只包含没有任何Android特定入口点的java代码.AndroidManifest.xml只包含此android:sharedUserId属性

AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testlib"
    android:sharedUserId="com.example.testlib"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />

    <application android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme">

    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

TestCore.java

package com.example.testlib;

public class TestCore implements ITestCore{
    private int count = 0;
    public String testString(String arg) {
        String res = arg + " " + count;
        count++;
        return res;
    }
}
Run Code Online (Sandbox Code Playgroud)

示例应用程序代码

使用该库的应用程序.这里只是AndroidManifest.xml和TestApplication.java.所有其他申请人员照常.

AndroidManifest.xml中

小心在AndroidManifest.xml中使用相同的android:sharedUserId值作为库1

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testapp"
    android:sharedUserId="com.example.testlib"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".TestApplication" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="android.app.Activity" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
                <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
        </activity>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

ITestCore.java

必须在应用程序中声明库接口以避免使用反射

package com.example.testlib;

public interface ITestCore {
    String testString(String arg);
}
Run Code Online (Sandbox Code Playgroud)

TestApplication.java

在应用程序的onCreate处理程序中,确实有工作要做

package com.example.testapp;

import com.example.testlib.ITestCore;
import dalvik.system.DexClassLoader;
import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

public class TestApplication extends Application {
    ClassLoader libClassLoader;

    @Override
    public void onCreate() {
        PackageManager pm = getPackageManager();
        String libSrcPath = null;
        for (ApplicationInfo app : pm.getInstalledApplications(0)) {
            if (app.packageName.equals("com.rhomobile.testlibrary")) {
                libSrcPath = app.sourceDir;
                Log.d("TestApplication", ">>>>>>>>>>>>>>> package: " + app.packageName + ", sourceDir: " + app.sourceDir);
            }
        }
        try {
            libClassLoader = new DexClassLoader(libSrcPath, getDir("dex", 0).getAbsolutePath(), null, getClassLoader());
            Class<?> clazz = libClassLoader.loadClass("com.rhomobile.testlib.TestCore");            
            try {
                ITestCore core = (ITestCore)clazz.newInstance();
                String str = core.testString("TestApplication 1:");
                Log.i("TestApplication", ">>>>>>>>>>>>>>> output: " + str);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            Log.e("TestApplication", libClassLoader.toString());
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Mal*_*olm 3

Android 博客上最近的帖子准确地解释了我所需要的:

\n\n
\n

Dalvik VM 为开发人员提供了执行自定义类加载的工具。应用程序可以从其他位置(例如内部存储或通过网络)加载 Dalvik 可执行文件 (\xe2\x80\x9cdex\xe2\x80\x9d),而不是从默认位置加载它们。

\n
\n\n

关键是使用DexClassLoader来加载需要的库。之后,可以通过接口或使用反射来访问代码。

\n