是否可以在构建期间动态指定包名称? [英] Is it possible to specify package name dynamically during build?

查看:19
本文介绍了是否可以在构建期间动态指定包名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时将调试版本和发布版本部署到我的设备上.如果我在构建之前手动更改清单中的包名称,我可以这样做,例如改变

I'd like to deploy both Debug and Release builds to my device at the same time. I can do this if I manually change the package name in the manifest before I build, e.g. change

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="my.package">

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="my.packageDEBUG">

但是,我希望在构建解决方案时自动执行此操作.如果我进行调试构建,它将在清单中使用 package="my.packageDEBUG" 构建.

However, I'd like to do this automatically when I'm building the solution. If I do a Debug build, it'll build with package="my.packageDEBUG" in the manifest.

这可能吗,如果可能,怎么做?

Is this possible, and if so, how?

推荐答案

您需要为您的项目创建自定义预构建事件.

You'll need to create a custom Pre-Build Event for your project.

右键单击您的项目并选择属性...

Right-click on your project and select Properties...

然后单击 Build Events 并将其添加到 Pre-build event 文本框:

Then click on Build Events and add this to the Pre-build event textbox:

PowerShell -File "$(SolutionDir)Update-PackageName.ps1" $(ProjectDir) $(ConfigurationName)

复制以下 PowerShell 脚本并将其作为 Update-PackageName.ps1

Copy the following PowerShell script and save it in your solution folder as Update-PackageName.ps1

param ([string] $ProjectDir, [string] $ConfigurationName)
Write-Host "ProjectDir: $ProjectDir"
Write-Host "ConfigurationName: $ConfigurationName"

$ManifestPath = $ProjectDir + "PropertiesAndroidManifest.xml"

Write-Host "ManifestPath: $ManifestPath"

[xml] $xdoc = Get-Content $ManifestPath

$package = $xdoc.manifest.package

If ($ConfigurationName -eq "Release" -and $package.EndsWith("DEBUG")) 
{ 
    $package = $package.Replace("DEBUG", "") 
}
If ($ConfigurationName -eq "Debug" -and -not $package.EndsWith("DEBUG")) 
{ 
    $package = $package + "DEBUG" 
}

If ($package -ne $xdoc.manifest.package) 
{
    $xdoc.manifest.package = $package
    $xdoc.Save($ManifestPath)
    Write-Host "AndroidManifest.xml package name updated to $package"
}

祝你好运!

这篇关于是否可以在构建期间动态指定包名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆