Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To target multiple platforms, you specify a minimum SDK level, and a target SDK level when building the project. You configure the project to link against the target SDK build. The Davlik VM will generate runtime exceptions if you ever execute a call against a method that's not present on the current platform. The compiler will generate warnings for calls that not available on the minimum SDK level. There are various Java @ declarations that suppress those warnings on classes, methods, or particular references. Press the F2 key in Eclipse to auto-generate the Java decoration that's appropriate. It sounds intimidating. But in practice, the warnings allow you to code relatively fearlessly.</p> <p>So the general approach is: link against the target SDK libraries; add @ decorations to code that uses methods from higher SDK builds; decorate the methods or classes that are specific to a particular build; and live and die by the "all warnings are errors" rule of coding.</p> <p>Re-implementing an entire activity for each API level is overkill. Class specialization with overriden methods is not a happy way to do this kind of thing. The binding complexity makes it difficult to work this way. Far better to use a smaller helper class or conditional code.</p> <p>There are various conventions on writing Helper classes -- essentially a separate implementation class for each API level, each implementing a common interface or abstract base class. The method given in the Android developer blog, is a reasonably tidy way to do it. There are plenty of minor variation on the pattern, all based on obtaining an instance of a helper class for a particular API level. </p> <p>Another example of this pattern would be the IActionBarHelper interface used in the google ActionBarCompat libary. In that case, your Activities inherit from ActivityCompat, instead of Activity, and the instance method ActivityCompat.getActionBarHelper() returns an implementation of IActionBarHelper that provides an appropriate implementation for the platform you're running on. In this case, the implementations contain a fair bit of initialization state, and state associated with the activity; so it makes sense to obtain the implementation from a member method.</p> <p>Despite all that, the Davlik VM is perfectly capable of dealing with inline methods that don't have linkage.</p> <pre><code> if (Build.Version &gt;= 10) { Call an API 10 method. } </code></pre> <p>also works perfectly well. The Davlik VM does whine a little bit via debug messages in LogCat. So there might be some overhead if you were to do this all the time. If you were to execute the API-10 method on a downlevel platform, the Davlik loader generates code that throws a runtime exception when executed instead of executing the call. But the code links and loads fine. The if statement is all you need to prevent execution of the missing method. In small doses, conditional code is perfectly ok. If there's a lot of platform-specific code, it's probably better to encapsulate platform-specific functionaily in a class, somehow.</p> <p>For features which are either there, or not there, like your NFC example, the best way to proceed is probably to implement as much of the NFC functionality in a class; and then conditionally reference that class from code in your main class or activity. e.g.:</p> <pre><code> void onCreate(...) { if (Build.Version &gt;= 10) { mNfcReceiver = new NfcReceiver(getContext()); } } </code></pre> <p>The Helper class pattern doesn't really help you much, since you're obviously not going to reimplement the functionality on downlevel platforms.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload