I have on this moment too many xml string values. I started to delete the ones I don’t use, but this is quite hard (I don’t really know which I do use, and witch I don’t). Is there a function in eclipse that can do this for me, or help me whit it? (Like some call hierarchy)
Android Lint should be able to help you with this.
On the website it states that it can do following things:
Missing translations (and unused translations)
Layout performance problems (all the issues the old layoutopt tool used to find, and more)
Unused resources
Inconsistent array sizes (when arrays are defined in multiple configurations)
Accessibility and internationalization problems (hardcoded strings, missing contentDescription, etc)
Icon problems (like missing densities, duplicate icons, wrong sizes, etc)
Usability problems (like not specifying an input type on a text field)
Manifest errors
Related
In Android Studio 2.2.2 I have an error in AndroidManifest.xml file saying
Resources referenced from the manifest cannot vary by configuration
There is a StackOverflow question by someone else on this message but the answer only describes how to ignore it. What I want to know is what does it mean?
The line associated with the error says
android:versionName="#string/appvername"
what does the error mean and how do I prevent (not just ignore) it? What is a "configuration" in this context?
What is a "configuration" in this context?
Quoting the documentation:
You should always externalize resources such as images and strings from your application code, so that you can maintain them independently. Externalizing your resources also allows you to provide alternative resources that support specific device configurations such as different languages or screen sizes, which becomes increasingly important as more Android-powered devices become available with different configurations. In order to provide compatibility with different configurations, you must organize resources in your project's res/ directory, using various sub-directories that group resources by type and configuration.
So, a configuration is a mix of device capabilities and states that controls what resources get loaded. For example, the device locale settings determine which strings get used from your available string resources.
What I want to know is what does it mean?
Not every attribute in the manifest can be populated by a resource, because the system cannot handle varying values based on configuration.
For example, you cannot change the Java class name of an <activity> by using a string resource in android:name, with an eye towards using different Java classes with different screen sizes. While that's an interesting concept, Android is not set up to support that.
how do I prevent (not just ignore) it?
In this case, I think you are encountering an IDE bug. android:versionName should support string resources, as that is a user-facing value, and therefore you might want to translate the string. So, add tools:ignore="ManifestResource" to the <manifest> element, until the bug gets fixed.
Let me start by describing my problem. I am working on a large Android application that has a large number of layouts and strings (all in strings.xml). A new requirement has been made that requires some of the strings to be different depending on a condition at startup (think all instances of "cat" being replaced with "dog"). This condition does NOT depend on anything with the device (locale, size, resolution, etc.): it only depends on something within the business logic.
The simplest approach would be to create new copies of all the layout files that use the strings that have "cat", and in those new layout copies instead use strings that have "dog". However, this is not entirely feasible due to the number of layouts, as well as future maintenance concerns of basically duplicated layouts.
I know this isn't a discussion forum, so I have an approach I'd like to focus on. It seems to me the problem is closely related to normal resource configuration/locale, where different resources are provided due to attributes of the device. Is there a way to get this working for my situation? The differences are the desired configuration is only known at runtime and there isn't an existing resource qualifier that makes sense (we might translate into French, for example, so I don't want to piggyback on that). I can't imagine my problem is unique, though.
If there's another approach I should use let me know, although I don't want to go too far in the discussion route because SO frowns upon it. Frankly it's probably risky even asking this here, but I don't know where else to ask it.
I can think on several solutions:
You can set the string on runtime like yourTextView.setText(R.string.any_string) with:
1) A To have some resources strings for you business logic.
2) A placeholder string,
getResources().getString(R.string.tempale, "your value")
3) Get the string from web service if that posible
However this is not a logical question still i am much interested to know if i have any idle file that i am not using in my project.Sometime after a long project we just dont recognize that which is the actual file and which is the copy of it.Sometime we just create some xml files to check the layout and other effects like selector or some custom drawable.I mean though these file never be get executed still it crates logical confusion when we get back to our code about true identity and use of the file.
I read that Proguard removes all unused files before making apk file.Does it remove all these xml files also ?
I am bit new about proguard concept.
Thanks.
You can use AndroidLint, tool who scan your project and return a lot of advices, and then, all unused resources !
http://tools.android.com/tips/lint
- Missing translations (and unused translations)
- Layout performance problems (all the issues the old layoutopt tool used to find, and more)
- Unused resources
- Inconsistent array sizes (when arrays are defined in multiple configurations)
- Accessibility and internationalization problems (hardcoded strings, missing contentDescription, etc)
- Icon problems (like missing densities, duplicate icons, wrong sizes, etc)
- Usability problems (like not specifying an input type on a text field)
- Manifest errors
The matter: I used many icons and pictures while developing an android application, later I replaced many of them but kept the old ones in case I would need to use them again. Now I have a huge amount of .png's in my drawable folder, many are now unused and it would take ages to manually sort them out. Is there a way to detect drawables to which no references exist?
You can use Android Lint Tool, follow the link I provide , as an overview it states:
Missing translations (and unused translations)
Layout performance problems (all the issues the old layoutopt tool used to find, and more)
Unused resources
etc.
As an alternative this project might help you (pretty easy to use):
https://code.google.com/p/android-unused-resources/
Hope it's useful...
In my experience, Lint does not detect unreferenced PNGs. It finds unreferenced layouts and drawable XML files like shapes and selectors but not PNGs. I stumbled upon this Python script for removing unreferenced PNG drawables. It worked well with my project but, as the documentation for it says, make sure you are using source control just in case.
https://github.com/instructure/android-ImageSweep
I have done some significant re-coding on one of my Android programs and now I am unsure if certain xml strings are used anymore. In addition I have a few translations which makes the task even more difficult. Is there a tool to test this? This would be useful for drawables also.
I am using the eclipse plugin.
This question has been discussed in the irc channel before. There is no tool to test it, but I agree it would be useful. Note that resources can be referenced in xml, but they can also be referenced from code. Furthermore, resources can also be looked up by their identifier, and such lookup could be determined by runtime.
So actually you cannot determine 100% whether a resource is used or not anymore, but you can probably determine which resources are referenced in a static way (in xml or code). Depending on your code/app which you know best yourself, such approach might be sufficient in many cases.
The approach would be to write a tool that parses xml and java source files and also take the import statements into consideration. With that information you should be able to determine which resources you can get rid of.
The easiest way is to remove them all, attempt to compile, and re-add those the compiler says are lacking. It's a little tiresome, but it's certainly tractable.
Note, as Mathias already pointed out, that it's technically possible to access resources by name with a string at runtime, and the way I suggest here would remove such resources though they are, in fact, needed. However, this pattern should be really rarely seen in any application, and if you are the one who wrote it, you already know if/where you do such treatment.
Use grep to extract a list of resources to a file by way of sort
Use recursive grep through sort and uniq to create a list of those mentioned in any source file (make a copy of project without unused files or dispatch grep on a list of used ones, of course commented out code will be an issue)
Use diff on the two lists