Using latest libraries for apps on old Android devices - java

I have updated the Apache Commons Compress to the latest 1.18 in my Android project which has minSdkVersion 16
Everything was fine(build, run) until I have tested it on old Xoom device with Android 4.1
It did not crash but worked wrong and it was really hard to diagnose what's wrong.
I end up with some log records like
unable to resolve static field (UTF_8) in
Ljava/nio/charset/StandardCharsets
NoClassDefFoundError; thrown while initializing
Lorg/apache/commons/compress/archivers/zip/ZipEncodingHelper
And find out the problem is lack of support java.nio.charset in the pre 19 Androids.
QUESTION 1: Is there a way to add support of Java 1.7 and newer on the older devices? So the all latest libraries which tend to move toward minimum required Java version 1.7 and up will work on such devices with no problem. May be some Google Play settings?
Guess answer for Q1 is NO. So...
QUESTION 2: Should we use libraries with minimum JDK 1.6 for Android if we want to run our app on older devices(e.g., minSdkVersion 16). I managed to resolve my issue migrating back to the latest Commons Compress 1.12 with minimum 1.6 JDK
QUESTION 3: Is there a way to check whether all dependencies gonna work with old devices without the need to test the app on such devices? E.g., some tool to validate dependencies against particular Android SDK.

Related

How does sourceCompatibility and targetCompatibility impact number of supported devices?

What I understand so far
minSdkVersion 21 - means that the device must have at least Android API level 21 or higher installed in order to install the app. (This value should be set as low as possible to help reach the largest number of users while still maintaining all critical app functionality.)
targetSdkVersion 26 - means that it was developed for the Android API version 26, allowing the device to determine if compatibility mode or optional features should be enabled. (This value should be set as high as possible to allow developers to access new features and manage any deprecated API calls.)
What I am confused about
But what about the configurations sourceCompatibility and targetCompatibility? I have been receiving conflicting information about which JDK version should be used.
An example of this is seen in the project structure settings in Android Studio, where using the default JDK version that comes with Android Studio (version 1.8) is recommended by the tooltips.
However, when I read other online sources such as the following:
https://www.christopherprice.net/which-jdk-do-i-use-with-android-studio-3686.html
Which JDK version (Language Level) is required for Android Studio?
It appears that Android primarily runs on Java version 1.7, with only limited support for some features of version 1.8. This suggests that using version 1.7 would be the more logical choice.
Questions that I have
Question 1) Which JDK version, 1.7 or 1.8, should I choose for maximum compatibility with both new and old Android devices? Does this configuration even make a difference in terms of reachable market size?
Question 2) Is the choice of sourceCompatibility and targetCompatibility (and JDK version) only relevant during the compilation of .java files to .class files and have no effect once the Java bytecode is generated? Or does the chosen JDK version affect the end user's experience, for example if their device's JVM can't interpret version 1.8 bytecode, causing the app to crash during runtime?
Question 3) What are the consequences of setting minSdkVersion to a low value (e.g. 10) and sourceCompatibility and targetCompatibility to a high value (e.g. 1.8)? Can I trust that Android Studio will detect all compatibility issues and if it builds an APK, it will function properly? Or can it still be built and installed on devices with API level >= 10, but crash at runtime if the device's JVM can't handle version 1.8?
The android toolchain does some extra steps before the code is run on your device:
.java -> .class -> .class (desugared) -> .dex
This is described here: https://developer.android.com/studio/write/java8-support
The desugaring step is responsible for turning your modern bytecode into something that works on older VMs.
How backwards compatible the desugaring makes your code depends on the minSdkVersion. If your project makes a combination of sourceCompatibility/targetCompatibility and minSdkVersion that isn't possible, the compiler will tell you.
This also goes for byte code from 3rd party libraries. Errors look like this:
Error: Static interface methods are only supported starting with Android N (--min-api 24): okhttp3.Request
(this specific problem came from using 1.7 source compatibility with okhttp3 4.0.1 and went away by using target 1.8)

importing java time in Android Studio [duplicate]

According to Android Studio documentation:
Android Studio 3.0 introduces built-in support for Java 8 libraries
But a huge improvement in Java 8 is the use of the new java.time library. For now, this library seems not included in this version.
So my question: I'm I wrong ? If not, is there any workaround possible to use this library in Android applications ?
To get Java 8 language feature support running in Android Studio, check these four things:
Make sure you are running the latest version of Android Studio 3.0
Ensure you are using the embedded JDK
Update your the source and
target compatibility versions to 1.8 in your project structure.
Ensure you minSdkVersion is 24 (or 26/'O' depending on which method
you want to use in the class)
Once your project complies with those settings, you can add libraries such as: import java.time.LocalDateTime; to your project. Check out this page for more information: https://developer.android.com/studio/preview/features/java8-support.html

Unable to load class 'com.google.gson.JsonObject'

I am trying to include firebase SDK in my Android Studio Project. But it giving me an error. I am using latest JDK version (1.7.0_71) and it is asking for upgrading higher version to (1.7.0_67).
I need help for basic firebase setup SDK on android. I tried to setup it by the reference of following link: https://firebase.google.com/docs/android/setup#add_the_sdk.
I am getting following error: Please check attached screenshot
I had the same problem trying to GCM to an old android project.
The solution for me was to also add
classpath 'com.android.tools.build:gradle:2.1.2'
to the top-level build file dependencies, just before classpath 'com.google.gms:google-services:3.0.0'. For the old android project, this dependency only existed in the module-level build file.
Make sure you are using Android Studio 1.5 or above. Just saw a guy who had the same issue and he was using android studio 1.0.1
Updating the Android Studio will fix it for you. Also check other pre-requisites.
Prerequisites from official documentation are as follows:
A device running Android 2.3 (Gingerbread) or newer, and Google Play services 9.2.1 or newer
The Google Play services SDK from the Android SDK Manager
Android Studio 1.5 or higher
An Android Studio project and its package name
First of all complete all the steps mentioned in a link you had followed for Firebase
And note that it requires active internet connection so please check internet connection and add both project and app level dependencies as per suggested in link of Firebase you had used in question
Also it is best to use JDK version 1.8 and above because oracle officialy ended support for those versions. If you are working with Firebase Api which is quiet new so i would highly recommend as it is also recommended by most professionals including developers at Google.Or if you want to go with JDK 1.7 then update it to latest Jdk 7u80 or 7u79 at least because it has least issues comparing to older versions.
The best solution is use JDK version 1.8 and follow all the steps in link https://firebase.google.com/docs/android/setup#add_the_sdk
Ensure that you have the google-services.json file, with the client id: <effective package name>, in the app directory. This you download from the Firebase console when you add an app.

set up different android dependencies (.jar) for a specific android version (Lollipop)

I have a problem.
My App works well and the major o Android versions.
The problem is for the devices that are greater than Android Lollipop(<5.0)
The library that gives the problem is a dependencies (PushIOManager.jar)
They have released a update for this problem. But this new file doesn't work for previous versions.
Can I Have two APK?
ThereĀ“s a way to get each .jar depend on the android version? one for the previous version and one .jar for the new Lollipop.
And where should I put the code?
Thanks.

Rebuild project for lower sdk/jdk. Failure [INSTALL_FAILED_OLDER_SDK]

I have an android project which was build under jdk 1.7. I have all 4+ and 2.1 -2.3 sdk packages in my InteilJ enviroment. All the time the project was tested under jdk 1.7, and v4.0++ emulator. Now I want to run it on device with 2.3.5 android api version but it responds with Failure [INSTALL_FAILED_OLDER_SDK] . I downloaded older jdk 1.6 and set 2.3 api for project. Even it passes rebuilding, the project can't start on real device. I've set minSdkVersion in manifest. When I start new project with above settings it works on this 2.3.5 device. Any ideas, sollution ?
Unfortunately, the installation failure error messages are not easy to understand in all cases.
[INSTALL_FAILED_OLDER_SDK] means that the minimum API level of the APK you are trying to install (android:minSdkVersion) is higher than the API level of the device you are trying to install it on.
In your case, a normal Android 2.3.5 device will report that it runs API Level 10. I say "normal", because a vendor firmware or ROM mod could report that it runs API Level 3, or API Level 1337, though usually the API level is not tinkered with this way.
In your case, it would appear that the manifest of the project has an android:minSdkVersion of 11 or higher. There is nothing wrong with that... except that the app will refuse to install on Android 1.x/2.x devices and emulators.

Categories