- Home /
How to detect the Android version of the user device?
My game doesnt run well with android 4. I would like some kind of waring popup if the game detects that the device runs on android 4 or older. How do I do that?
You can set the version on the google store so that user with an old version won't be able to download it.
This may sound like preventing people from downloading but it also protect your app from bad reviews.
Think about it.
Answer by RobWatling · Nov 09, 2015 at 01:08 PM
The following SystemInfo function will return what you are looking for http://docs.unity3d.com/ScriptReference/SystemInfo-operatingSystem.html
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Example() {
print(SystemInfo.operatingSystem);
}
}
Answer by HenryStrattonFW · Nov 09, 2015 at 12:36 PM
SystemInfo is a class that will give you access to this information (via the operatingSystem variable). Or someone on a thread over at GameDev Stack Exchange has put up a small bit of C# that should return you the exact API level which you can then use to determine what android version is being run.
Hope this helps.
the above link proposes the following code:
public int GetSD$$anonymous$$Level() {
var clazz = AndroidJNI.FindClass("android.os.Build$VERSION");
var fieldID = AndroidJNI.GetStaticFieldID(clazz, "SD$$anonymous$$_INT", "I");
var sdkLevel = AndroidJNI.GetStaticIntField(clazz, fieldID);
return sdkLevel;
}
I needed to change the FindClass parameter to "android/os/Build$VERSION", as the above code was issuing the error message "JNI DETECTED ERROR IN APPLICATION: illegal class name 'android.os.Build$VERSION'"
The corrected line seemed to do the trick, but now I am running the code again some weeks later and it crashes again. This time, I get the following messages :
JNI ERROR (app bug): accessed stale local reference [...] JNI DETECTED ERROR IN APPLICATION: jclass is an invalid local reference: [...] in call to GetStaticFieldID
Does anybody have an idea how to fix this?
I found out that the above calls should not be made in a method that is called from a Thread. In my case it was called in a callback returning from an async call on WebClient.
Your answer
Follow this Question
Related Questions
Android exported project for Unity 5.2 Crashes. FMOD implementation error. 2 Answers
Daydream compatible Unity versions? 0 Answers
Gradle Build Failed Unity version 2020..3.23 1 Answer
Android platform version 0 Answers
Android platform version 1 Answer