osgi框架 android,android osgi

场景:创造兼容android activity的OSGI bundle

创建兼容android activity的OSGI bundle

1.正常创建一个android 应用

2. 将OSGI 框架所在的主应用项目设置为该bundle的引用项目。在eclipse上进入build path->configure build path->projects进行设置。

3. 创建生成manifest.mf. 可使用eclipse的bndtools 插件或其原始版本bnd.jar来自动生成该文件(bndtools貌似有bug, 我用它无法生成正确的manifest.mf和jar包,但bnd.jar可以。我用的命令行是:java -jar bnd-2.1.0.jar buildx --classpath bin --eclipse --output test.jar bnd.bnd)。

4. 进入android tools->Export unsigned Android package导出未签名的android APK文件

5. 使用任意zip工具,比如7zip打开apk, 新建一个folder: META-INF,并且将生成的manifest.mf拷贝到这个folder

6. 将工程产生的java class文件拷入apk的根目录下

7. 使用java 签名工具对APK文件进行签名

jarsigner -verbose -keystore /path_to_keystore/mykeystore.keystore my_application.apk my_keystore_alias.

8. 在emulator 或device上安装该apk,OSGI framework 安装启动该bundle。

参考:http://stackoverflow.com/questions/19922775/android-activity-in-osgi-bundle/23722876#23722876

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OSGi是一个基于Java的动态模块化系统,它可以将应用程序分解为多个独立的模块,每个模块可以独立地安装、卸载、更新和启动。在Android平台上,OSGi框架也可以被使用,通过OSGi框架,我们可以实现Android应用程序的模块化开发和管理。 在Android上使用OSGi框架的好处在于,可以实现应用程序的动态升级和扩展,而不需要重新安装整个应用程序。同时,OSGi框架也能够提高应用程序的可维护性和可扩展性,更好地实现应用程序的分层架构和模块化设计。 在Android平台上使用OSGi框架的步骤如下: 1. 引入OSGi框架:在build.gradle文件中添加OSGi框架的依赖,如: ```gradle dependencies { implementation 'org.osgi:org.osgi.core:6.0.0' implementation 'org.osgi:org.osgi.compendium:5.0.0' } ``` 2. 定义Bundle:将应用程序按照功能划分为多个Bundle,每个Bundle包含一个或多个组件,如Activity、Service、BroadcastReceiver等。每个Bundle需要定义一个BundleActivator,如: ```java public class MyBundle implements BundleActivator { @Override public void start(BundleContext context) throws Exception { // Bundle启动时执行的操作 } @Override public void stop(BundleContext context) throws Exception { // Bundle停止时执行的操作 } } ``` 3. 模块管理:在应用程序中实现模块的管理,包括模块的安装、卸载、更新等功能。如: ```java BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); // 安装Bundle bundleContext.installBundle("file:./mybundle.jar"); // 卸载Bundle bundleContext.getBundle(1).uninstall(); // 更新Bundle bundleContext.getBundle(1).update(new FileInputStream("./mybundle.jar")); ``` 4. 组件通信:在不同Bundle之间实现组件的通信,如Activity之间的跳转、Service的调用等。如: ```java // 启动Activity Intent intent = new Intent().setClassName("com.example.mybundle", "com.example.mybundle.MyActivity"); startActivity(intent); // 绑定Service ServiceConnection serviceConn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { MyService myService = ((MyService.MyBinder) service).getService(); } @Override public void onServiceDisconnected(ComponentName name) { } }; bindService(new Intent(this, MyService.class), serviceConn, Context.BIND_AUTO_CREATE); ``` 5. UI管理:在应用程序中实现UI的管理,包括UI的加载、卸载、更新等功能。如: ```java // 加载Fragment Fragment fragment = (Fragment) bundleContext.getBundle(1).loadClass("com.example.mybundle.MyFragment").newInstance(); getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit(); // 卸载View View view = findViewById(R.id.my_view); ViewGroup viewGroup = (ViewGroup) view.getParent(); viewGroup.removeView(view); // 更新View TextView textView = findViewById(R.id.my_text); textView.setText("Hello, World!"); ``` 6. 安全管理:在应用程序中实现安全机制,包括应用模块的权限管理、数据隔离等。 以上代码仅为参考,请根据实际情况进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值