Android Tutorial Compiler Warnings

Starting from v2.70, Basic4android includes a warning engine. The purpose of the warning engine is to find potential programming mistakes as soon as possible.

The warning engine runs whenever you compile the project, when clicking on Project -> Test Compilation or when you save the project.

The compile time warnings appear above the logs and in the code itself:

SS-2013-05-06_20.16.31.png


Clicking on the warning will take you to the relevant code.

Ignoring warnings

You, as the developer, can choose to ignore any warning. Adding an "ignore" comment will remove all the warnings for that specific line:
B4X:
Sub UnusedSub(a As Int) 'ignore

You can also disable warning from a specific type in the module by adding the #IgnoreWarning attribute.
For example to disable warnings #10 and #12:
B4X:
#IgnoreWarnings: 10, 12

Runtime warnings

Some of the warnings are only checked at runtime. These warnings will appear in the regular logs.
Runtime warnings are only checked in Debug mode.

List of warnings

1: Unreachable code detected.
2: Not all code paths return a value.
3: Return type (in Sub signature) should be set explicitly.
4: Return value is missing. Default value will be used instead.
5: Variable declaration type is missing. String type will be used.
6: The following value misses screen units ('dip' or %x / %y): {1}.
7: Object converted to String. This is probably a programming mistake.
8: Undeclared variable '{1}'.
9: Unused variable '{1}'.
10: Variable '{1}' is never assigned any value.
11: Variable '{1}' was not initialized.
12: Sub '{1}' is not used.
13: Variable '{1}' should be declared in Sub Process_Globals.
14: File '{1}' in Files folder was not added to the Files tab.\nYou should either delete it or add it to the project.\nYou can choose Tools - Clean unused files.
15: File '{1}' is not used.
16: Layout file '{1}' is not used. Are you missing a call to Activity.LoadLayout?
17: File '{1}' is missing from the Files tab.
18: TextSize value should not be scaled as it is scaled internally.
19: Empty Catch block. You should at least add Log(LastException.Message).
20: View '{1}' was added with the designer. You should not initialize it.
21: Cannot access view's dimension before it is added to its parent.
22: Types do not match.
23: Modal dialogs are not allowed in Sub Activity_Pause. It will be ignored.
24: Accessing fields from other modules in Sub Process_Globals can be dangerous as the initialization order is not deterministic.
25: Sub '{1}' not found.

'Runtime warnings
1001: Panel.LoadLayout should only be called after the panel was added to its parent.
1002: The same object was added to the list. You should call Dim again to create a new object.
1003: Object was already initialized.
1004: FullScreen or IncludeTitle properties in layout file do not match the activity attributes settings.
 

Reviewnow

Active Member
Licensed User
Longtime User
10: Variable '{1}' is never assigned any value.

Erel,

I did find some false positives with this error where variable actually does have an assigned value..

So happy to see this new addition

Do you have a specific forum where to place bugs for this beta?

Thanks again
 

corwin42

Expert
Licensed User
Longtime User
If the variable gets only assigned values but is never used anywhere it is useless.

Edit: Sorry I thought you are talking about "Unused variable '{1}'.".

Are you sure that there is a value assigned to the variable before it is used the first time?
 

Dave O

Well-Known Member
Licensed User
Longtime User
Love the warnings, but would like to see one change:

For undeclared variables, I think it would be good to add an option to make this an error rather than just a warning.

For those of us who *always* declare our variables, it's sometimes hard to spot bugs because the default B4A behaviour is to allow undeclared variables. I don't check the warnings on each compile (mostly because I have lots of warnings and they get lost in the shuffle), so it takes me a while to realise there's a rogue variable running around.

If there was an option for stricter declarations (e.g. "Don't allow undeclared variables"), that would (rightly) stop me in my tracks and give me a bit more confidence in my code.

Hope this helps!
 

Dave O

Well-Known Member
Licensed User
Longtime User
Good question. Two reasons:

I am using some third-party classes that generate a lot of warnings. Is there an easy way to say "ignore warnings for this entire file"?

I have a lot of icon files that are either:

  • Included in the file list, but referenced by building the name programmatically
  • Not added to the file list, but used in the code.

Should I be adding all referenced files to the file list (in the designer)? Does this make a functional difference, or is it just a matter of keeping everything in good order? (The app seems to work either way.)

Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use the #IgnoreWarnings attribute in these classes to remove the warnings.

Included in the file list, but referenced by building the name programmatically
You should add the IgnoreWarnings attribute with attribute number 15.

Not added to the file list, but used in the code.
Just add the files to the Files tab.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Warning #2 means that the compiler found that there is at least one code path that doesn't return a value.
For example:
B4X:
Sub S1 (b As Boolean) As Int
 If b then
   Return 3
 End If
End If

There are cases where the compiler may not be correct. For example if you know that b is always True in the above code. The compiler doesn't have this information.

In that case you should add a 'ignore comment to the sub.
 

Anawratha

Member
Licensed User
Longtime User
Good Morning,
I'm now using B4A Version 4.30 and get two new compiler warnings:
1.: 'Add android:targetSdkVersion="14" to the manifest editor. (warning #26)'
=> If I add 'SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")' to the manifest, the compiler gives an error that 'Theme.Holo' cannot be found. What's wrong?
2.: 'Add SetApplicationAttribute(android:theme, "@android:style/Theme.Holo") to the manifest editor. (warning #28)'
=> What line will I have to write into the manifest editor (android:targetSdkVersion="14" does not work, other constructions also failed)?
I can't find a description for this warnings.Thank's for your answers...
 
Last edited:

lemonisdead

Well-Known Member
Licensed User
Longtime User
Good Morning,
Hello,
When you create a new project, the default Manifest contains :
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
 

Anawratha

Member
Licensed User
Longtime User
Hello,
When you create a new project, the default Manifest contains :
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
Thank's for your advice!
The problem with the targetSdkVersion 14 is solved now (...but I still do not really now what it means for my app...).
The theme-problem remains. When I fill in the line and start 'compile and run' I get the following error:
...
Generating R file. Error
AndroidManifest.xml:21: error: Error: No resource found that matches the given name (at 'theme' with value '@android:style/Theme.Holo').
...
When I delete the line 'SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")' everything works fine.
Any solution for this problem?
Btw: my default manifest did not contain the mentioned lines. Here is my default manifest (before I changesd anything!):
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4"/>
<supports-screens android:largeScreens="true"
  android:normalScreens="true"
  android:smallScreens="true"
  android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
 

palmaf

Member
Licensed User
Longtime User
I used the IIF function as described in the forum, but now compile i from this alert : Return type (Sub signature) should be set explicitly. How to correct ? Thanks
 

Dave O

Well-Known Member
Licensed User
Longtime User
The warnings are a great feature (especially when I forget "dip" units).

I just noticed that the current app I'm working on doesn't use some of the libraries that are ticked (because I derived it from a more complex app).

Would it be possible to add a warning for libraries that are ticked but not used?
 
Top