如何在Jetbrains Rider中构建Android APK

时间:2019-12-17 14:39:47

标签: android xamarin rider

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

1 个答案:

答案 0 :(得分:0)

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

以下是此类Cake文件的示例:

#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);