Imports just on API level > 15 - android

I have a App created on API 8. Now I want to make it work with ICS and for that I need additional imports which are not available in API 8.
I want to add following imports:
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
So do I have to make a diffenent app just for API > 15? The name of the App should not change.
Or maybe it is possible to place 2 App versions and make the minSdkVersion and maxSdkVersion according to the API level into Google Play?
How do you handle that?

I have a App created on API 8.
Great!
Now I want to make it work with ICS and for that I need additional imports which are not available in API 8.
No problem! Since import statements are applied at compile time, so long as you set your project's build target (e.g., Project > Properties > Android) to API Level 14 or higher, your code will compile fine.
So do I have to make a diffenent app just for API > 15?
No. Just use version guard blocks to ensure that you do not try using the newer code on older devices:
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// do stuff with CalendarContract
}
Or maybe it is possible to place 2 App versions and make the minSdkVersion and maxSdkVersion according to the API level into Google Play?
That should not be necessary.

You want to add a compatibility shim to your existing app so that it works on lower API levels and is still able to access API's from higher levels.
In general, the way to do this is to isolate the code that uses the higher-level API's in classes that are loaded at run-time via reflection, only if the API level supports them.

Related

Realm using try-with-resources

Android studio give me message that i can use try with automatic resource management.
But the docs says
If you’re working on an app with minSdkVersion >= 19 and Java >= 7,
then you can use try-with-resources:
My project have minSdkVersion 15 and Java 1.8.
Do i need to ignore this message or to use try-with-resources?
You can use it.
Docs say the following:
In addition to the Java 8 language features and APIs above, Android Studio 3.0 and later extends support for try-with-resources to all Android API levels.
Sidenote, please drop support for anything below api 19.

vague concept about appcompatv7 in android

appcompat(v7) library gets add up automatically when you are using a minimum API level for your application development. Why am I getting appcompat with API 11? I mean if its already included in API level 11 why does it add up then. Can and should I remove it?

How to support lowest possible API version?

I'm developing a Android library and I want to support as many API versions as possible. I have stumbled upon a problem with AsyncTask and found an answer here on SO. The proposed code to use is:
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
task.execute(params);
} else {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
}
My question is, how do I include the proposed code AND support lowest possible API level? What API version should I reference? What should I write in the uses-sdk tag inte manifest?
Since the field THREAD_POOL_EXECUTOR in AsyncTask is only available from API level 11. Can this code be compiled to a lower level?
Thanks!
Assume that you line below exists in you manifest
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
This means that you are using features from API-17 but to ensure backwards compatibility your application may start on minimum API-8 (Froyo).
According to your example, using THREAD_POOL_EXECUTOR for API-17 or lower is OK. And running your code with Froyo device is OK too. Because THREAD_POOL_EXECUTOR field will not be used in this case.
In the manifest set android:minSdkVersion="minimumApiYouNeed", this is the lowest api you want to support, and the android:targetSdkVersion="maximumApi". This is the api that will be used to compile the code. This way you will be able to do things like what you wrote there, if you ever write something that is not supported by the minimum api, the editor will notify you, but it will work well if you do the checking it will work well
You will have to use API level 11 or higher unless you can find a library that works on an earlier API level that provides the THREAD_POOL_EXECUTOR implementation. Also, check to see if Google provides any backports or support libraries that would allow this to work before API 11.
This supports Android back to 2.1 (sdk version 7), but compiles the code against sdk version 17 (HoneyComb). You would have to add that tag to your manifest, of course.
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
Your project.properties should include this line:
# Project target.
target=Google Inc.:Google APIs:17

Regarding Minimum Required SDK , Target SDK , Compile with options in Android

Can anyone explain regarding the Minimum Required SDK, Target SDK , Compile with options while creating an application.
If i set the minimum required SDK as API 8, Target SDK as API 16 and Compile with API 17,
will it work on Froyo devices in adroid?
If i want to use the methods introduced in API 16 or Library uses API 16, and want my app
to work on Froyo or ICS devices, how can i achieve this?
Thanks in advance.
You can use API-Level 16 methods only on devices that are Level 16 and higher. But you can check in your app and only call them when this is the case. Look into my small test-app which uses API-11-methods and runs from API-3 and up.
http://code.google.com/p/android-change-log/source/browse/trunk/src/sheetrock/panda/changelog/ChangeLog.java
Look at lines 40-41, 144-145 and 324-341. You don't need any third party libraries for this, but you need to put your higher API code in a separate class (lines 324-341).
Yes. But be careful not to include API higher than Froyo in your application
From my understanding, you can't, unless you use third party libraries. There are useful libraries out there which help you realise that:ViewPager and ActionBarSherlock.
Yes ofcourse for first question. If you use like this.
android:minSdkVersion="8"
android:targetSdkVersion="16"
For second. you can go for third party library if you import it, surely will work.For eg:
special features introduced in android 4.0 with tabs and swipe.But to overcome that
actionbar sherlock library been introduced which support in all version and in github.
Hope it helps you.

Android - Fragment API for API level < 11

I have came across a problem for dynamic forms in my app, which is suited for Android 2.1 and above. I know there is new Fragment API since API level 11 (Android 3.0 Honeycomb), but also I have read an article - http://android-developers.blogspot.com/2011/03/fragments-for-all.html stating Fragment Api is available also for API level lower then 11 in, so called, Compatiblity package. I have installed it via SDK, but I am not able to use is in my App, e.g. I cannot import android.app.FragmentManager, application doesn't know it.
Do you know, how to solve it? Is Fragment API truely available for older API levels? If so, how to make them going? Or is there any other solution like Fragments API? I will need for dynamic generated forms if possible
Thanks
Hmyzak
Android Studio:
Add a dependency for support compatibility package v4:
dependencies {
...
compile 'com.android.support:support-v4:21.0.+'
...
}
and then use import android.support.v4.app.Fragment; instead of import android.app.Fragment; in imports.
You need to add the package to the build path.
Use ActionBarSherlock. It comes with a lot of working examples.

Categories

Resources