apache / cordova-android

Apache Cordova Android
https://cordova.apache.org/
Apache License 2.0
3.59k stars 1.52k forks source link

Bug: Cordova does not set the minSdkVersion and targetSdkVersion from config.xml correctly in the APK. #686

Closed Domvel closed 5 years ago

Domvel commented 5 years ago

How to set the Requires Android version in Play Store? It is always 4.4 for all Ionic / Cordova Android Apps. (See Play Store app details)

Screenshot

The preference android-minSdkVersion in config.xml is set to 22 (Android 5.1). Also android-targetSdkVersion has no effect. And I found nothing in the developer section of Play Store.

Google Play Console says API-Levels: 19+. I think it comes from the apk. But how to set it? minSdkVersion does not work.

Cordova Android is not respecting the config.xml sdk version properties, right? API-Levels == android-minSdkVersion

Ionic Info:

Ionic:
   ionic (Ionic CLI)  : 4.2.1
   Ionic Framework    : ionic-angular 3.9.2
   @ionic/app-scripts : 3.1.11

Cordova:
   cordova (Cordova CLI) : 8.1.1 (cordova-lib@8.1.0)
   Cordova Platforms     : android 7.1.4

System:
   Android SDK Tools : 26.1.1 (C:\Users\%USERNAME%\AppData\Local\Android\Sdk)
   NodeJS            : v8.12.0 (C:\Program Files\nodejs\node.exe)
   npm               : 6.4.1
   OS                : Windows 10
fgarcia5 commented 5 years ago

From Android Studio, go to "File" >> "Project Structure".

In "Project Structure" select "app" into the "Modules", then "Flavors" tab and set "Min SDK Version"

I hope to be helpful

Domvel commented 5 years ago

This is the Android Studio solution for a native app. Thanks. But not my problem. It's about the compiled App (apk) from Cordova. There is a file config.xml with the property android-minSdkVersion. This should do this job. But the Cordova compiler does not respect this configuration.

You mean I have to load the Android project from platforms/android into the Android Studio and set the properties? But I don't want to modificate ("hack") the Cordova sources (platform). This should be a non tracked resource (gitignore). Cordova have this setting for "Min SDK Version", but it does not work. I think this is a bug.

I could set the minSdkVersion in the file AndroidManifest.xml.

<uses-sdk
  android:minSdkVersion="19"
  android:targetSdkVersion="22" />

Not tested, but I'll try it as workaround. btw. there are several AndroidManifest.xml files. I don't know which file is the correct file. If I change a file I get erros like:

Manifest merger failed : uses-sdk:minSdkVersion 19 cannot be smaller than version 22 declared in library [:CordovaLib] %PROJECT%\platforms\android\CordovaLib\build\intermediates\manifests\full\debug\AndroidManifest.xml as the library might be using APIs not available in 19
  Suggestion: use a compatible library with a minSdk of at most 19,
    or increase this project's minSdk version to at least 22,
    or use tools:overrideLibrary="org.apache.cordova" to force usage (may lead to runtime failures)

Ok, maybe this is not the correct file. But funny, because I set min: 22 and target: 23. I'll continue by successive approximation. ... Anyway this feature has to be work from Cordova config.xml!

EmeryGuillaume commented 5 years ago

I can confirm this bug: the minSdkVersion is ignored from the config.xml (I specify minSdkVersion=21 and the output apk targets 19 which is android 4.4). None of my Cordova plugin sets this minsdkversion to 19 so there is an issue with cordova-android itself.

As a workaround I've created a build-extras.gradle with the following content:

allprojects { project.ext { defaultMinSdkVersion=21 } }

I copy this file in the platforms/android folder with a before_build hook and it does the job.

I hope it can help until this issue is addressed.

Domvel commented 5 years ago

@EmeryGuillaume Thanks for the hint. It's a great workaround. The build.gradle file contains the default values which I can overwrite before build. That looks promising.

EmeryGuillaume commented 5 years ago

@Domvel exactly, if you can override directly the build.gradle file, it's a good workaround too. But as the build.gradle is an auto generated file, sometimes you can't override it (for example on a remote server, in a CI/CD pipeline). In such cases, the build-extras.gradle can be used.

b4zz4 commented 5 years ago

I do not attain that it change of version. It follows compiling in android 6.2.2 :(

baversjo commented 5 years ago

upgrading cordova-android to from 7.1.4 (where this bug got introduced I think) to 8.0.0 also solves this problem, due to this fix probably: https://github.com/apache/cordova-android/commit/96975504885a8cf98e251c37b64921337f32c194#diff-c162d66bb4a22683316f0fc47e104078R25

After changing to config.xml to <engine name="android" spec="^8.0.0" /> and package.json to "cordova-android": "^8.0.0",, I had to remove package-lock.json and node_modules for the version not to revert.

b4zz4 commented 5 years ago

Modifying this does not work I found that ..

platforms/android/project.properties platforms/android/CordovaLib/project.properties content: target=android-27

Domvel commented 5 years ago

@baversjo Your contribution solves the problem. Thank you very much. This issue is already fixed in the latest Cordova-Android version (8.x). See pull #655 I'll update and hope for full compatibility. The first tests look good.

Closed as fixed.

dpogue commented 5 years ago

Ah, I missed that this was reported with cordova-android@7.x. Closing as it's been fixed in cordova-android@8.

Domvel commented 5 years ago

NOTE You have to clean the platform. Otherwise the minSdkVersion has a memory-effect. The value will not be updated. Clean e.g. by removing the android platform folder before build.

Also note that Android does not handle the minSdkVersion value correctly if you try to install the apk manually (copy apk to device and install by file manager). You'll get a parsing error like "There was a problem parsing the package". That's confusing. I expected a message like "This app is not compatible for your Android version." ... But it looks like this is a file manager issue. If I try to install this app by adb install I get a correct message like "Requires newer sdk version $minSdkVersion (currently version is $installedVersion)". I can't say how it behaves on Play Store yet. I'll test it on the next update of the app.

b4zz4 commented 5 years ago

I update and believes a new project ...

sdkmanager --list Path Version Description Location
build-tools;19.1.0 19.1.0 Android SDK Build-Tools 19.1 build-tools/19.1.0/

... platforms;android-19 | 4 | Android SDK Platform 19 | platforms/android-19/

npm install -g cordova npm install -g cordova-android ... cordova build ... adb install /tmp/.../platforms/android/app/build/outputs/apk/debug/app-debug.apk 3438 KB/s (1696769 bytes in 0.481s) pkg: /data/local/tmp/app-debug.apk Failure [INSTALL_FAILED_OLDER_SDK]

dpogue commented 5 years ago

Cordova Android 8.x targets android-28 and requires version 28 of the build tools.

android-19 is the minimum OS version where the app will run, but building requires SDK 28.

b4zz4 commented 5 years ago

I have the 28 also. It works. But it does not compile for old versions I am testing with a device android 5.x

Domvel commented 5 years ago

NOTE Google PlayStore does not accept target level below 26. Whatever... it's recommended to keep the Cordova set target (which is always the latest known Android) and only set the Min-SDK-Level if wanted. It's all fine.

b4zz4 commented 5 years ago

But I could publish in android 5 or 4 with sdk 28?

Domvel commented 5 years ago

@b4zz4 Yes. Just ignore the target-sdk-level. Remove it from config.xml. It's handled by Cordova and your build-tools. (Should be the latest known Android version.) You can use the min-sdk-level value to control the required Android version. (by default 19) This can be set on the lowest possible value to reach the most users. But I recommend to ignore the old versions (e.g. Android lower than 6. See statistics of usage. To avoid support requests and rage rating.) It's also generally recommended to use the latest software / libraries e.g. Android-Cordova.

eddskt commented 4 years ago

Another alternative? neither the tips of @EmeryGuillaume and @b4zz4 work for me, always back to minsdk 21

eddskt commented 4 years ago

I solved changing lines in file: platforms/android/gradle.properties

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m
android.useDeprecatedNdk=true
cdvMinSdkVersion=21

to

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m
android.useDeprecatedNdk=true
cdvMinSdkVersion=19

thanks @b4zz4 for cite the file

benyaminl commented 4 years ago

I solved changing lines in file: platforms/android/gradle.properties

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m
android.useDeprecatedNdk=true
cdvMinSdkVersion=21

to

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m
android.useDeprecatedNdk=true
cdvMinSdkVersion=19

thanks @b4zz4 for cite the file

Question, does this set the android version to api 27?

target=android-27
android.library.reference.1=CordovaLib
android.library.reference.2=app
cordova.system.library.1=com.android.support:support-annotations:27.+
anandtripathi5 commented 3 years ago

in your AndroidManifest.xml you can specify it like <uses-sdk android:minSdkVersion="22" android:targetSdkVersion="23" />

Also can set in config.xml

<platform name="android">

  <preference name="android-minSdkVersion" value="22"/> 
  <preference name="android-targetSdkVersion" value="23"/>

  <!--other line code -->

</platform>