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:



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

 

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
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?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…