受欢迎的博客标签

Android|Visual Studio|Xamarin.Android-How to generate an .apk file from Xamarin.Android Project using Visual Studio?

Published

 

向用户发布Android 应用

1.通过应用市场发布应用
google play
2.通过网站发布应用
将应用放在您的网站或服务器上供用户下载。只需将准备好发布的 APK 文件托管到网站上并向用户提供下载链接即可。

 

Create a Xamarin.Android project
Generate an .apk file from Xamarin.Android Project
     Go to Solution Explorer -> Android project (right click) --> Archive --> Distribute -- AdHoc --> Sign & Add Details --> Save As

 

Create a Xamarin.Android project
Decompose an app into activities
Build an activity's UI
Write an activity's behavior
Update your Android SDK

 

OS:Windows 10

Tools:Microsoft Visual Studio Enterprise 2022 (64 位) 版本 17.0.2

 

Part 1:Create a new Xamarin.Android project with default project template

 

https://docs.microsoft.com/en-us/learn/modules/introduction-to-xamarin-android/

 

part 2:How to generate an .apk file from Xamarin.Android Project using Visual Studio?

Office doc:https://docs.microsoft.com/en-us/xamarin/android/deploy-test/release-prep/?tabs=windows#Compile

必要条件

1.只有release版本的APK才能安装在自己的手机上;debug版本的APK在手机上安装时会提示失败!所以发布时要选用release mode 生成apk文件。
2.Android 系统要求安装的每个应用都必须使用应用开发者拥有的证书,所以要对apk进行数字签名。
Android系统会将所有的APK文件识别为应用程序的安装包,类似于windows系统上的exe文件。

 

step 1:完成发布应用所需的准备工作
Step 2:签名 .apk 文件
Step 3: 将.apk 文件分发给用户。

Try The Following Steps:

Go to Solution Explorer -> Android project (right click) --> Archive --> Distribute -- AdHoc --> Sign & Add Details --> Save As

Part 1:Prepare you application for generate an .apk file
Release-You need to change Your Project Mode from debug to release.

Compile-Rebuild your project.-Select Build > Rebuild Solution to verify that it builds successfully in Release mode.  this step does not yet produce an APK.

Part 2:generate an .apk file
Archive -Go to Solution Explorer -> Android project (right click) --> Archive --> Distribute -- AdHoc --> Sign & Add Details --> Save As

this step  produce an APK.

Part 3:Distribute an .apk file to Users
Archive -Go to Solution Explorer -> Android project (right click) --> Archive --> Distribute -- AdHoc --> Sign & Add Details --> Save As

file:

Archive -Go to Solution Explorer -> Android project (right click) --> Archive --> Distribute -- AdHoc --> Sign & Add Details --> Save As

play store:

 

The APK is Ready To Publish in Play Store

part3:How to generate an .apk file from Xamarin.Android Project using Visual Studio Step by Step?

step 1:Change Your Project Mode from debug to release.

To change the build configuration, either:

From the Build menu, select Configuration Manager, then select Debug or Release.
or

On the toolbar, choose either Debug or Release from the Solution Configurations list.

 

AndroidApp.csproj

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
 +   <DebugSymbols>false</DebugSymbols>
 +   <DebugType>none</DebugType>
    <Optimize>True</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
	@@ -50,8 +50,11 @@
    <AndroidManagedSymbols>true</AndroidManagedSymbols>
    <AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
    <AndroidLinkMode>SdkOnly</AndroidLinkMode>
    <EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
    <AndroidCreatePackagePerAbi>false</AndroidCreatePackagePerAbi>
    <AndroidPackageFormat>apk</AndroidPackageFormat>
    <AndroidUseAapt2>true</AndroidUseAapt2>
    <BundleAssemblies>false</BundleAssemblies>
  </PropertyGroup>

 

Office doc:How to Set debug and release configurations in Visual Studio 22 Step by Step?

 

step 2: Archive

this step  produce an APK.

right click your Android project and select "Archive..."

Step 3:Distribute

Step 4:AdHoc

Step 5:Sign

要执行这一步,是因为:


Android 系统要求安装的每个应用都必须使用应用开发者拥有的证书(即开发者用于存放私钥的证书)进行数字签名。

Android 系统利用该证书来识别应用作者并在应用之间建立信任关系。您用于签名的的证书无需由证书授权机构签名;

Android 系统允许您使用自签名证书为您的应用签名。

Step 6:Save As

 

F:\stock\ReadDZHRealTime\src\WinForm\AndroidApp\bin\Release

 

https://docs.microsoft.com/en-us/xamarin/android/deploy-test/signing/?tabs=windows

Android SDK Manager

Tools > Android > Android SDK Manager


  Could not find android.jar for API level 30.

This means the Android SDK platform for API level 30 is not installed. Either install it in the Android SDK Manager (Tools > Android > Android SDK Manager...), or change the Xamarin.Android project to target an API version that is installed. (D:\Android\android-sdk\platforms\android-30\android.jar missing.) AndroidApp   

 

Useful links

Xamarin.Android  Publishing your Xamarin.Android APK in the Visual Studio 

https://riptutorial.com/xamarin-android/example/29653/preparing-your-apk-in-the-visual-studio