从命令行构建的 Xamarin.Forms android 应用程序不起作用 [英] Xamarin.Forms android application built from command line doesn't work
问题描述
我有一个 Xamarin.Forms Android 应用程序,使用 Visual Studio 2017 开发.如果我使用 USB 设备从 Visual Studio 运行/调试应用程序,它运行良好(调试和发布配置).
然后我在 Visual Studio 中使用存档命令创建 .apk.为了测试它,我只需从我的开发人员上传它.计算机到谷歌驱动器,然后从同一设备下载并安装它.这也有效.
当我尝试从命令行创建 .apk 时出现问题.以这种方式获得的文件被设备识别,并正确安装它,但是当我启动应用程序时,它似乎启动但一秒钟后突然关闭.我什至没有看到我在开发阶段出现异常时遇到的熟悉的弹出窗口不幸的是应用程序停止了".
这些是我使用的命令:
<块引用>msbuild/t:Clean/p:Configuration=Release
msbuild/t:PackageForAndroid/p:Configuration=Release
keytool -genkey -v -keystore SymCheck.keystore -alias SimCheck -keyalgRSA -keysize 2048 -validity 10000
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystoreSymCheck.keystore my_application.apk SimCheck
根据
所以,这里是完整的操作序列:
- 确保为发布配置禁用快速部署
- 在
.csproj
所在的目录中打开一个 Visual Studio 命令提示符,然后在其中运行以下命令:
# clean stepmsbuild/t:Clean/p:Configuration=Release# 构建步骤msbuild/t:PackageForAndroid/p:Configuration=Release# keytool 步骤[c:\Program Files (x86)\Java\jdk1.8.0_161\bin\\]keytool.exe"-genkey -v -keystore <密钥库的文件名>-alias {a string} -keyalg RSA -keysize 2048 -validity 10000# jarsigner 步骤"[c:\Program Files (x86)\Android\android-sdk\build-tools\\{version e.g.27.0.1}\\]zipalign.exe"-f -v 4 bin\Release\\{由msbuild在步骤构建步骤中创建的文件apk} bin\Release\\{输出apk文件名}# apksigner 步骤"[c:\Program Files (x86)\Android\android-sdk\build-tools\\{version e.g.27.0.1}\\]apksigner.bat"sign --ks {在步骤 c 中选择的密钥库的文件名.} --ks-key-alias {在 keytool 步骤中选择的别名字符串.} bin\Release\\{从 zipalign 步骤输出 apk 文件名.}
最后一步(apksigner step
)生成的apk是好的.
[]
中的路径是特定于平台的,可以避免更改机器的 PATH
环境变量.
{}
中的部分是用户选择的名称.
I have a Xamarin.Forms Android application, developed using Visual Studio 2017. If I run/debug the application from Visual studio, with a USB device, it works well (both debug and release configurations).
I then create the .apk using the archive command in visual studio. To test it I simply upload it from my dev. computer to google drive and then download it from the same device and install it. This works as well.
The problem comes out when I try to create the .apk from the command line. The file obtained in this way is recognized by the device, which installs it correctly, but when I start the app, it seems to start but after a second it closes abruptly. I don't even get the familiar popup "unofortunately the app stopped" that I got during the development phase when there were exceptions.
These are the commands I use:
msbuild /t:Clean /p:Configuration=Release
msbuild /t:PackageForAndroid /p:Configuration=Release
keytool -genkey -v -keystore SymCheck.keystore -alias SimCheck -keyalg RSA -keysize 2048 -validity 10000
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore SymCheck.keystore my_application.apk SimCheck
According to https://docs.microsoft.com/en-gb/xamarin/android/deploy-test/signing/manually-signing-the-apk
I also tried this sequence
msbuild /t:Clean /p:Configuration=Release
msbuild /t:PackageForAndroid /p:Configuration=Release
keytool -genkey -v -keystore SymCheck.keystore -alias SimCheck -keyalg RSA -keysize 2048 -validity 10000
zipalign.exe -f -v 4 my_application.apk my_application_aligned.apk
apksigner.bat sign --ks SymCheck.keystore --ks-key-alias SimCheck my_application_aligned.apk
with the same outcome.
I found the issue.
For some reasons the project option indicated below was set also in the release configuration.
When debugging, visual studio did transfer all the needed resources to the device, so everything worked fine in that case.
However, msbuild /t:PackageForAndroid
was embedding in the .apk
only part of the resources needed to run the application.
Removing that check did fix the problem; the .apk
went from 3.6MB
to 16MB
.
So, here the full sequence of operations:
- Ensure that Fast Deployment is disabled for the Release configuration
- Open a Visual Studio command prompt on the directory where the
.csproj
is, then run the following commands in there:
# clean step
msbuild /t:Clean /p:Configuration=Release
# build step
msbuild /t:PackageForAndroid /p:Configuration=Release
# keytool step
"[c:\Program Files (x86)\Java\jdk1.8.0_161\bin\\]keytool.exe" -genkey -v -keystore <a filename for keystore> -alias {a string} -keyalg RSA -keysize 2048 -validity 10000
# jarsigner step
"[c:\Program Files (x86)\Android\android-sdk\build-tools\\{version e.g. 27.0.1}\\]zipalign.exe" -f -v 4 bin\Release\\{file apk created by msbuild in step build step} bin\Release\\{output apk filename}
# apksigner step
"[c:\Program Files (x86)\Android\android-sdk\build-tools\\{version e.g. 27.0.1}\\]apksigner.bat" sign --ks {filename for keystore chosen in step c.} --ks-key-alias {alias string chosen in keytool step.} bin\Release\\{output apk filename from zipalign step.}
The apk generated with last step (apksigner step
) is the good one.
Paths in []
are platform specific and can be avoided changing the machine's PATH
environment variable.
Parts in {}
are names to be chosen by the user.
这篇关于从命令行构建的 Xamarin.Forms android 应用程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!