Why does the Android SDK 30 generate invalid APK files?

血红的双手。 提交于 2021-02-11 12:23:59

问题


I built a new Java Android app on a new PC using a fresh install of Android Studio 4.1.2, and was able to run it on my phone from the debugger fine. However, when I built a signed APK file (using my normal key store file) and copied it to my phone, I could not install it on the phone using the Google Files app, like I usually can. After several hours, I eventually worked out that if I changed my build.gradle file so that the compileSdkVersion, buildToolsVersion, and targetSdkVersion were all 29 instead of 30, then the APK installed fine. This is even though my phone is running Android 11 and the minSdkVersion in my build.gradle is 19 (Android 4.1).

The app is fairly simple, consisting of two java files: a main activity and a server class which extends the open-source NanoHTTPD class. The NanoHTTPD jar file is in the app's libs folder. The app only requires the following permissions in it's manifest: android.permission.INTERNET, android.permission.ACCESS_WIFI_STATE, android.permission.ACCESS_NETWORK_STATE

How do I find out if this is something I'm doing wrong, a bug with version 30 of Google's SDK, or a bug in Android Studio so I can report it to the appropriate people? Thanks.


回答1:


To find out why your APK is not installing, use command line adb tool to install it:

adb install myapp.apk

If the installation fails, adb will print an error message describing the reason.

One possible reason for failing to install from file browser is if the debug version of your app is still installed on your phone.

The signed version would have the same package name but different signature, so the OS will not install it.




回答2:


OK, solved now. Thanks to Lev M for the suggestion to use adb to try and install the app. That showed what the issue was straight away!

The problem was that when I was building the signed APK file, I was only ticking the "V1 (Jar Signature)" option, but I also needed to tick "V2 (Full APK Signature)". It looks like you need V2 signing for SDK 30 onwards.

This is confirmed here: https://developer.android.com/about/versions/11/behavior-changes-11



来源:https://stackoverflow.com/questions/66128615/why-does-the-android-sdk-30-generate-invalid-apk-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!