gpt4 book ai didi

android - 使用 Android API 的 OSGI 包

转载 作者:太空狗 更新时间:2023-10-29 16:40:42 26 4
gpt4 key购买 nike

问题是:

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

到目前为止完成的工作:

1- 准备导出 android.util 的名为 android 的 Android 包。我通过以下方式做到了这一点: File->New->Project->Plugin From existing jar Archieves ->Next->Add External->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 Bundle,它将导入 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)已成功 dexified,但是当我尝试 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 bundle 时遇到的上述错误?

更新 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 仅用于编译。假设您已经成功编译了您的包,那么您只需将您的包(不是android.jar)安装到设备上。

更新

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com