I am developing an android app in which I have included a library project, I am able to use the resources of library project (ie layouts, styles etc) inside my main project. But I want to use the resources of my main project inside my library project. Is there any way to do that? I am stuck at this problem for quite a time.
You can create dummy resources inside your library project, and anything with the same name in the main project will override it.
Example:
LibProject/res/values/strings.xml:
<string name="greeting">Hello from Library Project</string>
MainProject/res/values/strings.xml:
<string name="greeting">Hello from App Project</string>
Then, wherever you use R.string.greeting, even in your library project, the value from the main project will be used.
From the docs:
The application itself has highest priority and its resources are
always used in preference to identical resource IDs defined in
libraries.
Related
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.
I am creating an Android dependency library which will be packaged as an AAR file. The dependency will have layouts, strings, dimens, images etc. I created a sample demo project which includes this dependency.
Now consider the following scenario.
My library includes an Image file named filter.png and uses it inside a layout file. The demo project also has an Image named filter.png but it is a different image. So when the project gets built, only the app's filter.png is picked up. So even in my library's layout file, I am seeing the image used by the demo project.
As of now, I have changed the name and appended package name before every resource name to avoid the above scenario.
But is there any way I can force Android to pick resources only from the current module?
A library will only have access to its own set of resources, but nothing can prevent the application from intentionally overriding the library's resources.
To prevent accidental overrides, you usually prefix your library resource names. For example, AppCompat uses the abc_ prefix and the Design support library uses the design_ prefix.
Furthermore, you can explicitly declare which resources of your library are public, so that the other ones will be private by default and if the app overrides them you will get a Lint warning.
You app and library will have different package names, yes.
As of now, I have changed the name and appended package name before every resource name to avoid the above scenario
And that is correct because resources are associated by package name.
com.example.app.R.drawable.filter (Which is often just R.drawable.filter, check your import statements!), the current module.
vs some other module, com.example.library.R.drawable.filter
Android - Accessing Resources
[<package_name>.]R.<resource_type>.<resource_name>
I have been happily refactoring code from different versions of the same app (paid/free) into Android library projects so that the actual apps can simply customize the library and reduce code duplication.
One thing I'm started to wonder is what getApplicationContext() inside the library code means? Is is the same ApplicationContext as one would get from the child apps? What happens when I access SharedPreferences from a library project's getApplicationContext() instead of the original app's getApplicationContext()? Will the SharedPreferences file be same or different?
What if I had used the activity to access SharedPreferences? Does it matter that the activity is now a library activity and not the original app? Is the SharedPreferences the same?
Thanks for clarifying.
When the APK is packaged up then all classes will be belong to the main application.
call getApplicationContext().getPackageName() and it will return the app's package name, and not the library's package.
I have the same setup for a free/paid application and no issues when I moved my classes into a library project.
However you have to check your xml files (manifest, widgets, etc.) to use the full package name of your library project.
A library project is almost like having all the code in one project. There are a couple of things to watch out for related to namespaces but generally it works very well.
e.g. Your library has its own namespace
Library package name = uk.co.lib
Main App package name = uk.co.app
Activities in the library that you want tro access from the main app have to be added to the app manifest. Activity named A in library project would be added to manifest in main app like this:
<activity android:name="uk.co.lib.A">
Accessing shared preferences etc would give the same result from either namespace and would return the preferences for the app.
There is only one application so there is only one ApplicationContext
I am creating a library, which should be distributed as a jar file.
Public function is void SomeLibClass.showSomeDialog(Context context) and task library is dialog show with a complex View inside. No activity, nothing difficult.
The pattern using library - developer add it to your project, and call in one place SomeLibClass.showSomeDialog and enjoy the funny dialog.
Seems logical that the resources needed to render the dialog were inside the jar file(to avoid errors such as: jar updated, but no resources) - we need in jar file: layout, string, drawable, etc.
And i want call resource by Id, like when i make layout i continue write like android:text="#string/hello"
But this logical thing to do can not be obtained.. now i make all resources in jar file with prefix, but how to solve the problem with the same id in R.java?
Or am I wrong look, and this is all we have to somehow do differently? Wants to repeat a simple - all you need for the library was in it.
If you wish to distribute Android-compatible Java code with resources, you need to use an Android library project.
The upcoming r14 edition of the Android Developer Tools reportedly has the ability to package up a library project, including resources, as a JAR. r14 should be released soon -- I would wait until this is released and then see how to apply it to your project.
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.