Android Tutorial Compiler Warnings

Discussion in 'Tutorials & Examples' started by Erel, May 6, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:

    [​IMG]

    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:
    Code:
    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:
    Code:
    #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

     
  2. Reviewnow

    Reviewnow Active Member Licensed 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
     
  3. corwin42

    corwin42 Expert Licensed 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?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Please post bugs in the "bugs & wishlist" forum. You can add [beta] in the thread title.
     
  5. Dave O

    Dave O Active Member Licensed 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!
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Why do you have lots of warnings? This is the problem...
     
  7. Dave O

    Dave O Active Member Licensed 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!
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    You can use the #IgnoreWarnings attribute in these classes to remove the warnings.

    You should add the IgnoreWarnings attribute with attribute number 15.

    Just add the files to the Files tab.
     
  9. Rick Harris

    Rick Harris Well-Known Member Licensed User

    Kindly add the ability to switch-off warnings entirely. The warnings are useful, but the many (dark yellow) marker bars in the IDE scroll bar make it difficult to find the regular (grey) scroll marker.
     
    Phayao likes this.
  10. Erel

    Erel Administrator Staff Member Licensed User

    You can add this line to your modules:
    #IgnoreWarnings: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25

    Though it is better to understand why there are warnings and fix them...
     
  11. Rick Harris

    Rick Harris Well-Known Member Licensed User

    Thank you. I have taken a closer look at why these warnings occur. I was too hasty with my comment. All warnings are indeed there for a reason. Some of them were really an eye-opener to me, others are less relevant and some I still don't understand why they occur, like warning #2: "Not all code paths return a value". There is nothing wrong with the sub itself that is called hundreds of times from many places throughout my app. It is impossible to find the culpret.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Warning #2 means that the compiler found that there is at least one code path that doesn't return a value.
    For example:
    Code:
    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.
     
  13. Anawratha

    Anawratha Member Licensed 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: Feb 14, 2015
  14. lemonisdead

    lemonisdead Well-Known Member Licensed User

    Hello,
    When you create a new project, the default Manifest contains :
    Code:
    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")
     
  15. Anawratha

    Anawratha Member Licensed 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!):
    Code:
    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$")
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    Yes, install the latest version of Android SDK and set the IDE to use it (android.jar under Tools - Configure Paths).
     
  17. palmaf

    palmaf Member Licensed 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
     
  18. Anawratha

    Anawratha Member Licensed User

    Thank you, Erel!!
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this and post the sub code.
     
  20. Dave O

    Dave O Active Member Licensed 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?
     
Loading...