android - 使用 Android API 的 OSGI bundle

标签 android osgi bundle logcat apache-felix

问题是这样的:

在 eclipse 中,创建一个使用 Android API 的 OSGI 包,以显示日志消息。然后,在嵌入 Felix 框架的 Android 应用程序中安装并启动 OSGI 包。

目前已完成的工作:

1- 准备名为 android 的 Android 包,它导出 android.util。我通过以下方式做到了这一点:文件->新建->项目->插件从现有的jar文件->下一步->添加外部->android.jar 这导致了一个具有 android API 的 OSGI 包。

list .MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Android
Bundle-SymbolicName: android
Bundle-Version: 1.0.0
Bundle-ClassPath: .
Export-Package: android,
 android.util,
 ............etc
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

2- 创建名为 AndroidAPI_Bundle 的 OSGI 包,它将导入 android.util 并显示日志消息。

激活器类:

package androidapi_bundle;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import android.util.Log;


public class Activator implements BundleActivator {

    private static BundleContext context;

    static BundleContext getContext() {
        return context;
    }


    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;

        System.out.println("This is a java message inside the start method of AndroidAPI_Bundle");
        Log.d("Zitona Log","This is android message inside the start method of AndroidAPI_Bundle");
    }

    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;

        Log.d("Zitona Log","AndroidAPI_Bundle stopped!");
    }

}

list .MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: AndroidAPI_Bundle
Bundle-SymbolicName: AndroidAPI_Bundle
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: androidapi_bundle.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: android.util,
 org.osgi.framework;version="1.3.0"

现在是时候对这两个包进行 dexify 以便安装它们并在 Felix 中使用它们了。

我用的是这个:

dx --dex --output=classes.dex JAR_file.jar

aapt add JAR_file.jar classes.dex

bundle AndroidAPI_Bundle(使用Android API)已成功dexify,但是当我尝试dexify android bundle 时,出现此错误:

trouble processing "java/awt/font/NumericShaper.class":

Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.

This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.

However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.

If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.

1 error; aborting

第一:我的方向正确吗?这是我应该如何解决问题吗? 第二:如何解决在 dexifying android 包时出现的错误?

更新1:

我从 android.jar 中删除了一些软件包,这些软件包阻止了dexification,并且我当前不需要这些软件包(这是一个临时解决方案),通过这样做,我能够dexify android.jar.

接下来,我在 Apache Felix 上安装并启动了 android.jarAndroidAPI_Bundle.jarandroid.jar 安装正常。但是,在安装从 android.jar 导入 android.utilAndroidAPI_Bundle.jar 时,我收到了以下错误:

08-18 14:42:05.470: W/System.err(1318): org.osgi.framework.BundleException: Activator start error in bundle AndroidAPI_Bundle [2].
08-18 14:42:05.478: W/System.err(1318):     at org.apache.felix.framework.Felix.activateBundle(Felix.java:2196)
08-18 14:42:05.478: W/System.err(1318):     at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
08-18 14:42:05.478: W/System.err(1318):     at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
08-18 14:42:05.478: W/System.err(1318):     at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:942)
08-18 14:42:05.478: W/System.err(1318):     at com.example.apache_felix_android.MainActivity.onCreate(MainActivity.java:93)
08-18 14:42:05.478: W/System.err(1318):     at android.app.Activity.performCreate(Activity.java:5104)
08-18 14:42:05.478: W/System.err(1318):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-18 14:42:05.508: W/System.err(1318):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-18 14:42:05.508: W/System.err(1318):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-18 14:42:05.508: W/System.err(1318):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-18 14:42:05.508: W/System.err(1318):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-18 14:42:05.508: W/System.err(1318):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 14:42:05.508: W/System.err(1318):     at android.os.Looper.loop(Looper.java:137)
08-18 14:42:05.518: W/System.err(1318):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-18 14:42:05.518: W/System.err(1318):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 14:42:05.528: W/System.err(1318):     at java.lang.reflect.Method.invoke(Method.java:511)
08-18 14:42:05.528: W/System.err(1318):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-18 14:42:05.538: W/System.err(1318):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-18 14:42:05.538: W/System.err(1318):     at dalvik.system.NativeStart.main(Native Method)
08-18 14:42:05.548: W/System.err(1318): Caused by: java.lang.RuntimeException: Stub!
08-18 14:42:05.558: W/System.err(1318):     at android.util.Log.d(Log.java:7)
08-18 14:42:05.558: W/System.err(1318):     at androidapi_bundle.Activator.start(Activator.java:24)
08-18 14:42:05.578: W/System.err(1318):     at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
08-18 14:42:05.588: W/System.err(1318):     at org.apache.felix.framework.Felix.activateBundle(Felix.java:2146)
08-18 14:42:05.588: W/System.err(1318):     ... 18 more

我从错误中了解到 android.util.Log 只有“ stub ”,而不是方法的真正实现。我该如何解决这个问题?!

更新2:

@Neil 在下面的答案中建议,一旦编译 AndroidAPI_Bundle.jar,就不需要 bundle android.jar。所以我只将 AndroidAPI_Bundle.jar 安装到 Felix 中。但这不起作用。我尝试了几次。我确实知道平台已经有 Android API,所以不需要 android.jar,但这只是理论,实际上,这对我不起作用。以下是我收到的错误:

08-18 18:58:44.307: W/System.err(7898): org.osgi.framework.BundleException: Unresolved constraint in bundle AndroidAPI_Bundle [1]: Unable to resolve 1.0: missing requirement [1.0] osgi.wiring.package; (osgi.wiring.package=android.util)
08-18 18:58:44.317: W/System.err(7898):     at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974)
08-18 18:58:44.317: W/System.err(7898):     at org.apache.felix.framework.Felix.startBundle(Felix.java:2037)
08-18 18:58:44.317: W/System.err(7898):     at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
08-18 18:58:44.327: W/System.err(7898):     at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:942)
08-18 18:58:44.327: W/System.err(7898):     at com.example.apache_felix_android.MainActivity.onCreate(MainActivity.java:93)
08-18 18:58:44.327: D/dalvikvm(7898): GC_CONCURRENT freed 387K, 19% free 2774K/3388K, paused 22ms+5ms, total 131ms
08-18 18:58:44.337: W/System.err(7898):     at android.app.Activity.performCreate(Activity.java:5104)
08-18 18:58:44.337: W/System.err(7898):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-18 18:58:44.337: W/System.err(7898):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-18 18:58:44.337: W/System.err(7898):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-18 18:58:44.337: W/System.err(7898):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-18 18:58:44.337: W/System.err(7898):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-18 18:58:44.347: W/System.err(7898):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 18:58:44.347: W/System.err(7898):     at android.os.Looper.loop(Looper.java:137)
08-18 18:58:44.347: W/System.err(7898):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-18 18:58:44.347: W/System.err(7898):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 18:58:44.347: W/System.err(7898):     at java.lang.reflect.Method.invoke(Method.java:511)
08-18 18:58:44.347: W/System.err(7898):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-18 18:58:44.357: W/System.err(7898):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-18 18:58:44.357: W/System.err(7898):     at dalvik.system.NativeStart.main(Native Method)

阅读错误,它清楚地表明 android.util 包丢失,因为该包导入了它。它必须在那里。

我什至删除了 AndroidAPI_Bundle 的 MANIFEST.MF 中的所有导入,而不是导入,而是将 android.jar 添加到了我的额外依赖项部分。它编译得很好,但是当我单独运行该包时(不在 android 应用程序上),它显示 java.lang.NoClassDefFoundError: android/util/Log ,并且当我将它安装在我的 android 应用程序中的 Felix 中时,它显示:

08-18 19:13:22.666: I/dalvikvm(12325): Could not find method android.util.Log.d, referenced from method androidapi_bundle.Activator.start

我的要求很简单:创建使用 Android API 的 bundle 的正确步骤是什么,然后将该 bundle 成功安装到 Android 应用程序中。但我在网上没有看到任何实现。如果有人有任何想法,请帮忙。

最佳答案

您似乎一直在问同样的问题,只是略有不同。答案与之前相同:您不需要这样做

首先,android.jar 仅包含 stub 方法,因此即使您设法将其dexify,它实际上也不会工作。其次,您不需要将此 jar 加载到设备上,因为 API 已经存在于设备中。

JAR 仅用于编译。假设您已经成功编译了 bundle ,那么您只需将 bundle (而不是 android.jar)安装到设备上即可。

更新

为了导入包android.util,有人需要导出该包。该包可从基本​​运行时获取——即 JRE 启动类路径的 Android 等效项。这些包通常由系统 bundle 导出,即代表 OSGi 框架本身的特殊 bundle 。当然,OSGi 对 Android 一无所知!它仅导出由标准 Java 定义的包。因此,如果您需要访问基本运行时中的其他包,只需通过设置 org.osgi.framework.system.packages.extra 属性将它们添加到系统 bundle 导出中即可。此属性是使用启动 OSGi 时提供给 FrameworkFactory.newFramework 的配置映射来传递的。

关于android - 使用 Android API 的 OSGI bundle ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299057/

相关文章:

android - ListView 上的 SwipeListener 和 ListView 项目上的 ClickListener

java - OSGI缓存目录

macos - 从 Mac 应用程序返回代码

magento - 获取 bundle 产品的单个选项 ID

java - 使用 appium 捕获 Android 屏幕截图

java - 从 Web 服务编辑 Android 中的文本字段验证

android - 如何在 RecyclerView 或 ListView 项目 View 中使用 layout_weight 让等高的项目填满屏幕上的可用空间?

java - 我无法使用 aries jpa/v2.0.0 和 transactions/v2.0.0

java - Eclipse 扩展和声明式服务

IOS:复制文档文件夹中的文件