Android的 - 我如何动态地设置在构建时的包名的开源项目?开源、项目、动态、Android

2023-09-03 21:30:01 作者:〆、﹎生離ヤ

我工作的一个开源项目。由于它的目的,任何人都可以下载源,并建立它自己,我不想硬code包名的的任何地方的 - 其中包括目录结构

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.

我用蚂蚁建筑。显然,我可以修改的build.xml ,但我相信这是由 Android的更新覆盖。无论是用来将致力于Git的回购协议,它不应该是太复杂了。

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的回购code笔直的过程相当简单。下面是来自 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 倒是。由于包的名称将不会是任何其他地方,构建就会失败没有这样做。因此,需要有在 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.

编辑:当然,另外一个要求是,diff文件意义 - 他们不如果手动重命名包名

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

推荐答案

我做过类似(但不是这个原因),这需要更新的清单在生成时的东西。我完成了这个问题的方法是通过使第二AndroidManifest,并把它在一个名为目录配置。 因此,在配置/ 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-构建,改变这样的:

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文件中,可以放包的名称,版本名称/ code像这样:

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

现在你只需要确保你的清单,你完全符合所有的活动/服务/广播监听器等。这意味着你总是指定的完整包的源$ C ​​$ C。如果你想选择适合你自己的源$ C ​​$ C是动态的,你可以替换出每个prefixes每个类的..但是,这似乎有点傻..这是很容易打包code可高达下你自己的包名,他们可以通过简单地包括源或在他们的项目中一个罐子使用它从任何项目。

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