How to setup android libraries to support different android versions - android

I have developed an application for Android in Kotlin. I was assuming that Android studio is taking care of the compatibility of the used libraries and the code once I setup the minimum targetted Android version.
I am getting responses that the app is crashing with noSuchMethodError, which is linked to a listview's .getView() methode.
I am running the app on Android 8, but these errors are reported on Android 6 and 7 randomly.
I was reading different articles about this failure and I am having the feeling that i might need to setup the libraries setting differently in Studio.

In your build.gradle you can specify a minSdk and a targetSdk, while the targetSdk specifies as the name says the target audience and is in general the newest version e.G. 29, your minSdk takes care of your minimum supported version.
So if you have minSDK 19, Android Studio will show Warnings in your code whenever a method or class is not compatible with devices running sdk 19.
Anyway this might not solve your "noSuchMethodError".

Related

New Android App ( Xamarin) targeting Android 9.0 api not compiling because missing Android X assemblies (but they need Android 11.0 version)

I am trying to build an Android app to make a simple prototype. Using Microsoft Visual Studio 2022 (v17.2.4), I create an Android App (Xamarin), blank app. And I target Android 9.0 version.
But, I have compilation errors :
Could not find 2 Android X assemblies, make sure to install the following NuGet packages:
Xamarin.AndroidX.Lifecycle.LiveData
Xamarin.AndroidX.Browser
Problem is that I can't install these NuGet packages because they need Android 12.0 version or greater :
Package Xamarin.AndroidX.Lifecycle.LiveData 2.5.1 is not compatible with monoandroid90 (MonoAndroid,Version=v9.0). Package Xamarin.AndroidX.Lifecycle.LiveData 2.5.1 supports:
monoandroid12.0 (MonoAndroid,Version=v12.0)
net6.0-android31.0 (.NETCoreApp,Version=v6.0)
So I don't know how to solve this. I don't want to target Android 12 version, I need this app to launch on an Android 9.0 device.
What are these libraries the app need to compile? Can I do without them? I don't find any references to them.
Note : I have a warning too on the same way, but not sure if it has to deal with my compilation issue. I Put it anyway, if it can help :
Warning Google Play requires that new applications and updates must use a TargetFrameworkVersion of v11.0 (API level 30) or above. You are currently targeting v9.0 (API level 28).
Please, any idea to solve my issue?
I just want to compile and launch an Android (Xamarin) blank app targeting Android 9.0.
Thank you and have a good day,
I'm finding this a problem too. Just deployed an app (Dec 2022) that targeted 28 / 12 and has min version 10 as appears enforced by Google?
Prob is I've got a lot of people wanting to use the app that can't see it because they have ancient phones (9 or earlier - is that 5 years or more). Seems a bit tough? Is there a way to overcome this.
I assume that the today's Xamarin Android project templates are designed to compile with Android 12. I don't think there is a way to build them with Android 9.
I guess we just have to find another older template?
For my part I had finally took an older project I had instead of creating a new project.

Android studio 3 not showing Android 2.3 sdk API 10 for a new project altought sdk is installed

I need to develop a test application for an old Samsung smartphone that has Android 2.3.6
I'm using Android Studio 3.1 and I've installed needed several old SDK from 2.1 to 2.3.3 including google api (that are showed as obsolete in sdk manager)
The problem is that when I create a new project, in the list where I can choose the SDK the lowest API available is 14; it seems that android studio is not showing me obsolete api and I cannot find where to set that I want to see all installed SDK.
Newer versions of Android Studio have minimum API level set to 14. I would suggest you look for android studio 2.3.3 which allows lower level APIs. Download link below:
https://developer.android.com/studio/archive
I face the same error while developing for the API 11. That was minimum of 14. But yes android studio never shows minimum then API 14. This is due to some Appcompat library.
Because when I dug out deeper then I found out that This Library was using minimum of 14 SDK. and due to manifest merging problem I think android studio is not showing minimum then that. At that time I was using 25.+ version of Appcompat library and I was using android studio 3.0.
You can find out more by selecting API 14 and then go into build.gradle file. now here you can change minSdkVersion to 11 or at any api level you want.
Then you can notice that while building you will get manifestMerger error with following proposed solutions
Error:Execution failed for task ':app:processDebugManifest'.
Manifest merger failed : uses-sdk:minSdkVersion 11 cannot be smaller than version 14 declared in library [com.android.support:appcompat-v7:26.1.0] \9fe0f18d3439e3d6185e96eddd3ec321\AndroidManifest.xml as the library might be using APIs not available in 11
Suggestion: use a compatible library with a minSdk of at most 11,
or increase this project's minSdk version to at least 14,
or use tools:overrideLibrary="android.support.v7.appcompat" to force usage (may lead to runtime failures)
So here is the problem and here you got the reason. May be some one can help you in bypassing it. I just thought to share my findings.
I am currently using Android Studio 3.0.1 and I have also experienced not being able to select api versions below 14 in the new project window, however I am unsure how to fix this issue directly. I have been successful as changing minSdkVersion (or using a project which had it already set) and haven't had a build issue. The same build issue appears to have solutions here, if you are experiencing this: Manifest merger failed : uses-sdk:minSdkVersion 14
I had a issue running the application on the emulator, as it was constantly waiting for the emulation to finish starting and this was fixed by updating the gradle as suggested by some notifications.
Otherwise I can only suggest trying the older ide version, especially if you are natively debugging using 32-bit windows, as it appears to not be supported in Android Studio 3. However after reading the release log it doesn't appear to mention excluding api below 14.
Native debugging with Android Studio no longer supports 32-bit Windows. We've chosen to focus on other platforms because very few developers are using this platform. If you are using 32-bit Windows and you plan to debug native code, you should keep using Android Studio 2.3.
[Source: https://developer.android.com/studio/releases/index.html]
Link to older ide version from the same source: https://developer.android.com/studio/archive.html#android-studio-2-3-3

Xamarin Targetframeworkversion

I am trying to make a basic Xamarin.forms (pcl) application (in Visual studio 2017) and i want to Target Lolipop (5.0.0, api level 21).
So i made a basic Xamarin solution and i adjusted the Android properties all to Android 5.0 lollipop
Compile using Android version "Android 5.0 (Lollipop)"
Minimum Android to target "Android 5.0 (API Level 21 - Lollipop)"
Target Android Version "Android 5.0 (API Level 21 - Lollipop)"
Unfortunately in my warning list i get the following
Severity Code Description Project File Line Suppression State
Warning The $(TargetFrameworkVersion) for Xamarin.Forms.Platform.dll (v7.0) is greater than the $(TargetFrameworkVersion) for your project (v5.0). You need to increase the $(TargetFrameworkVersion) for your project. Prolang.Android C:\Projects\Prolang\Prolang\Prolang.Android\Prolang.Android.csproj
If i google for this error most people say to indeed update your android project to use the latest Targetframework version. But in my case i really want to only be able to call Api calls that are available to level 21 aka Android 5.0 Lollipop. As in i want the compile time safety net that i only use Api Calls from Api level 21.
To me it seems like i have to downgrade my xamarin.Forms dll to a version that is made specifically for android lollipop since the one i use right now targets a higher version.
So should i downgrade my xamarin.forms dll to achieve this and get rid of the error? And if i downgrade to what version do i need to downgrade so i target Android 5.0 Lollipop? This information is all pretty unclear to me.
Xamarin.Forms is designed using the Android's support libraries so it will run on a wide range of APIs without any additional actions from the developer.
If you are using native APIs via Xamarin.Android (assumably via Forms' Dependency Service) then yes you would have to be aware of the API level of the method calls.
That said, since Xamarin.Forms uses the com.android.support.* libraries and you would have to have to a Forms versions that uses com.android.support:design:21.x.x and the associated Android build tools for API 21.
Now you can review the package dependancies and you will see that your choices are Forms' is 1.4
`1.4.0.6336-pre1` to `1.4.4.6449`
Note: v1.5.x moved to version 22 of the Android support libraries
IMHO using Forms's 1.4 would a bad mistake due to its age and the mountain of improvements and bugs fixes since 2015, you are so much better off:
Using the latest Forms version
Set the Framework version to latest (7.1)
Set the Target Android version to Android 5.0 (API Level 21)
Set the Minimum Android version to whatever you need...
Forgo the compile time API checks
Check runtime API issues via API-21 devices and emulators

Android target sdk vs deprecated methods

This code is some basic code to set up tabs in Android.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
If you write this code in a project where targetSDK < 20 or compileSDK < 20 (I don't know what the difference is between versions of compileSDK and targetSDK) then it is fine.
However, if you have compileSDK higher, e.g. 23, then Android Studio will tell you that the code above is deprecated (and moreover is removed from SDK, I guess).
And it will crash my app if i run it on device (btw on Android level 20, though the target of project was set to 23 and compiled version too 23, so it crashes because it is deprecated and removed from SDK level 23).
Questions:
1) What happens if I compile my project with compileSDK version and
targetSDK version set to 20 (where there is no problem with being
deprecated) and try run it on Android with SDK 21 or 23 where these
functions are obviously removed from SDK? Will the app crash or work?
2) What happens if I decide to target the "high" SDK (e.g. 23)? That
is, I will leave the "old" code mentioned above and will use some
"new" code for tabs, which appeared in SDK 23 or so (which didn't
exist in lower SDK); then I will try to run the application on
Android with lower SDK, e.g. 20. Will the app crash or work?
edit:
Btw now i launched project with targetSDK and compileSDK 20 on Android with level 22 and it worked.
So it seems the only thing that really matters is the compileSDK version? Doesn't even matter on the SDK version of device but it really matters the compileSDK?
Because as i said about the two tests - project with bad HIGH SDK launched on device with good LOW SDK = crash. Project with good LOW SDK launched on devide with bad HIGH SDK = not crash.
But the logic of course say that it must also matter on android version of device (eg to access some new functions.. or really only matter on compiledSDK or at least from view of backwards-functionallity)?
Need some opinions of experts on this.
However, if you have compileSDK higher eg 23, then Android Studio will tell you, the code above is deprecated (and moreover removed from SDK i guess).
No. getActionBar() is not deprecated. setNavigationMode() is deprecated but not removed.
What happens, if i compile my project with compileSDK version and targetSDK version eg 20 (where is not problem with deprecated) and will try run it on Android with SDK eg 21 or 23 where are these functions from SDK obviously removed? Will app crash or work?
The app should work fine. "Deprecated" means "we think that there is a better solution and we would prefer that you use it".
What happens, if i decide to target the "high" SDK that means eg 23, i will leave the "old" code mentioned above and will use some "new" code for tabs, which appeared eg in SDK 23 or so (that means didn't exist in lower SDK) and i will try run the application on Android with lower SDK, eg 20? Will app crash or work?
First, I am not aware of any "'new' code for tabs, which appeared eg in SDK 23". The Design Support library has the only new tab implementation that I can think of, and it works back to API Level 7.
Second, your app should fail to compile, if you are trying to use something that is newer than your minSdkVersion without adequate checks (e.g., using Build.VERSION.SDK_INT and bypassing that code on older devices).
In the end, if you refer to a class, method, field, interface, and so on that does not exist on the older API level, your app will crash if and when that code gets executed. A typical error is VerifyError, though it depends a bit on the nature of what it is that you are trying to use that does not exist.
With respect to tabs, there are countless implementations available to you. Most come in the form of third-party libraries tied to ViewPager. In terms of the Android SDK, there the aforementioned TabLayout from the Design Support library, FragmentTabHost, and PagerTabStrip (though the latter has a bug in the 23.0.0 edition of the support-v4 library, apparently).
Deprecated API are still available, so you're free to use them - the app won't crash on neither of platforms. However, it's advised that you read the documentation to understand why the API have been deprecated: usually developers provide alternative solutions.
Here is some explanation on compile/target/minSDK.
1) What happens, if i compile my project with compileSDK version and
targetSDK version eg 20 (where is not problem with deprecated) and
will try run it on Android with SDK eg 21 or 23 where are these
functions from SDK obviously removed? Will app crash or work?
Android Studio is probably only warning you that the API has been deprecated, but not removed. If it had been removed, setting the compile SDK to 23 should break your compilation. Your application should work fine even if it is using a deprecated API, but you should update that when you have the time as it is no longer the recommended one.
2) What happens, if i decide to target the "high" SDK that means eg
23, i will leave the "old" code mentioned above and will use some
"new" code for tabs, which appeared eg in SDK 23 or so (that means
didn't exist in lower SDK) and i will try run the application on
Android with lower SDK, eg 20? Will app crash or work?
When you use an API that has been introduced in SDK 23, you will need to set compileSDK to 23 as well. In case you set your minSDK to 22, you will be able to install it on a device that is using SDK 22, however, your app will crash with a NoSuchMethodError if you try to use that method. You can however check the SDK running on the device (use Build.VERSION.SDK_INT) and not call that method in this case.

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.

Categories

Resources