Android: how to share code between projects signed with the same certificate

In Android documentation concerning code signing we can read: “By signing multiple applications with the same certificate and using signature-based permissions checks, your applications can share code and data in a secure manner.

How exactly such code sharing can be done? Is it possible to release main application and multiple exchangeable plugins then discover them at runtime? What does source code looks like and what are advantages over “standard” intents calls from/to different APK packages?

  • Android SDK and AVD Manager Error: File not found: C:\Program Files (x86)\Android\android-sdk\temp\
  • Android: how to check if a View inside of ScrollView is visible?
  • Found 2 versions of android-support-v4.jar in the dependency list appcompat in android
  • Scroll to last line of TableLayout within a ScrollView
  • Failed to convert @drawable/ into a drawable
  • Local Notifications in Android?
  • Related posts:

    Set upper case for TextView
    How to update ListView in case of CursorAdapter usage?
    Creating Hardlinks and Symlinks in Android
    MediaRecorder.stop() hanging with Android 4.0 (ICS)
    Custom Class Loading in Dalvik with Gradle (Android New Build System)
    How to retain Android ListView scroll position when returning to an activity?
  • ImageButton does not display a particular drawable
  • Getting Image from ImageView
  • Generate .so file in Android Studio
  • How to send a high-priority GCM?
  • Android Bind Service returns false every time
  • Fatal signal 7 (SIGBUS) at 0x00000000 (code=2)
  • 2 Solutions collect form web for “Android: how to share code between projects signed with the same certificate”

    Use Context.createPackageContext() to instantiate a package for another .apk you are interested in. If it is signed with the same cert as yours, AND you are both using the same shared user ID, then you can use the flag to load its code into your process, allowing you to get the ClassLoader from the context and instantiate whatever classes you want.

    Otherwise, if they are not signed the same and explicitly using the same shared used ID, you can only load its resources.

    Please note that you can not change the shared user ID for an application (to something else or moving between having and not having a shared user ID) once that application is on market.

    Say you want to call a public function of the dynamically loaded class. Use the following code snippet:

    Context friendContext = this.createPackageContext("packageName", Context.CONTEXT_INCLUDE_CODE);
    Class friendClass = friendContext.getClassLoader().loadClass("packageName.className");
    Class noparams[] = {}; //say the function (functionName) required no inputs
    friendClass.getMethod("functionName", noparams).invoke(friendClass.newInstance(), null);
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.