如何在 Jetbrains Rider 中构建 Android APK

Posted

技术标签:

【中文标题】如何在 Jetbrains Rider 中构建 Android APK【英文标题】:How to build Android APK in Jetbrains Rider 【发布时间】:2019-12-17 14:39:47 【问题描述】:

我在 Rider IDE 中找不到从 xamarin 项目构建 apk 的选项。谢谢

【问题讨论】:

右键单击解决方案资源管理器中的项目节点,然后“存档以供发布”? 2019.3可用 【参考方案1】:

不可能直接(或者可能在2019.3,我还没有更新),但就生产发布版本而言,具有一致/自动更新的内部版本号,zip对齐和签名的apk,您可以使用额外的工具,例如 cake,它可以利用 MSBuild 并处理增加内部版本号、清理/恢复块包、在 apk 中构建/签名/ZipAlign、运行测试、将 apk 上传到您的服务器/google 的所有过程。

这是一个这样的蛋糕文件的例子:

#addin "Cake.androidAppManifest"
#addin nuget:?package=Cake.Git
#tool nuget:?package=NUnit.ConsoleRunner&version=3.9.0


var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var SolutionFile = "../MyProject.sln"; 
var projectToBuild = "./MyProject.Droid.csproj";
var buildDir = Directory("./bin") + Directory(configuration);
var objDir = Directory("./obj") + Directory(configuration);

var pkg = "com.organisation.app";
var AndroidSDK = "/Users/Me/Library/Developer/Xamarin/android-sdk-macosx";
var AndroidBuildToolsVersion = "28.0.3";
int rc = 0;

// the build number will be set to the commit number. First commit, bn = 1,...

var buildNumber = GitLog(Directory(".."), int.MaxValue).Count; 

Task("Clean")
    .Does(() =>

    CleanDirectory(buildDir);
    CleanDirectories(objDir);
);

Task("Restore")
    .IsDependentOn("Clean")
    .Does(() =>

    NuGetRestore(SolutionFile);
);

Task("Increase build number")
    .IsDependentOn("Restore")
    .Does(() =>

    var pathToAndroidManifest = "./Properties/AndroidManifest.xml";
    var manifest = DeserializeAppManifest(new FilePath(pathToAndroidManifest));
    manifest.VersionCode = buildNumber;
    SerializeAppManifest(new FilePath(pathToAndroidManifest), manifest);
);

Task("Build")
    .IsDependentOn("Increase build number")
    .Does(() =>

    MSBuild("../MyProject.sln", new MSBuildSettings().SetConfiguration(configuration));
);

Task("Tests")
    .IsDependentOn("Build")
    .Does(() =>

);

Task("package")
    .IsDependentOn("Tests")
    .Does(() =>

    // Creates the initial build of an apk.
    MSBuild(projectToBuild, s => 
        s.SetVerbosity(Verbosity.Quiet);
        s.SetPlatformTarget(PlatformTarget.MSIL);
        s.SetConfiguration(configuration);
        s.WithTarget("SignAndroidPackage");
        s.WithProperty("SolutionDir", "../");
    );
);

Task("zipAlign")
    .IsDependentOn("package")
    .Does(() =>

    Information("Zip-aligning APK");
    rc = StartProcess($"AndroidSDK/build-tools/AndroidBuildToolsVersion/zipalign", new ProcessSettings 
        Arguments = new ProcessArgumentBuilder()
            .Append("-f")
            .Append("-v 4")
            .Append($"buildDir/pkg-Signed.apk")
            .Append($"buildDir/pkg.apk")
    );
    if (rc == 0) 
        Information("APK Zip Alignment succeeded.");
     else  
        var msg = "APK Zip Alignment failure.";
        throw new Exception(msg); 
    
);

Task("Signing")
    .IsDependentOn("zipAlign")
    .Does(() =>

    var keystore = $"../keystore/release.keystore";
    var pass = "*********";

    Information("Signing apk...");
    rc = StartProcess($"AndroidSDK/build-tools/AndroidBuildToolsVersion/apksigner", new ProcessSettings 
        Arguments = new ProcessArgumentBuilder()
            .Append("sign")
            .Append("--ks")
            .Append(keystore)
            .Append($"--ks-pass pass:pass")
            .Append($"buildDir/pkg.apk")
    );

    if (rc != 0)  
        var msg = "APK Signing failed.";
        throw new Exception(msg); 
    
);

Task("Tag")
    .IsDependentOn("Signing")
    .Does(() => 
        Information("Adding a github tag with the build number");
        GitTag(Directory(".."), $"buildNumber");
    );

Task("Publish")
    .IsDependentOn("Tag")
    .Does(() =>

    Information("Publishing with FastLane");
    var jsonFile = $"../keystore/apiKey.json";
    var apk = $"buildDir/pkg.apk";

    Information("Publishing to google play with fastlane supply");

    rc = StartProcess("fastlane", new ProcessSettings 
        Arguments = new ProcessArgumentBuilder()
            .Append("supply")
            .Append("--json_key")
            .Append(jsonFile)
            .Append("--package_name")
            .Append(pkg)
            .Append("--apk")
            .Append(apk)
            .Append("--track")
            .Append("beta")
    );
);

Task("Default")
    .IsDependentOn("Publish");

RunTarget(target);

【讨论】:

以上是关于如何在 Jetbrains Rider 中构建 Android APK的主要内容,如果未能解决你的问题,请参考以下文章

如何在 JetBrains Rider 中添加 C# 解决方案文件?

如何在 JetBrains Rider 中修改“双选”代码的背景颜色?

如何在 JetBrains Rider 中添加/使用 C# 库?

如何在 Jetbrains Rider 工具箱中添加 Guna UI?

如何在 JetBrains Rider 设置中启用“隐式评估”?

JetBrains宣布Project Rider:一款C#跨平台IDE