My usage scenario is a bit complicated.
In the dynamic form, I only have some views and resources. Those views should have access to those resources. In my base module, I will use these views from the activity of a basic module. Both my application and activity replace the attachBaseContext method.
But when I download and install the dynamic form, my views can not access resources using the task context. However, resources are accessible from applicationContext.
I do not understand how SplitCompat works. So the classes and resources loaded by the dynamic module connect only to applicationContext?
This problem is probably caused by an Android Studio bug. Clean the project and invalidate the cache, or build the project again.
You can take a look at how Dynamic Features are implemented in this sample.
There's sample code on how to open some resources, located in a dynamic module, from the base module.
Plus, the samples includes others dynamic modules with activities in implemented in Java/Kotlin and native.
Related
I am trying to write a modular extensible application to deploy in Android. The idea is to provide an API to allow the creation of custom functionality for the app that may include custom layouts and other resources. This custom functionality will be loaded, at runtime, from another location (e.g. SD Card).
Currently I am able to load .jar files from this location and work with them as I like, unfortunately I can only include references to layouts and resources that are also present in the "Main" project.
I have been unable to find a good way to reference an entire library project, resources included. I essentially want each custom piece to contain all the resources it needs to display and run itself.
Right now I am toying with the idea of including an "Assets" project that can be referenced by each of the modules to be a central area to store layouts and other resources. Unfortunately this would require me to have a project that must be loaded by any other project that needs to be built.
Another idea was to include the layouts, images and strings, along with the jar files, in a folder and load those at run time. I don't think this will work well since the layouts seem to be pre-processed at compile time in some way and cannot be inflated at run time.
Does anyone know a way for me to include all the resources and code into a single, dynamically loadable, file that I can then access at runtime?
Does anyone know a way for me to include all the resources and code into a single, dynamically loadable, file that I can then access at runtime?
That is not supported. It is rather likely that it will never be supported, though it is possible that it might work with the new build system that is under development.
The standard advice for sharing code & resources between Android projects is to use a library. Personally I find this works poorly if (a) the shared code changes a lot, or (b) your computer isn't fast enough.
I also don't want to get into deploying multiple APK's, which seems to be necessary when I use dependent projects (i.e. Java Build Path, Projects Tab).
On the other hand, sharing a folder of source code by using the Eclipse linked source feature works great (Java Build Path, Source tab, Link Source button), but for these two issues:
1) I can't use the same technique to share resources. I can create the link to the resources parent folder but then things get wonky and the shared resources don't get compiled (I'm using ADT 21).
2) So then I settle for copying the shared resources into each project, but this doesn't work because either. The shared code can't import the copy of its resources because it doesn't know the package name of the project that uses it. The solution I've been using is to access the resources dynamically, but that has become cumbersome as the number of resources grows.
So, I need a solution to either (1) or (2), or I'll have to go back to a library project. (Or maybe there is another option I haven't thought of?)
Your real problem is (2). Fixing (1) would eliminate some copying, but you would still run into problems with (2).
Unfortunately, that really isn't possible. There's a fair bit of fancy footwork that goes on to make multiple packages possible with library projects, and there's no good way to get that same result without library projects. Anything in res/ of a project is accessed via that project's R class, including your copied resources.
The solution I've been using is to access the resources dynamically
I translated that into you using getIdentifier(). That certainly works. Another approach is to having the hosting app supply resource IDs as parameters to the library code -- this is the pattern that the Android SDK itself uses. This is faster at runtime than the reflection-based getIdentifier(), and it gives the hosting app somewhat more flexibility, but you do wind up adding a bunch of parameters to your methods and constructors as needed to supply the various project-specific R values.
Is it possible to share resources across APK's? For example, can application A (in APK A) load an icon or layout view from application B (in APK B)?
You can make use of getResourcesForApplication
That way you can load whatever you want from other app package as long as you know at least the package name and the id or name of the resource to load.
As a side note, layouts cannot be loaded without further processing them with an XMLResourceParser because of possible id mismatches between your app package and the "guest" package.
Two different apps can share resources - images/ files,etc. if they are signed with same certificate.
Please check android doc here
Only if it is delivered by content provider and the content serialized.
You can have two applications use the same Android library, which lets you share resources like activities, etc.
An Android library project is a
development project that holds shared
Android source code and resources.
Other Android application projects can
reference the library project and, at
build time, include its compiled
sources in their .apk files. Multiple
application projects can reference the
same library project and any single
application project can reference
multiple library projects.
I am creating a library for Android projects.
The activity is declared in the library project, as it will be reusable in different projects. This activity is using images which are project specific. I have added those images in the main project.
How can I access resources from the main project in the library?
Put a copy of all relevant resources in the library. It's perfectly legal. If you want to override them in the actual app, you can. If the IDs clash, the end product will use the resources from the app.
EDIT: the alternative is filesystem-level links. On Windows, use mklink to create hardlinks to your resource folders in the project; on *nix, use ln. This may mess up your source control, if any, so proceed with caution.
I would like to build a library and be able to distribute it as a jar without having to give the source. In the library, layouts are used for specifying the UI, however android doesn't seem to facilitate easily bundling a jar and distributing it, as it doesn't properly scope the resources (anything in '/res/*') in this jar file, the references made with R.xxxx within the jar don't work.
I can give the xml layouts and other resources to the client and ask them to put them into their resources directory, thus their R.java would have these references, now, how can the client pass this R.java to the library when invoking a method in the library?
Guess, answer to part of the question would be through answer to 'How to pass class in java?"
Yes, I am new to android and java too.
Thanks,
Krishna
If you have just simple layouts you could also create them in Java and not define them in XML.
It's not so nice but you don't have to distribute some other files.