如何從共享庫中獲取版本信息到 Android 清單中? (How to get version info from a shared library into the Android manifest?)


問題描述

如何從共享庫中獲取版本信息到 Android 清單中? (How to get version info from a shared library into the Android manifest?)

我們有一個包含版本信息的共享庫,我們的 Visual Studio 解決方案中的所有項目都引用它。

在大多數情況下,我們可以引用每個項目的版本字符串,並且 dll 反映相應的信息。

我的問題是,我們的 Android 應用程序(基於 xamarin)。它有一個包含 versionName 和 versionCode 的清單文件。

我們如何讓我們的 android 清單文件中的這些值從我們的共享項目中讀取?


參考解法

方法 1:

Note: I do not know anything about xamarin.

In java you can get the versioninfo from the manifest like this

public static String getAppVersionName(final Context context) {
    try {

        final String versionName = context.getPackageManager()
                .getPackageInfo(context.getPackageName(), 0).versionName;
        return versionName;
    } catch (final NameNotFoundException e) {
    }
    return null;
}

I assume that xamarin has some mechanism to call PackageManager to get Packageinfo, too

方法 2:

My understanding is that, it is not possible. Because

The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code.

From Google's documentation

So this is a file that is required before the App builds.

C# Code in Shared Project (SAP/PCL) is ready to be used only after successful Compilation. So logically setting the Version Code and Version Name in Android Manifest File from Shared logic is not possible.

Another standard approach would be to set it from String Resource (XML) file in Android. You may have to copy and paste the value from Shared Project to strings.xml file and refer it in manifest, like

@string/versionCode

方法 3:

You could do this by using a Dependency Service. Here's a great article on them: https://developer.xamarin.com/guides/xamarin‑forms/dependency‑service/

The idea would be your Dependency Service would expose the Android specific information to the shared code library.

For instance you might have an interface in your common code declared such as:

public interface IPlatformVersionInfo
{
    string GetOSVersion (); 
}

Now, in your Android library you would implement it:

public class PlatformVersionInfo : IPlatformVersionInfo
{
    public string GetOSVersion () {
        return Android.OS.Build.VERSION.SdkInt.ToString ();
    }
}

Finally, in your common code you would use your dependency service of choice to invoke an instance of it:

var osVersion = DependencyService.Get<IPlatformVersionInfo>().GetOSVersion ();

Of course this is somewhat pseudo‑code and depending what dependency service you choose the code may look a bit different.

(by stoick3bSreerajRedth)

參考文件

  1. How to get version info from a shared library into the Android manifest? (CC BY‑SA 2.5/3.0/4.0)

#versioning #Android #android-manifest #xamarin






相關問題

AndroidManifest.xml 文件 <manifest> versionCode 屬性 (AndroidManifest.xml file <manifest> versionCode attribute)

grails 插件兼容性 (grails plugins compatibility)

Debug Unit Test bằng cách sử dụng Resharper 7 cho MBUnit ném ra một ngoại lệ (Debug Unit Tests using Resharper 7 for MBUnit throws an exception)

在 Maven 依賴項中包含不同版本的庫,完全破壞了我的構建 (Including a different version of a library, in a maven dependency totally broke my build)

Servicestack nuget 版本控制 (Servicestack nuget versioning)

如何從共享庫中獲取版本信息到 Android 清單中? (How to get version info from a shared library into the Android manifest?)

我可以在 Visual Studio 中為解決方案設置通用的 major.minor.release (Can I set a general major.minor.release for a solution in Visual Studio)

跨版本的 PowerShell 安裝文件夾和腳本文件擴展名 (PowerShell installation folder and script filename extension across versions)

嚴格版本化的 WCF 服務命名空間是否應該對合同是唯一的? (Should strictly versioned WCF service namespaces be unique to the contract?)

為不同版本的 3rd 方庫設計實用程序 (Design of utilities for diffrent versions of 3rd party library)

如何檢索顯示上游狀態的 git 版本信息 (How do I retrieve git version info showing upstream state)

帶有紗線漿果的 CLI 凹凸版本號 (CLI bump version number with yarn berry)







留言討論