Backwards Compatibility

Put your problem here if it does not fit any of the other categories.

Backwards Compatibility

Postby PaulFake » Sun Dec 13, 2009 12:24 pm

Hello,

So the new SDK 5 release presented a new ContactsContract library that I want to use. I also want to keep my app compatible with SDK 4. I am having trouble accomplishing both. The problem is that even if the code path does not get to SDK-5-only code in an SDK 4 run, the class that uses the SDK-5-only code crashes immediately before it gets to onCreate when running on SDK 4, because Java is unable to compile the code for the newer SDK (that one actually took me awhile to realize). So, I started thinking about conditional compilation, and I found this nice workaround:

http://www.devx.com/tips/Tip/13342

However, it doesn't seem to be working for me. Here is what I am trying:



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. interface SDKVersionChecker
  5.  
  6. {
  7.  
  8.         public static final boolean IS_SDK_5_COMPATIBLE = Build.VERSION.SDK_INT >= 5;
  9.  
  10. }
  11.  
  12.  
  13.  
  14. public class FriendListScreen extends Activity implements SDKVersionChecker
  15.  
  16. {  
  17.  
  18.         @Override
  19.  
  20.         public void onCreate(Bundle savedInstanceState)
  21.  
  22.         {
  23.  
  24.                 super.onCreate(savedInstanceState);
  25.  
  26.  
  27.  
  28.                 if(SDKVersionChecker.IS_SDK_5_COMPATIBLE)
  29.  
  30.                 {
  31.  
  32.                         //do my sdk 5 stuff
  33.  
  34.                 }
  35.  
  36.  
  37.  
  38.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


According to the link above, the compiler should just completely ignore the SDK 5 stuff if IS_SDK_5_COMPATIBLE is false. Now for the weirdest part. If I do this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. interface SDKVersionChecker
  5.  
  6. {
  7.  
  8.         public static final boolean IS_SDK_5_COMPATIBLE = false;
  9.  
  10. }
  11.  
  12.  
  13.  
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


then everything works just fine in SDK 4. I'm guessing there is something going on here that is way over my head, so if anyone can make sense of any of this, please tell me what's happening and how I can properly make my code backwards compatible.
PaulFake
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Aug 26, 2009 1:05 am

Top

Postby PaulFake » Mon Dec 14, 2009 12:17 am

Well as it turns out there is an easy way to do it. All I had to do was wrap the SDK 5 defines in a separate class. That way, it only gets loaded when it is needed, and it is only needed if I'm using SDK 5.
PaulFake
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Aug 26, 2009 1:05 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests