title: Android Activity in OSGI Bundle
body: <p>I am working on developing OSGI bundles that have a full support for android. So far, by my previous questions, I was able to use android API inside OSGI bundle. It works fine and I tried it. I am using Felix framework.</p>
  
  <p>However, I am stuck now at the mission of making an OSGI bundle to have an android activity and to start that activity. I also need these activities to be able to request permissions so I guess I will need an <code>AndroidManifest.xml</code> in the OSGI bundle.</p>
  
  <p>While doing a research, I was able to find only one person describing his experience in achieving this. Unfortunately, the steps he mentioned is ambiguous to me.</p>
  
  <p>In his question <a href="https://stackoverflow.com/questions/18587855/full-android-support-for-osgi-bundles">"Full Android Support for OSGI"</a> , Here's what he said: </p>
  
  <pre><code>I have found a way to start activities owned by android bundles:
  
  •the android bundle MUST be an APK which can be created using Eclipse Android Project
  •add a Reference Library entry to the project Build Path for your OSGi framework (in my case framework.jar)
  •edit bundle.manifest describing the bundle. The file is not part of the APK but will be used on build
  •the bundle's code, especially the Activator class, MUST be in the same package as defined in AndroidManifest.xml AND the symbolic name of the bundle MUST be the package name as well. If these conditions are met then all of the classes will be correctly loaded. If not, it will result in seeing java.lang.NoClassDefFoundError on runtime
  •Use Android Tools &gt; Export Unsigned Android Package
  •copy bundle.manifest in the unsigned APK as META-INF/MANIFEST.MF
  •sign the APK using whatever certificate you want
  •install the signed APK like any standard android application. Installation is required in order to have the Activity resolved. Without this the activity won't resolve and the bundle will fail
  •have the OSGi framework load the bundle APK
  </code></pre>
  
  <p>Where he says:</p>
  
  <pre><code>•edit bundle.manifest describing the bundle. The file is not part of the APK but will be used on build
  </code></pre>
  
  <p>All I did is to create an android project (APK) and to follow the first two steps. But at this third step above, I couldn't find <code>"bundle.manifest"</code> to edit it. It is not there at all, so how come he says edit it? </p>
  
  <p>Also, when I <code>Export Unsigned Android Package</code>, from where to where should I copy the manifest file?</p>
  
  <p>Finally, is the final signed APK file my bundle that should be loaded by the framework? This seems odd because It is not even a jar file.</p>
  
  <p>If these steps would not help me, then can someone lead me in the right direction? Thank you.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>No one answers my question, so I did the following:</p>
  
  <p>1- In my android application project (which I am trying to make it act as a bundle), I included my Activator class in the same package mentioned in the AndroidManifest.xml.
   Here's my <code>Activator.java</code> class:</p>
  
  <pre><code>package com.example.patient;
  
  import org.osgi.framework.BundleActivator;
  import org.osgi.framework.BundleContext;
  
  public class Activator implements BundleActivator {
  
      private static BundleContext context;
  
      static BundleContext getContext() {
          return context;
      }
  
  
      public void start(BundleContext bundleContext) throws Exception {
          Activator.context = bundleContext;
  
     //I WOULD LIKE TO START THE ACTIVITY HERE TO DISPLAY THE TOAST MESSAGE
          System.out.println("Android APK Bundle Started");
  
      }
  
  
      public void stop(BundleContext bundleContext) throws Exception {
          Activator.context = null;
  
  
  
      }
  
  }
  </code></pre>
  
  <p>and here's my <code>AndroidManifest.xml</code></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.patient"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.patient.View_Patient_File_Activity"
              android:label="View Patient File" &gt;
  
  
          &lt;/activity&gt;
          &lt;activity
              android:name="com.example.patient.Enter_Patient_ID_Activity"
              android:label="View Patient File" &gt;
  
                          &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
  
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>2- I added a Reference Library entry to the project Build Path for my OSGi framework (felix.jar)</p>
  
  <p>3- I generated an unsigned copy of my project using Android Tools.</p>
  
  <p>4- I added a folder to the root directory of the unsigned copy called <code>META-INF</code>, and inside that folder, I added a file called <code>MANIFEST.MF</code>, below is the content of that file:</p>
  
  <pre><code>Manifest-Version: 1.0
  Bundle-ManifestVersion: 2
  Bundle-Name: Patient
  Bundle-SymbolicName: com.example.patient
  Bundle-Version: 1.0.0.qualifier
  Bundle-Activator: com.example.patient.Activator
  Import-Package: org.osgi.framework;version="1.3.0"
  Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.0
  </code></pre>
  
  <p>5- I manually signed the unsigned copy using command line and jar signer tool. Something like:</p>
  
  <pre><code>jarsigner -verbose -keystore /path_to_keystore/mykeystore.keystore my_application.apk my_keystore_alias 
  </code></pre>
  
  <p>6- I installed the signed copy on my PC and on my tab.</p>
  
  <p>7- Finally, I run my application, and let the OSGI framework load the same signed apk file. </p>
  
  <p><strong>NO USE</strong>, although the bundle status is active, but I do NOT see the message in the activator start() method, meaning that, my bundle is not loaded correctly. Where did I go worng?
  Please help.</p>
  
  <p><strong>UPDATE 25-11-2013</strong></p>
  
  <p>I made sure to do the steps correctly, and now I got this:</p>
  
  <pre><code>11-25 17:54:08.600: W/System.err(2714): org.osgi.framework.BundleException: Not found: com.example.patient.Activator
  11-25 19:22:36.590: W/System.err(6652): Caused by: java.lang.ClassNotFoundException: com.example.patient.Activator not found by com.example.patient
  </code></pre>
  
  <p>Meaning that my bundle does not contain the Activator class, but I am sure it does. What could be wrong?</p>
  
  <p><strong>UPDATE 26-11-2013</strong></p>
  
  <p>I opened the signed APK using WinZip. I noticed that unlike the bundles I used to build, the signed APK does not contain the .class files including the "Activator.class", so I copied the com directory that contains all the .class files of the project, and I pasted it in the signed APK. Next, I signed that APK again. Now, when I install the APK, I get the following log which contains many errors:</p>
  
  <pre><code>11-25 23:16:25.651: D/dalvikvm(5617): DexOpt: --- BEGIN 'bundle.jar' (bootstrap=0) ---
  11-25 23:16:26.271: D/dalvikvm(5617): DexOpt: --- END 'bundle.jar' (success) ---
  11-25 23:16:26.271: D/dalvikvm(5617): DEX prep '/sdcard/felix-cache-1472376252.tmp/bundle1/version0.0/bundle.jar': unzip in 102ms, rewrite 620ms
  11-25 23:16:26.271: W/dalvikvm(5617): Class resolved by unexpected DEX: Lcom/example/patient/Activator;(0x4074fa08):0x18a7b8 ref [Lorg/osgi/framework/BundleActivator;] Lorg/osgi/framework/BundleActivator;(0x40714410):0xbd630
  11-25 23:16:26.271: W/dalvikvm(5617): (Lcom/example/patient/Activator; had used a different Lorg/osgi/framework/BundleActivator; during pre-verification)
  11-25 23:16:26.271: I/dalvikvm(5617): Failed resolving Lcom/example/patient/Activator; interface 902 'Lorg/osgi/framework/BundleActivator;'
  11-25 23:16:26.271: W/dalvikvm(5617): Link of class 'Lcom/example/patient/Activator;' failed
  11-25 23:16:26.271: E/dalvikvm(5617): ERROR: defineClass(0x4074fa08, com.example.patient.Activator, 0x4079d468, 0, 955, 0x4073e918)
  11-25 23:16:26.271: E/Zaid Log(5617): Problem installing the bundle :s
  11-25 23:16:26.271: W/System.err(5617): org.osgi.framework.BundleException: Activator start error in bundle com.example.patient [1].
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.Felix.activateBundle(Felix.java:2196)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:942)
  11-25 23:16:26.271: W/System.err(5617):     at com.example.patient_application.MainActivity.onCreate(MainActivity.java:136)
  11-25 23:16:26.271: W/System.err(5617):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
  11-25 23:16:26.271: W/System.err(5617):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
  11-25 23:16:26.271: W/System.err(5617):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
  11-25 23:16:26.271: W/System.err(5617):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
  11-25 23:16:26.271: W/System.err(5617):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
  11-25 23:16:26.271: W/System.err(5617):     at android.os.Handler.dispatchMessage(Handler.java:99)
  11-25 23:16:26.271: W/System.err(5617):     at android.os.Looper.loop(Looper.java:132)
  11-25 23:16:26.271: W/System.err(5617):     at android.app.ActivityThread.main(ActivityThread.java:4028)
  11-25 23:16:26.271: W/System.err(5617):     at java.lang.reflect.Method.invokeNative(Native Method)
  11-25 23:16:26.271: W/System.err(5617):     at java.lang.reflect.Method.invoke(Method.java:491)
  11-25 23:16:26.271: W/System.err(5617):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
  11-25 23:16:26.271: W/System.err(5617):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
  11-25 23:16:26.271: W/System.err(5617):     at dalvik.system.NativeStart.main(Native Method)
  11-25 23:16:26.271: W/System.err(5617): Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
  11-25 23:16:26.271: W/System.err(5617):     at java.lang.VMClassLoader.defineClass(Native Method)
  11-25 23:16:26.271: W/System.err(5617):     at java.lang.ClassLoader.defineClass(ClassLoader.java:319)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2279)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1501)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
  11-25 23:16:26.271: W/System.err(5617):     at java.lang.ClassLoader.loadClass(ClassLoader.java:500)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.BundleWiringImpl.getClassByDelegation(BundleWiringImpl.java:1374)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.Felix.createBundleActivator(Felix.java:4329)
  11-25 23:16:26.271: W/System.err(5617):     at org.apache.felix.framework.Felix.activateBundle(Felix.java:2141)
  11-25 23:16:26.271: W/System.err(5617):     ... 17 more
  </code></pre>