1

I want to use a library in my android app but want this library to be optional to the users.

To make it optional, I want to distribute the library separate from the apk. So that:

  • Users who intend to use this option can download the lib from within the app

  • And those who don't wish to use this get a smaller apk file.

I want to ask, Is it possible to distribute a library separately from the apk?.

I've seen some games downloading files after their installation. So, I feel this might be achievable but don't know how to achieve this.


Note: Please keep in mind the following:

  • The classes or methods would only be used in case the library has been downloaded by the user and would not be used in the other case.

  • Do not confuse distribution of library with distribution of resources. So please keep in mind that I want to separately distribute a library and not resources.

Shivam Arora
  • 107
  • 3
  • 10
  • Android games make use of expansion files: https://developer.android.com/google/play/expansion-files.html – dragi May 20 '16 at 08:44
  • You can probably put it in an OOB file and distribute it that way. – Shark May 20 '16 at 09:32
  • @Shark If it is possible to distribute libraries via OBB files, can you please tell me how can I provide a reference to the classes of the library in order to import them? – Shivam Arora May 20 '16 at 12:15
  • You would bundle a library with stub methods that do nothing? Sorry to say this but more information is required. More specifically - a usecase. – Shark May 20 '16 at 13:14
  • @Shark The library would only be used in case the user opts to use it and not in the other case. – Shivam Arora May 21 '16 at 04:53

2 Answers2

0

I don't think you can use a library as expansion file because if you use any class, methods of library in your project you have to import them, and you can not import them without using library in project, But I am not sure.

Please see below link for more detail.

https://developer.android.com/google/play/expansion-files.html

Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54
  • The classes or methods would only be used in case the library has been downloaded by the user and would not be used in the other case. – Shivam Arora May 20 '16 at 12:04
0
  • You can distribute your base functionality with your APK
  • If the user chooses to, your application can download additional files, including libraries. If they are not expansion files (OBB), then you should take care hosting them.
  • These additional libraries will have to be loaded in a similar manner:

    for (File pluginFile : pluginFiles)
    {
        try
        {
            JarFile jarFile = new JarFile(pluginFile);
            Enumeration<? extends JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements())
            {
                JarEntry entry = entries.nextElement();
                if (entry.getName().endsWith(".class"))
                {
                    URLClassLoader loader = URLClassLoader.newInstance(new URL[]{pluginFile.toURI().toURL()});
                    System.out.println(entry.getName());
                    Class<?> entryClass = loader.loadClass(entry.getName().replace(".class", "").replace("/", "."));
                    if (isPlugin(entryClass))
                    {
                        IPlugin plugin = (IPlugin)entryClass.newInstance();
                        plugins.add(plugin);
    
                        System.out.println("Added plugin \'" + plugin.getPluginId() + "\' from \'" + pluginFile.getName() + "\'");
                    }
                }
            }
        }
        catch (InstantiationException | ClassNotFoundException | IOException | IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
    

    This assumes you want to load JAR files.

dragi
  • 3,385
  • 5
  • 22
  • 40
  • Sorry, didn't understand what this code does at some points. But want to ask something related to this, **1.** Does this code need to be run only once i.e after downloading the library or everytime when user runs the app? **2.** Would it work the same as if I've bundled the library within the apk i.e. with the same efficiency? – Shivam Arora May 21 '16 at 04:59
  • The code loads the classes from a JAR file, so that your application can use them. 1. This code needs to be run every time the app is started by the user. 2. I guess it would add a little overhead, so will be a little bit slower, but it will probably not be noticeable by the user (unless this library is huge). – dragi May 21 '16 at 08:35
  • Thanks. Is there something that I can do, when the user downloads the library and after its completion, execute a code to add the classes to the apk or add a lib to the apk installation just as it is added in a normally apk bundled with libs? – Shivam Arora May 22 '16 at 14:58
  • Maybe there is, but I am not aware of that. – dragi May 22 '16 at 19:09
  • Ok. Thanks for your help @helleye – Shivam Arora May 24 '16 at 11:40