这是问题所在:
在 Eclipse 中,创建一个使用 Android API 的 OSGI 包,以显示日志消息。然后,在嵌入了 Felix 框架的 Android 应用程序中安装并启动 OSGI 包。
迄今为止完成的工作:
1-准备名为android
which exports的 Android 包android.util
。我通过以下方式做到了这一点: File->New->Project->Plugin From existing jar Archieves ->Next->Add External->android.jar 这导致了一个具有 android API 的 OSGI 包。
清单.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- 创建调用的 OSGI Bundle AndroidAPI_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!");
}
}
清单.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
捆绑包AndroidAPI_Bundle
(使用 Android API)已成功 dexified,但是当我尝试 dexifyandroid
捆绑包时,我收到此错误:
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
。
接下来,我安装并启动了android.jar
Apache AndroidAPI_Bundle.jar
Felix。android.jar
安装OK。但是,在安装AndroidAPI_Bundle.jar
which imports android.util
fromandroid.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 只有“存根”,而不是方法的真正实现。我该如何解决这个问题?!
更新 2:
@Neil 在下面的答案中建议android.jar
,一旦你 compile 就不需要捆绑包AndroidAPI_Bundle.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包丢失了,因为这个包导入了它。它必须在那里。
我什至删除了 MANIFEST.MF 中的任何导入AndroidAPI_Bundle
,而不是导入,我将 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 的包的正确程序是什么,然后将该包成功安装到 Android 应用程序中。但我只是在网上看不到它的任何实现。如果有人有任何想法,请帮助。