Android - 如何在构建时为开源项目动态设置包名称? [英] Android - How do I dynamically set the package name at build time for an Open-source project?

查看:32
本文介绍了Android - 如何在构建时为开源项目动态设置包名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个开源项目.因为任何人都可以下载源代码并自己构建它,所以我不想硬编码包名称​​任何地方 - 包括目录结构.

I'm working on an Open-source project. As it is intended that anyone can download the source and build it themselves, I do not want to hard-code the package name anywhere - including the directory structure.

我使用 ant 进行构建.显然我可以修改 build.xml,但我相信这会被 android update 覆盖.使用什么都会提交到Git repo,不要太复杂.

I use ant for building. Apparently I can modify build.xml, but I believe this is overwritten by android update. Whatever is used will be committed to the Git repo, and it should not be too complicated.

目前,直接从 Git 存储库构建代码的过程相当简单.以下是 README 文件的摘录:

Currently the process to build the code straight from the Git repo is fairly simple. Here's an excerpt from the README file:

$ cd ~/src/isokeys/IsoKeys
$ android list targets # I build against API level 10.
$ android update project --name IsoKeys --target 1 --path ./ # Only needed first time.
$ ant debug && adb -d install -r bin/IsoKeys-debug.apk

对我来说,将包名放在 local.properties 中是有意义的,因为这是 .gitignore'd.由于包名不会在其他任何地方,如果不这样做,构建就会失败.因此,README 中至少需要多出 1 个步骤,但我想尽量减少.

To me, it makes sense to put the package name in local.properties, because this is .gitignore'd. As the package name won't be anywhere else, the build will fail without doing this. So there needs to be at least 1 extra step in the README, but I want to keep it to a minimum.

当然,另一个要求是差异有意义 - 如果您手动重命名包名称,差异就没有意义.

Of course, another requirement is that diffs make sense - which they don't if you manually rename the package name.

推荐答案

我做了类似的事情(但不是因为这个原因),需要在构建时更新清单.我完成此操作的方法是制作第二个 AndroidManifest 并将其放在名为 config 的目录下.所以在 config/AndroidManifest 你可以有这样的东西:

I did something similar (but not for this reason) which required updating the manifest at build time. The way I accomplished this was by making a second AndroidManifest and putting it under a directory named config. So in config/AndroidManifest you could have something like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="@CONFIG.APP_PACKAGE_NAME@"
      android:versionCode="@CONFIG.APP_VERSION_CODE@"
      android:versionName="@CONFIG.APP_VERSION@">


<!-- EVERYTHING ELSE GOES HERE -->


</manifest>

然后您可以使用常规的裸骨 build.xml ant 脚本,只需进行一些修改(无需从 android 构建系统复制整个脚本,因为它们添加了一些钩子供您使用,而无需重新发明轮子).默认情况下,构建脚本应该读取 local.properties,但如果没有添加(或取消注释)如下一行:

Then you can use the regular bare bones build.xml ant script with just a few modifications (no need to copy the whole script from the android build system as they added some hooks for you to use without reinventing the wheel). The build script should be reading local.properties by default, but if not add (or uncomment) a line like this:

<property file="local.properties" />

在您的构建脚本中,您应该看到一个名为-pre-build"的任务,将其更改如下:

In your build script you should see a task called "-pre-build", change it like this:

<target name="-pre-build">
     <copy file="config/AndroidManifest.xml" todir="." overwrite="true" encoding="utf-8">
       <filterset>
          <filter token="CONFIG.APP_PACKAGE_NAME" value="${app.packagename}" />
          <filter token="CONFIG.APP_VERSION" value="${app.version}" />
          <filter token="CONFIG.APP_VERSION_CODE" value="${app.versioncode}" />
       </filterset>
     </copy>           
</target>

然后你的 local.properties 文件,你会像这样输入包名、版本名/代码:

Then your local.properties file you would put the package name, version name/code like so:

app.version=1.0
app.versioncode=1
app.packagename=com.mypackage.name

现在您只需要确保在清单中完全限定所有活动/服务/广播侦听器等.这意味着您始终指定源代码的完整包.如果您希望您自己的源代码的包是动态的,您可以替换每个类的每个前缀.. 但这看起来有点傻.. 很容易将您的代码打包到您自己的包名下,并且它们只需在项目中包含源代码或 jar,即可在任何项目中使用它.

Now you just need to make sure in your manifest that you fully qualify all of your activities/services/broadcast listeners etc.. That means you always specify the full package of your source code. If you want the package for your own source code to be dynamic you could replace out each of the prefixes to each class.. But that seems kind of silly.. It is easy enough to package your code up under your own package name and they can use it from any project by simply including the source or a jar in their project.

-- 更新--哦,你可以做的另一件事是通知用户他们必须定义一个包名称是在你的构建 xml 中使用失败标记,如下所示:

-- UPDATE -- Oh and one other thing you can do to notify the user that they must define a package name is use the fail tag in your build xml like this:

<fail message="app.packagename is missing. This must be defined in your local.properties file" unless="app.packagename" />

将其放在读取 local.properties 文件的行之后

Put this after the line which reads the local.properties file

这篇关于Android - 如何在构建时为开源项目动态设置包名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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