I don't Understand Call or Class Requires API Level 3 - android

I'm doing some Android work in IntelliJ 13. I've created an Android test application so I can start writing unit tests. IntelliJ is reporting an error I haven't seen before and this error does not prevent the code from working:
Call requires API level 3 (current min is 1)
It will also have an error at the class level that is:
Class requires API level 3 (current min is 1)
And sometimes it will say level 11.

You need to set min SDK version in AndroidManifest like
<uses-sdk android:targetSdkVersion="18" android:minSdkVersion="11"/>

As Josef said, you need to increase the minimum SDK version specified in your AndroidManifest.xml file - this is the minimum Android API version you support. The error messages you're getting ("requires API level 11") mean that the method or class you're trying to use was only introduced in API 11. There are ways around this - specifically, the support library is an official back-port of many newer features, included in your SDK.
Have a look at the Android Dashboards to choose a minimum API level (and hence your device/userbase coverage) - typically 8 or 10, if you plan on supporting "older" devices (Android 2.2 and 2.3 devices), and 14 or 15 if you plan on supporting only newer (Android 4.x) devices.
Generally you'd specify your targetSdkVersion to be the latest version of Android - at the moment, this is 19. You need your SDK up-to-date, though.
The developer documentation has an in-depth explanation of what these things mean - I'd recommend having a read.

If after making above changes you still have an issue, please use File > Invalidate Caches / Restart. Here it explains what it does. In case you don't read that, summary warning:
Cleaning system caches also results in clearing the local history. To
avoid losing data, check in the changes to your version control system
before invalidating caches.

Related

Firemonkey: can we update targetSdkVersion in AndroidManifest.template.xml?

I read that berlin is compatible with marshmallow, but i see that by default in the AndroidManifest.template.xml we have :
<uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="%targetSdkVersion%" />
i don't know where delphi took the variable to update %targetSdkVersion% but it's seam to be all the time 14 and i don't see any way to configure it
i would like to replace %targetSdkVersion% by 23 (marshmallow api level, to support the app permissions), but is it safe to do so or it's will introduce many other bug ?
FireMonkey was developed to work against a certain range of Android functionality. As you can see from the RAD Studio Platform Status page FireMonkey apps built with Delphi 10.1 Berlin have a lowest supported Android version of 4.0.3, which corresponds to Android API Level 15.
The minSdkVersion field is supposed to be set to the earliest Android version your apps will work with so in truth this should probably be set to 15 but actually is set to 14 (Android 4.0-4.0.2).
If you look back at an Android manifest file generated by Delphi XE7, which supported Android 2.3.3 (API Level 10) it specifies a min SDK version of 9 (Android 2.3-2.3.2), which is the version of Android that introduced the NativeActivity type underlying every Delphi FireMonkey Android app. Again, this seems a little bit out of kilter with what is documented as lowest supported version.
Anyway, minSdkVersion can be used by Google Play store to filter your app out of the listings for someone running a lower version of Android. It is also checked when you install an app on a device; Android won't let you install on a lower version of Android.
targetSdkVersion, on the other hand, indicates what version of Android your app has been tested with and works sensibly with. It can often be higher than minSdkVersion if your your app needs to use features introduced in Android versions later than minSdkVersion.
If you want to use a feature from API Level 23 then sure, you'll need to update that manifest part. Just remove the %targetSdkVersion% template from the Android manifest template file and replace it with the required version.
Problems that you might run into:
You'll either need to check the Android version and, if lower than your targetSdkVersion, not use those features that aren't available or set minSdkVersion to a suitably higher version to ensure the app can only run on devices that have the features you wish to use.
FireMonkey code not aware of differing behaviour in API Levels may
function adversely. In your case you may get issues because of the
different runtime permissions behaviour enabled in API Level 23.
Actually I can't currently think of any more issues, though a good amount of testing is recommended. You might have more to think about in a regular Android app using visual activities and so on, when different API levels may affect the theming or other UI elements. You can see the various things that change when you target target SDK versions on this Android documentation page.
By the way, the use of the SDK Manager in the Tools. Options... dialog is entirely irrelevant to the question of how to update the value in the generated manifest file. The only evident way to change it is to manually edit the Android manifest template file as per the documentation, and mentioned in a comment.
The only relevance of the SDK Manager is that the default Android SDK installation for Delphi 10.1 Berlin installs the Platform libraries for API Level 22. If you want to use a feature from API Level 23 you might think that updating those platform libraries might be necessary, but of course if you're doing the coding in Delphi then you're (presumably) actually just compiling against import definitions of the features in that higher API level, and so whether or not those features are contained in the android.jar file is of no consequence.**
** I'm happy to be proved wrong on this count, but I have never observed a connection between the manifest and what the SDK Manager is set up against.

Choosing SDK version to reach maximum number of people

I am a complete beginner in Android Development and just downloaded Android SDK to integrate it with Eclipse.
However, I don't know which Android version I should select in SDK Manager to allow a maximum number of people to download and use my app.
Here's a screenshot of the manager that is showing up on my screen.
Any kind of help would be really appreciated.
Thanks.
In fact, you don't need to download an OLD SDK just to allow OLD Devices to use your app.
This is done in your project settings via target SDK version and min API level(AndroidManifest.xml or build.gradle).
Define Audience
First, define the audience to your app.
HERE you can find the market share for every Android Version (this can change for each country but it may help).
If you check that table, you can see that only few devices are still using API 15 or older.
So, if you support API<15, you will only reach ~3% more people. If you can support them without lose any feature, good (not all android features are supported/ported to all versions). But if you may need to disable a feature (like that special Floating Action Button that you created) just to reach those devices, I think it does not worth.
So, based on that table, you define the audience
In my case for example, I like to build apps for devices with API from API_16 to API_24.
Usually, we always want to use maximum API available. Today, 24.
This way, I could reach ~97% of the people using Android.
But this is optional... I could select min API as 13... or 14.. or 4.. This is up to you..
After defining the audience
Target SDK
I usually set the target API according to the MAX API I want to support. So, for example, if the MAX API I want to support is API_24, this will be my target API and will download the SDK v24 (7.0).
Usually, you always will have the latest SDK installed since you always want to reach latest android version and enjoy all the new features.
MIN API
Now, you also have defined the min API version that you want to support. So, lets use API 16 for example (first JB version).
You will set this in your project file (AndroidManifest.xml). You don't need to download SDK 16 just because of that.
Setting Eclipse
As I said, min SDK will be set in your project. That would be done in your Android Manifest
Here, you can see that targetSdk is 24. It means that I just need to download SDK v24 to build this project. Even then, I'll be able to install this App in devices with API 16 or later.
Eclipse (AndroidManifest.xml)
<manifest>
....
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="24" />
...
</manifest>
Android Studio (build.gradle)
compileSdkVersion 24
buildToolsVersion "24.0.0"
minSdkVersion "16"
targetSdkVersion "24"
minSdkVersion Does Not Guarantee that your app will work
Note that settings above only allow your app to run in devices with API>=16 or API<=24. However, your app may crash or does not work in all devices within that range. This usually happens because some View Components or Methods were added or removed according to API version.
For example, Fragment was added only on API11. So, if you try to install your app in a phone with APIv11, it will work. However, if you install it in a device with API4, it will crash.
Another example is method Fragment.onAttach(Context context) which was added only in API 23.
That's why we use Support Library
Support Design Library it is a library created by Google which enables the developer to use features from new Android Versions in old Android versions.
So, for example, to add a Fragment which is compatible with devices with API<11, you should import and use Fragment class from package android.support.v4.app.Fragment instead of default version included in SDK (android.app.Fragment).
This way, your app wont crash.
Of course this is very basic example. It is just to illustrate...
Anyway, I hope I could help you
Regards

Compiling with Android 4.X but supporting API Level 9

I'm working on an application which uses ActionBarSherlock. As it's documentation points out:
[...] the library requires that both it and your project are
compiled with Android 4.0 or newer. The project also requires that
you are compiling with JDK 1.6 in both your editor and any build
systems that you may be using.
So, that means I'll compile my application (and the library) against Android 4.X but in my Manifest, I declare that I'm targeting (e.g.) API Level 9.
This all works fine and well but there is something that disturbs me. From the FAQ:
What API level should I target in my manifest when using the library?
Targetting API level 11 or newer is required as it will cause Android
to automatically add the native action bar when run on newer devices.
Since you will be compiling against new APIs but your app will likely
be run on devices with older versions of Android extra care must be
taken to either avoid using or properly check and call any methods
that were introduced after your minimum SDK version.
That means, that I'll have to manually check every method call, so I don't use any that are not available in my targeted API Level (9 in my case)? This sounds wrong to me.
Is there a way to tell my IDE (IntelliJ), that I'm only using the API Level 9 (so I don't get any auto-completion for non-existing methods/classes and don't use them by accident) and then choose to compile it against another Android version?
Or can I use some automated checks (which run at compile time) to check for that?
The ADT's lint feature should take care of this by warning when API calls are being made for the wrong API version.
You should be compiling both ABS and your project with the latest SDK available (at present, 4.1). Your manifest should have a targetSdkVersion as high as possible (ideally matching your compilation SDK) and your minSdkVersion should be set to the lowest version you support.
Lint is partially integrated with IntelliJ IDEA and is also available as a command line tool.
You temporarily set your target SDK to the various lower ones and debug with it. Your final build then is with the latest SDK.
Set a Build target similar to that you have mentioned in your manifest.
as always , you should set the targetSdk to the maximum available on both the manifest and the project.properties file (as recommended by google on one of their videos) , so that the ADT&SDK would be able to optimize the ADK accordingly.
set the minSdk to the one that you wish to support your app from , and let Lint to tell you if there are any problems in case you use too-new-features.

How to support devices 1.6 and up?

I've just started developing android apps and I am wondering about this right from the start.
Apparently it doesn't matter if you work with API 13 (3.2), you can still support Android 1.6. Is that true?
For AdMob to work, I had to start working with API 13 and have set
target=android-13
Someone please clarify this or point me somewhere to read it.
Apparently it doesn't matter if you work with API 13 (3.2), you can still support Android 1.6. Is that true?
That depends on what you mean by "work with API 13".
Having a build target of API Level 13, as you mention later in your question, simply means that is the version of the Android SDK you are compiling against. You will be able to reference classes and methods that exist on API Level 13 (and earlier). Your app can still run on any version of Android you want.
However:
You cannot call methods that do not exist in API Level 13 when you are not running on an API Level 13 device (and likewise for API Level 12, 11, 10, etc.).
If you wish to support Android 1.x, you cannot even reference classes or methods that do not exist on the device from a class that gets loaded by the VM.
In either of those cases, you will get a VerifyError, and your app will go "boom".
There are strategies for conditionally using newer-API stuff that allow you to avoid trying to use that stuff on older-API devices. Or, just stick to older-API stuff in the first place, even though you won't get compiler assistance to let you know when you are accidentally using newer-API stuff, given that you are using the newer build target.
Or, contact AdMob support and figure out why they need that build target.
In your Manifest file you can put the min sdk version you want to support and your target sdk version, they could be different, to support 1.6 specify android:minSdkVersion="4" as follows
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="13" />

Making an Android 2.2 app work on 1.6

I have written a simple app for Android and when I first created the project I selected v2.2 for the build target. I have found out that my app does not work on v1.6, it runs for a few seconds then crashes.
So I guess I am using some commands that were not implemented in 1.6. So I need to find out what these commands are and find the 1.6 equivalent. Should not be too hard I guess.
My real question is; to make a 1.6v is the only way to do this to create a new project and set the build target to 1.6 then copy and paste the source code and re-compile? Or is there an easier way? ie can I take my existing 2.2 build and then tell it to compile to 1.6?
Many thanks for the help.
The rule is that you always build with the latest SDK, but use the android:minSdkVersion value of the <uses-sdk> attribute to indicate the lowest API level your app can support.
Note that this attribute is only a clue to the installer and the Market... setting it to 4 (for 1.6) won't mean your app can magically run on 1.6. It's up to you to devise nice fallbacks if more modern APIs are unavailable (or raise your minSdkVersion appropriately if the app cant function without them).
Assuming your app can run on 1.6 without 2.x APIs, then you need to be sure that your 2.x API usage is done from a class that won't get loaded on 2.x. Here's an example:
import android.view.Display;
// Class for wrapping APIs only available in
// API level 8 and up. (i.e. 2.2, a.k.a. Froyo)
class ApiLevel8Only {
public static int getDisplayRotation(Display display) {
return display.getRotation();
}
}
That getRotation() call will cause a force-close on 1.6. To avoid this you inspect the current version before your API-wrapping class is referenced:
if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
ApiLevel8Only.getDisplayRotation(...);
}
Hope that makes sense.
All you have to do is set the minsdk value in your manifest. This sets 1.5 as your minsdk, and 1.6 as your target, for example:
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="4"/>.
Versioning your app.
android:minSdkVersion — The minimum version of the Android platform on which the application will run, specified by the platform's API Level identifier.
So you set this to the API # for 1.5 or 1.6 and you're app will run on those older versions of Android. But you MUST check for API calls to newer version of Android. You can't use 2.2 features in 1.5, etc.
So you can set your target to Android 2.2, and have your minsdk set to Android 1.5. You can then use Build.VERSION to determine what version of Android the user is using to avoid using newer API calls in older versions of Android.

Categories

Resources