Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1026175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:02:32+00:00 2026-05-16T12:02:32+00:00

Is there a simple line of code that would allow only loading the code

  • 0

Is there a simple line of code that would allow only loading the code if the OS version meets the requirements?

Lets say I have my target OS as 2.2 but the min sdk is 3 for android 1.5 so even if i have some code in my project that isn’t compatable with 1.5 it will still compile since the target OS is 2.2. Anyway, I want to ad a feature that requires code that’s not in the 1.5 SDK and will cause a crash if it’s loaded on a 1.5 phone. Is there a simple thing like this that I can do? So i dont have to make the entire app not available to 1.5 users?

 if (Android OS == >2.1){
            //Insert code here that requires 2.1 and up}
        else{
            //insert code that would appear is OS is <2.1}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:02:33+00:00Added an answer on May 16, 2026 at 12:02 pm

    Yes, you can do that. In fact there is more than one way. (Note: the only Android specific part of this answer is the way that you find out the platform version.)

    Suppose that class X has method void y() in version 2.0 onwards, but not before.

    One way to invoke this method with out introducing any compile time dependencies whatsoever is to use reflection to locate the Method and call invoke on it. For example:

    X x = ...
    if (BUILD.VERSION.RELEASE.compareTo("2.0") >= 0) {
        // (exception handling omitted ...)
        Method m = c.getClass().getDeclaredMethod("y");
        m.invoke(x);
    }
    

    Another way is to create a version compatibility adapter API for your application like this:

    /** Version compatibility adapter API */
    interface Compat {
        void doY();
    }
    
    /** Adapter class for version 1 */
    class CompatV1 {
        public void y(X x) {
           // do nothing
        }
    }
    
    /** Adapter class for version 2 */
    class CompatV2 {
        public void y(X x) {
           x.y();
        }
    }
    
    // 
    // Code to instantiate the relevant adapter for the current platform.
    //
    Class<?> compatClass;
    // (Exception handling omitted)
    if (BUILD.VERSION.RELEASE.compareTo("2.0") < 0) {
        compatClass = Class.forName("...CompatV1");
    } else {
        compatClass = Class.forName("...CompatV2");
    }
    // (Exception handling omitted)
    Compat compat = (Compat) compatClass.newInstance();
    
    // The adapter object can be passed around as a parameter, wrapped
    // as a singleton or injected using dependency injection.
    
    // Invoke X.y() as follows:
    
    X x = ...
    compat.y(x);
    

    The second version looks a bit heavyweight, but it has the advantages that the dynamic (slow, non-type-safe) code is executed just once, and that the version specific code is isolated from the rest of the code. In real life, you would probably put a number of methods into the adapter interface.

    This approach requires a bit more thought, to work out how to design the compatibility API so that it cleanly isolates the version dependencies from the rest of the code. You might also to have to revise the adapter API, and create new adapter classes for each new (incompatible) major release.

    Finally, if the platform API changes that you need to adapt to entail using classes or methods in the older version that are removed in the newer version, then you will need to compile your various adapter classes (e.g. the CompatV* classes) using different Android SDKs. This will make your build processes rather more complicated.


    For other “takes” on this problem, read the following articles on the Android Blog:

    • Backward compatibility for Android applications
    • How to have your (Cup-)cake and eat it too.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Can you prevent me from switching to another channel on… May 16, 2026 at 1:57 pm
  • Editorial Team
    Editorial Team added an answer Rather than use a custom control you could take the… May 16, 2026 at 1:57 pm
  • Editorial Team
    Editorial Team added an answer I received this same error when trying to import data… May 16, 2026 at 1:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm dabbling with pChart and would like to start with a simple line graph
I wrote this simple test code, by adapting a piece from a book, to
I've written an experimental function evaluator that allows me to bind simple functions together
I had this simple modal contact form working fine but must have broke it
I have an existing site that is working well with Authlogic login. I'm trying
I am attempting to make use of the Twisted.Web framework. Notice the three line
An open source JavaScript project I work on includes code: if (color) { tapeDiv.style.backgroundColor
Consider a simple setup: // _conn is the OdbcConnection with a MySQL-Server (MySQL-Connector 3.51)
I have been trying to register 3 hotkeys. I followed this example (or this
I am writing a simple unit test harness in powershell I designed the harness

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.