本文介绍了在APK包采用Android API为:"找不到类的android.content.Context“,从法com.example.patient.Activator.start&QUOT参考;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我嵌入OSGi框架为Android应用程序。除了我的Andr​​oid应用程序,我有我创建使用以下步骤的APK包:

I am embedding OSGI framework into Android application. Beside my android application, I have an APK bundle that I create using the following steps:


  • 将创建Eclipse的Andr​​oid创建一个普通APK,例如
    项目。

  • 请您使用捆绑所用的相同的OSGI框架库您
    应用程序通过:建立Path->配置构建Path->项目,然后
    添加应用程序项目,将加载包。你的
    应用程序项目应该包含在OSGi框架jar文件的
    建设(在我的情况 felix.jar )的路径。

  • 创建描述捆绑包清单文件。您可以致电
    bundle.manifest

  • 假设你的应用程序包 com.acme.helloworld (此值
    的Andr​​oidManifest.xml ),你的OSGi包的:与设置清单
    Activator类必须被放置在包 com.acme.helloworld
    您必须设置捆绑-SymbolicName: com.acme.helloworld 在包
    表现。如果任何这些条件不那么满足将导致
    java.lang.NoClassDefFoundError的上运行。

  • 使用Android的工具>导出未签名的Andr​​oid包

  • 复制bundle.manifest到生成的无符号的根目录
    APK为 META-INF / MANIFEST.MF 。您可以使用WinZip打开符号
    APK并添加文件夹 META-INF

  • 登入的APK使用命令:的jarsigner -verbose -keystore
    /path_to_keystore/mykeystore.keystore my_application.apk my_keystore_alias

  • 复制您的目录中包含所有的.class文件
    Android项目到您的apk签署的根目录。在我的情况:
    它是 COM 目录。

  • 再次签署APK。

  • 安装APK包。

  • 有OSGi框架加载和启动APK包(非常相同
    APK文件)

  • Create a regular APK, for example by creating Eclipse AndroidProject.
  • Make your bundle use the same OSGI framework library used by yourapplication by: Build Path->Configure Build Path->Projects, and thenadd your application project that will load the bundle. Yourapplication project should contain the OSGI framework jar file in itsbuild path, (in my case felix.jar).
  • Create the bundle manifest file describing the bundle. You can callit bundle.manifest.
  • Say your application package is com.acme.helloworld (this value isset with manifest:package in AndroidManifest.xml), your OSGI bundle'sActivator class MUST be placed in the package com.acme.helloworld andyou MUST set Bundle-SymbolicName: com.acme.helloworld in the bundlemanifest. If any of these conditions is not met then will result in ajava.lang.NoClassDefFoundError on runtime.
  • Use Android Tools > Export Unsigned Android Package
  • Copy bundle.manifest to the root directory of the generated unsignedAPK as META-INF/MANIFEST.MF. You can use Winzip to open the unsignedAPK and add the folder META-INF.
  • Sign the APK using the command: jarsigner -verbose -keystore/path_to_keystore/mykeystore.keystore my_application.apk my_keystore_alias.
  • Copy your directory that contains all the .class files from yourandroid project to the root directory of your signed apk. In my case:it is the com directory.
  • Sign your APK once again.
  • Install the APK bundle.
  • Have the OSGi framework load and start the APK bundle (the very sameAPK file)

使用这些步骤后,我成功加载了APK包费利克斯框架,并开始确定。

Using these steps, I successfully loaded the APK bundle to Felix framework, and It starts OK.

不过,我现在正试图推出一个存在于APK包的活动,所以我做了以下内容:

However, I am now trying to launch an activity that exists in the APK bundle, so I did the following:

在我的应用程序项目,我注册了应用程序上下文作为一种服务,使其可用于捆绑APK:

In my Application project, I register the application context as a service to make it available for the APK bundle:

 // Register the application's context as an OSGi service!
    BundleContext bundleContext = m_felix.getBundleContext();
    ServiceRegistration regContext = bundleContext.registerService(Context.class,
      getApplicationContext(), new Hashtable());

在我的APK包开始在激活()方法:

In my APK bundle start method() in the Activator class:

ServiceReference<Context> ref = bundleContext.getServiceReference(Context.class);
final Context ctx = bundleContext.getService(ref);

Intent intent = new Intent();
String pkgName = View_Patient_File_Activity.class.getPackage().getName();
String clssName = View_Patient_File_Activity.class.getName();
intent.setClassName(pkgName, clssName);

// You may add the NEW_TASK flag
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// Important: do not use startActivity(Context, Class) version because it will fail to resolve the activity
ctx.startActivity(intent);

当我运行我的应用程序的项目,包不安装,因为以下错误:

When I run my application project, the bundle is not installed because of the following errors:

11-26 20:07:06.600: W/dalvikvm(24646): VFY: unable to find class referenced in signature (Lorg/osgi/framework/BundleContext;)
11-26 20:07:06.600: W/dalvikvm(24646): VFY: unable to find class referenced in signature (Lorg/osgi/framework/BundleContext;)
11-26 20:07:06.600: E/dalvikvm(24646): Could not find class 'android.content.Context', referenced from method com.example.patient.Activator.start
11-26 20:07:06.600: W/dalvikvm(24646): VFY: unable to resolve const-class 25 (Landroid/content/Context;) in Lcom/example/patient/Activator;
11-26 20:07:06.600: D/dalvikvm(24646): VFY: replacing opcode 0x1c at 0x0009
11-26 20:07:06.600: W/dalvikvm(24646): VFY: unable to find class referenced in signature (Lorg/osgi/framework/BundleContext;)
11-26 20:07:06.600: W/dalvikvm(24646): Method mismatch: start in Lcom/example/patient/Activator; (cl=0x4072f3c0) and iface Lorg/osgi/framework/BundleActivator; (cl=0x40712e88)
11-26 20:07:06.600: W/dalvikvm(24646): Class init failed in newInstance call (Lcom/example/patient/Activator;)
11-26 20:07:06.600: E/Zaid Log(24646): Problem installing the bundle :s
11-26 20:07:06.600: W/System.err(24646): org.osgi.framework.BundleException: Activator start error in bundle com.example.patient [1].
11-26 20:07:06.600: W/System.err(24646):    at org.apache.felix.framework.Felix.activateBundle(Felix.java:2196)
11-26 20:07:06.600: W/System.err(24646):    at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
11-26 20:07:06.600: W/System.err(24646):    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
11-26 20:07:06.600: W/System.err(24646):    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:942)
11-26 20:07:06.600: W/System.err(24646):    at com.example.patient_application.MainActivity.onCreate(MainActivity.java:149)
11-26 20:07:06.600: W/System.err(24646):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
11-26 20:07:06.600: W/System.err(24646):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
11-26 20:07:06.600: W/System.err(24646):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
11-26 20:07:06.600: W/System.err(24646):    at android.app.ActivityThread.access$1500(ActivityThread.java:122)
11-26 20:07:06.600: W/System.err(24646):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
11-26 20:07:06.600: W/System.err(24646):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 20:07:06.600: W/System.err(24646):    at android.os.Looper.loop(Looper.java:132)
11-26 20:07:06.600: W/System.err(24646):    at android.app.ActivityThread.main(ActivityThread.java:4028)
11-26 20:07:06.600: W/System.err(24646):    at java.lang.reflect.Method.invokeNative(Native Method)
11-26 20:07:06.600: W/System.err(24646):    at java.lang.reflect.Method.invoke(Method.java:491)
11-26 20:07:06.600: W/System.err(24646):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
11-26 20:07:06.600: W/System.err(24646):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-26 20:07:06.600: W/System.err(24646):    at dalvik.system.NativeStart.main(Native Method)
11-26 20:07:06.600: W/System.err(24646): Caused by: java.lang.LinkageError: Classes resolve differently in interface
11-26 20:07:06.600: W/System.err(24646):    at java.lang.Class.newInstanceImpl(Native Method)
11-26 20:07:06.600: W/System.err(24646):    at java.lang.Class.newInstance(Class.java:1301)
11-26 20:07:06.600: W/System.err(24646):    at org.apache.felix.framework.Felix.createBundleActivator(Felix.java:4336)
11-26 20:07:06.600: W/System.err(24646):    at org.apache.felix.framework.Felix.activateBundle(Felix.java:2141)
11-26 20:07:06.600: W/System.err(24646):    ... 17 more
11-26 20:07:06.610: V/TLINE(24646): new: android.text.TextLine@40782610
11-26 20:07:06.700: D/CLIPBOARD(24646): Hide Clipboard dialog at Starting input: finished by someone else... !

我出口的Andr​​oid包在我的应用程序项目是这样的:

I exported the android packages in my application project like this:

static final String ANDROID_FRAMEWORK_PACKAGES = (
            "android,"
            + "android.app,"
            + "android.content,"
            + "android.database,"
            + "android.database.sqlite,"
            + "android.graphics,"
            + "android.graphics.drawable,"
            + "android.graphics.glutils,"
            + "android.hardware,"
            + "android.location,"
            + "android.media,"
            + "android.net,"
            + "android.net.wifi,"
            + "android.opengl,"
            + "android.os,"
            + "android.provider,"
            + "android.sax,"
            + "android.speech.recognition,"
            + "android.telephony,"
            + "android.telephony.gsm,"
            + "android.text,"
            + "android.text.method,"
            + "android.text.style,"
            + "android.text.util,"
            + "android.util,"
            + "android.view,"
            + "android.view.animation,"
            + "android.webkit,"
            + "android.widget");


         m_configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
                ANDROID_FRAMEWORK_PACKAGES);

和我在APK包导入这些包。下面是我的 MANIFEST.MF

And I import these packages in my APK bundle. Below is my MANIFEST.MF

Manifest-Version: 1.0
Bundle-Vendor: Zaid
Bundle-Version: 1.0.0
Bundle-Name: Patient
Bundle-ManifestVersion: 2
Bundle-Activator: com.example.patient.Activator
Bundle-Description: View Patient File
Bundle-SymbolicName: com.example.patient
Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.0
Import-Package: android, android.app, android.content, org.osgi.framework

同样的错误继续一次又一次。有人能帮忙吗?谢谢你。

The same errors keep coming again and again. Can someone help? Thank you.

注意:我把大部分的这些想法,并从谁曾与这个话题previous经验用户的 @ slash33 的的code。

Note: I took Most of these ideas, and the code from the user @slash33 who had a previous experience with this topic.

推荐答案

原来,这个问题是在我的 MANIFEST.MF 文件。我无法找到的错误,但我使用的文件的旧副本,然后所有的错误消失了。下面是我更新 MANIFEST.MF 文件

The problem turned out to be in my MANIFEST.MF file. I could not locate the error, but I used an old copy of the file, and then all the errors disappeared. Below is my updated MANIFEST.MF file

Manifest-Version: 1.0
Bundle-Vendor: Zaid
Bundle-Version: 1.0.0
Bundle-Name: Patient
Bundle-ManifestVersion: 2
Bundle-Activator: com.example.patient.Activator
Bundle-Description: View Patient File
Import-Package: org.osgi.framework, android.content
Bundle-SymbolicName: com.example.patient
Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.0

我的APK包已成功加载到菲利克斯框架,我现在能够启动包内的活动。感谢上帝!

My APK bundle was successfully loaded to Felix framework and I am now able to launch activities inside the bundle. Thanks God!

这篇关于在APK包采用Android API为:&QUOT;找不到类的android.content.Context“,从法com.example.patient.Activator.start&QUOT参考;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:05