How to not compile certain Android menu items on 'release' builds - android

I have a certain menu item I call 'debug' that does some things that are handy for 'debug' releases. I want this menu item not to exist on 'release' APKs.
Is there a way to tell the compiler to ignore those entries in the XML menu file?
Thanks :-)

If you are using menu XML resources, you can isolate those debug-only choices into their own resource XML file. Then, in addition to inflating your main one, you also inflate the debug-only one, if BuildConfig.DEBUG is true:
if (BuildConfig.DEBUG) {
// inflate that second resource XML file
}
If you are setting up your menu using Java code, you can do the same basic thing, wrapping the debug-only add() calls in a check of BuildConfig.DEBUG.
The upcoming Gradle-based build system will help to make this a bit easier, in principle, but that is several months away at this point AFAIK.

If you use Jenkins to create your builds you can replace your debug menu xml file with the production one using shell commands. This is also very effective with webservice detail xml files.

Related

How to rename (Refactor) xml element in Android Studio without affecting other activities

In my Android mobile app project, I am using Android Studio as an IDE. I have multiple activities there. Multiple of activities have xml element with the same name ("myTextView"). These are absolutely different xml textViews located in different layouts for different not related to each other activities. The only common thing is these textViews have similar id = "myTextView".
I try to rename xml element in xml layout for one activity. Say, in activity_layout1.xml I would like to rename "myTextView" to "someonesTextView". Android Studio automatically makes it via refactoring.
However, during refactoring, it renames elements with the same id (myTextView) in all the other activities. Finally, I have "someonesTextView" in other activities though they are not related.
How can I avoid that?
In that case, you don't have to refactor the id but instead edit it from the Split or Code view of your xml file. This is because when you refactor or modify the id from the Design View, it modifies all other TextViews having the same id.
This is from my own personal experience since I faced what you are facing...
You can set a scope for the Rename refactoring (idea 2020.3). For example, close all tabs and open only activity_main.xml and MainActivity.java files. Then, select id in xml (Refactor -> Rename) and in the Rename popup dialog under "Scope" select "Open Files" in order to rename the id in those files only.

Proguard/R8 do not rename classes used in XML layouts

I need to obfuscate the whole code of an Android library except some classes/methods which will be used by developers.
I succeed except that some classes invoked in my XML layouts have not been renamed and I struggle to find an option in R8/Proguard or a trick to force it, even if I have to update manually or with a script these classes in my XML layouts (I know that R8/Proguard do not edit them itself) thanks to the generated mapping.txt file.
The closer question I found is Proguard (R8) obfuscate custom view names but it did not solve the issue I face, R8/Proguard is still ignoring the rename for these classes :/
If anyone has an idea, you're welcome :)
Thanks for your time and knowledge ;)
[EDIT]
I finally gave up and put placeholders in my XML layouts for my custom views and inflated them at runtime.
It's a shame that Proguard/R8 can not handle custom classes renaming in XML layouts with aapt :/
For info, I ran into issues also with the use of fragment items in my XML layouts where the name property is not renamed while the corresponding class is...
So for these too, I had to put placeholders and inflate them at runtime...
I let the question opened in case someone find a trick one day ;)
There is currently no support for renaming inside XML layouts. As part of the compilation process the aapt2 tool will generate -keep rules for the names present in the XML layouts, so the Android runtime will be able to perform the required reflection for layout inflation.
By adding the following option to the configuration (proguard-rules.pro)
-printconfiguration <somefile>
the full configuration can be inspected including the rules generated by aapt2.

Assign a view ID programmatically in Android

I'm trying to create a RelativeLayout with several children programmatically. To make the rules like RelativeLayout.RIGHT_OF work, the child views must have proper IDs.
However, when I try to assign an ID, Android Studio flags it as an error:
view.setId(123);
ERROR: Expected resource of type id
Found it:
view.setId(View.generateViewId());
You have two options:
This is not a compiler error. It is just editor validation error as this is not a common way to deal with Ids. So compile and run with no problems
Use pre-defined list of Ids with type "id" as this accepted answer https://stackoverflow.com/a/8937477/1657333 so the editor will be happy.
In Android Studio click on lightbulb on line with this 'error'. And select 'Disable inspection' in first submenu.
As others have said, you shouldn't be using Integers directly in this case. There are some very rare cases where you might need to do this or in general suppress validation warnings. If so then you need to look into Lint.
In Android Studio you can click on the red light bulb at the left side of the line of code. This will show a context menu with a list of methods of suppression.

How to rename widget ID's in Eclipse?

I am working on an app built upon an example from a tutorial. Now the different widget IDs no longer reflect their purpose so I would like to rename them. However, this seems quite a task as the IDs are used in multiple files.
Is it possible somehow to rename the IDs so the changes are migrated into the other files in the project? That is pretty much similar to refactor source code names, but for widget IDs.
I don't think a tool like that exists in Eclipse. The easiest way to do it manually is to rename an item in the XML layout and then track down the errors in the Java classes. If you do it one-by-one then you should have it cleaned up in a minute or two.
You can try to use the Find/Replace function is Eclipse. I have found this useful several times when changing ID's or something to that effect. Let us know what you end up doing.
In eclipse:
Go to the xml layout -> Graphical Layout -> Properties then click the ... button near the desired field:
In case anyone stumbles across this problem now, you can rename the ID from the visual layout editor and it will do all the hard work automatically.

Any possibility to get autocompletion / type-ahead / intellisense in Android XML Files (Eclipse)

Is there any way to make Eclipse + Android SDK + ADT Plugin offer some sort of auto-complete in XML files if I hit CTRL+Space when my cursor is in a spot such as the ones marked with an * below.
<LinearLayout id* ... lay*>
The thing is that I think that the above was actually working already directly after the initial install - even though it of course never worked within style files.
<style name="ActionBarWrapper" parent="Fill_Parent.Vertical">
<item name="android:layout_height">36dp</item>
<item name="a*"
</style>
There is an icon in the eclispe toolbar to directly access the "new android xml file" wizard.
You could use IntelliJ IDEA instead of Eclipse, it has full auto-completion and works well for Android development.
http://www.jetbrains.com/idea/
The Community Edition fully supports Android, and is FREE
The latest ADT plugin (r9) does have partial auto complete feature in the layout xml.
for example, if you type ctrl-spacebar while the cursor is between 2 tags, you get the list of all avaliable tags (views and layouts). If your cursor is inside a tag, you get a list of all avaliable attributes.
First of all: Thanks for the good advice everybody. Not sure if the following should be an answer or a comment. Going for the answer because comments do not allow enough characters.
After trying out some eclipse plugins (like Rinzo XML Editor ) I found out that it actually worked for some files while it did not work at all for others. After some testing I am now under the impression that there is a difference between creating XML files via...
New > XML
and
New > Other > Android > Android XML File
I double checked that even when both files have exactly the same content (xml-version, encoding, namespace etc.) they behave differently with regards to auto-code-completion depending on the way of file-creation. I also checked the file properties but could not make out any significant differences. Obviously it must be something local or some meta stuff I am overlooking.
I think the different behavior is because when you create the file using "New->XML" the wizard opens the file using the default associated editor to xml content.
If you create a file using "New->Other->Android->Android XML File" I think the wizards tries to open the xml file using a specific xml editor, instead of the one you have configured as default, in this case Rinzo I guess.
In the second case why don't you try finding the xml file in either "Package Explorer" view or "Navigator" view, right click on the xml file and selecting Rinzo through the "Open With" option.

Categories

Resources