使用Android API的OSGI包

Tra*_*man 2 android osgi bundle logcat apache-felix

这是问题所在:

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

迄今为止完成的工作:

1-准备Android捆绑包android,其中包含哪个导出android.util.我通过以下方式做到了这一点:File-> New-> Project-> Plugin来自现有的jar Archieves - > Next-> Add External-> android.jar,这导致了一个带有android API的OSGI包.

MANIFEST.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
Run Code Online (Sandbox Code Playgroud)

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!");
    }

}
Run Code Online (Sandbox Code Playgroud)

MANIFEST.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"
Run Code Online (Sandbox Code Playgroud)

现在是时候对两个软件包进行dexify以便安装它们并在Felix中使用它们.

我用过这个:

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

aapt add JAR_file.jar classes.dex
Run Code Online (Sandbox Code Playgroud)

捆绑包AndroidAPI_Bundle(使用Android API)已成功dexified,但是当我尝试dexify androidbundle时,我收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

第一:我正朝着正确的方向前进吗?这是我应该如何解决问题的?第二:如何解决dexifying androidbundle 时我得到的错误?

更新1:

我删除了一些android.jar防止dexification的包,我目前不需要(这是一个临时解决方案),通过这样做,我能够dexify android.jar.

接下来,我安装和启动都android.jarAndroidAPI_Bundle.jar在Apache菲利克斯.android.jar安装好了.但是,在安装AndroidAPI_Bundle.jar导入android.utilandroid.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
Run Code Online (Sandbox Code Playgroud)

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

更新2:

@Neil在下面的答案中建议,android.jar编译后不需要捆绑包AndroidAPI_Bundle.jar.所以我只安装AndroidAPI_Bundle.jar了Felix.但这不起作用.我试了好几次.我确实理解平台alreayd有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)
Run Code Online (Sandbox Code Playgroud)

阅读错误,显然是因为这个包导入了它,所以缺少android.util包.它必须在那里.

我甚至删除了MANIFEST.MF中的任何导入AndroidAPI_Bundle,而不是导入,我将android.jar添加到我的extra-dependencies部分.它编译得很好,但是当我单独运行捆绑包(不在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
Run Code Online (Sandbox Code Playgroud)

我的要求很简单:创建使用Android API的捆绑包的正确程序是什么,然后将该捆绑包成功安装到Android应用程序中.但我在网上看不到它的任何实现.如果有人有任何想法,请帮助.

Nei*_*ett 5

好像你一直在问同样的问题而只是略有不同.答案和以前一样:你不需要这样做.

首先,android.jar只包含存根方法,所以即使你设法对它进行dexify它也不会真正起作用.其次,你没有需要得到这个罐子到设备上,因为这些API已经存在那里.

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

UPDATE

为了导入包android.util,有人需要导出该包.该程序包可从基本​​运行时获得 - 即与JRE引导类路径的Android等效项.这些包通常由系统包导出,即代表OSGi Framework本身的特殊包.除了当然OSGi对Android没有任何了解!它只导出标准 Java 定义的包.因此,如果您需要访问基本运行时的其他包,则只需通过设置org.osgi.framework.system.packages.extra属性将它们添加到系统包导出中.使用您FrameworkFactory.newFramework在启动OSGi时提供的配置映射传递此属性.