7

一开始我不得不说我的英语不是很好,所以如果我能很好地解释我的意思,我很抱歉:)

我有一个项目需要复制 n 次;每个新项目必须具有相同的源代码,但不同的资源(例如:图像、html 文件、声音、pdf 等)和不同的类/包名称。

我的项目不是标准的java,而是android + phonegap。我有一个创建一个空的phonegap项目的eclipse插件......也许有办法修改这个插件来创建我的标准项目?

可能吗?最好还有一个系统来将主项目的源代码更改提交给子项目,但这不是强制性的。

再次对不起我的英语。

编辑:

对不起,如果我再次编辑这个问题,但我真的找不到我的问题的解决方案。

我想将它与一个示例集成,也许我可以解释我需要什么。

想象一下,您已经为一支足球队(例如巴塞罗那)开发了带有 eclipse 和 phonegap 的 android 应用程序。

该应用程序主要是html + jquerymobile,但是您修改了活动,android清单,添加了一些phonegap插件,一些媒体资源等。

现在你必须为更多团队、很多团队复制这个应用程序。对于每个人,您必须创建一个新的 phonegap 项目,修改每个文件,添加插件,添加资产……这些任务不可能没有错误。

但最大的问题是:如果你的代码只有一点点更新,你如何在 10/20/50/100/1000 个项目中复制它?

为了更具体,我也在帖子中添加了 android、phonegap 和 cordova 标签。

再次对不起我的英语。

编辑 N°2

我刚刚玩了一个多星期的 maven android 插件,但没有成功。我需要的是集中式代码,我可以在其中切换应用程序和包名、图标以及少量配置文件。

Android libs 不是解决方案,因为它无法导出资产文件。

我开始为这个问题寻找一个详细的答案......请帮助:(

4

5 回答 5

7

从维护的角度来看,我不会考虑基于相同的代码库复制 1000 个项目。我将使用单个项目来管理特定于客户的资源,并在项目构建阶段交换所需的资源。换句话说,1 个项目构建 1000 个 apk,而不是 1000 个项目构建 1000 个 apk。

根据您在问题中提供的详细信息,解决这种使用场景(从单一源库构建多个应用程序)的正确方向(据我所知)是采用外部构建工具,如 Ant 或 Maven 主导构建进程(两者都提供了在整个构建生命周期中对每个步骤进行精细控制的能力,即编译、dex、打包等),如果需要,还可以编写用于批量构建的 shell 脚本。

我不是PhoneGap的乐趣,但快速浏览一下它的入门指南,根据我的理解,它只是Android SDK之上的另一个编程层堆栈,提供主要使用Web编程语言编写移动应用程序的能力( HTML、CSS、Javascript),应用程序仍然保留原始项目框架,所以我不会期望编写 Ant/Maven 脚本来构建 PhoneGap 应用程序有太多麻烦。

一旦你通过 Maven 成功构建了你的 PhoneGap 应用程序。您可以开始研究如何解决您的场景,我之前在 StackOverflow 中看到过类似的场景,请查看此线程此线程以进行一些案例研究。您可以启动概念验证项目以进行可行性研究。

我已经发布了一些示例代码,展示了 Maven 如何实现这一点,见下文。

示例项目目录结构:

myApp/
  src/
  res-barcelona/
  assets-barcelona/
  res-realmadrid/
  assets-realmadrid/
  ... ...
  project.properties
  AndroidManifest.xml
  pom.xml

示例 pom.xml 文件:

<profiles>
  <profile>
    <id>barcelona</id>
    <properties>
      <app.package.name>com.company.app.barcelona</app.package.name>
      <app.res.dir>${project.basedir}/res-barcelona</app.res.dir>
      <app.assets.dir>${project.basedir}/assets-barcelona</app.assets.dir>
    </properties>
  </profile>
  <profile>
    <id>realmadrid</id>
    <properties>
      <app.package.name>com.company.app.realmadrid</app.package.name>
      <app.res.dir>${project.basedir}/res-realmadrid</app.res.dir>
      <app.assets.dir>${project.basedir}/assets-realmadrid</app.assets.dir>
    </properties>
  </profile>
  ... ...
</profiles>

....

<plugins>
  <plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <version>3.1.1</version>
    <extensions>true</extensions>
    <configuration>
      <sdk>
        <platform>13</platform>
      </sdk>
      <undeployBeforeDeploy>true</undeployBeforeDeploy>
      <renameManifestPackage>${app.package.name}</renameManifestPackage>
      <resourceDirectory>${app.res.dir}</resourceDirectory>
      <assetsDirectory>${app.assets.dir}</assetsDirectory>
    </configuration>
  </plugin>
  ... ...
</plugins>

要构建 app-barcelona.apk,请运行mvn clean install -Pbarcelona.
要构建 app-realmadrid.apk,请运行mvn clean install -Prealmadrid.
要构建其他 1000 个 apk,请编写 shell 脚本。

Android Maven 插件提供了许多配置,让您可以在每个阶段/目标精细控制构建过程。查看项目文档以获取完整的详细信息。

于 2012-04-20T04:55:19.793 回答
3

Hope I got your right when assuming you're more looking for a general way how to do it then an actual list of files you've to change. Because this is something you'll probably won't come around to figure it out by yourself. (THough android projects are well structured it might be enough to just change the res/, asserts/ folder and the android.manifest file. I merely used anything more on my Phonegap projects.

So my weapon of choice would be git branches.
(Some words about git if you've not already heard about it.)

Given the last commit represents your finished application. From here you can fork the project for a (e.g. Valencia CF) release. Make sure that you only change the resources but nothing in the main program! It is important that your main development branch remains the most developed version!

So after releasing the second and third app you might want to do some bug fixes. You'll switch back into your development branch and continue to make your code even better. Hitting the next release you'll again branch of for all the other teams you want to gives updates to.
So you have a couple of branches with the updated version but the same resources on the one hand and already styled apps all on your old version.

If you have a charming git client you could easily pick the files you want to changes. This takes varies from easy to "wtf-MOAH-pcCra$h throwing away the computer" if you have a lot of individual files to merge but not simply big folders + few files like res/ and android.manifest.

Another possibility would be to use a git feature called cherry-pick. I've never used it but the description sounds pretty much like what you want:

Given one or more existing commits, apply the change each one introduces, recording a new commit for each. This requires your working tree to be clean (no modifications from the HEAD commit).

(Here's the rich SO thread where I've picked that it up.)

You have your clean working tree since you just branched of your new version to re-apply the Valencia CF design. Therefore the HEAD is clean and you can cherry-pick the commits from the old version where you've applied the design already.
Very probably something has changed from the old to the new version which requires to update the resources. (Some views went but other got introduced for example). However just go ahead and get it done but make sure to commit your final result because this commit will be the cherry-pick for the even better and greater version which you've already started to work on in your developing branch.

Though not directly related to your question I also would you recommend to read A successful Git branching model which gives you another idea how you can use branches to your favour.

Anyway I don't think it's the only possible solution but for it seems to be the most convenient method. Since all the magic you do is in the project folder you can just copy and paste them and edit the stuff manually or via clever programs. But I'm not sure where the Eclipse project files are stored and also copying them will probably let Eclipse freak out a little bit. (Same project name but wrong project path etc.). Creating new Eclipse project all the times also sounds like a lot of work to me. While git cooperates independently from your IDE (it's nothing more than an extra folder in the project.) you can just close Eclipse switch the branches via git and reopen the program and you've got the changes you applied. (OK, you've might have to right click on the project and hit refresh ... right.)

于 2012-04-19T20:17:38.293 回答
3

正如您已经发现的那样,您需要一个共享的Android 库项目。不幸的是(正如您已经提到的)assets仅在使用 Android Maven Plugin构建版本时继承到依赖项目。

在我们的项目中,我们syncAssets.xml通过 Eclipse 中的 ADT 为开发人员构建创建了一个 Ant 任务,该任务位于 Android 库项目的基本文件夹中:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This is an Ant build script to synchronize assets from Android library "pkonvert-base"
    because they are not included automatically by the ADT tools. See discussion
    at http://stackoverflow.com/questions/5889833/android-library-assets-folder-doesnt-get-copied
    and topic "Library projects cannot include raw assets" at http://developer.android.com/guide/developing/projects/index.html#considerations.
    The Maven build through the maven-android-plugin automatically includes dependent
    assets from referenced Android libraries, so this Ant script only has to
    be executed when the build is executed from within eclipse. -->
<project name="syncAssets" default="syncAssets" basedir=".">

    <property name="sync.source.dir" value="${ant.file}/../assets" />
    <property name="sync.target.dir" value="${basedir}/assets" />

    <target name="syncAssets" description="Synchronize assets from Android library pkonvert-base">
            <echo>Synchronizing assets from ${sync.source.dir} to ${sync.target.dir}</echo>
            <sync todir="${sync.target.dir}">
                    <fileset dir="${sync.source.dir}" includes="**" />
            </sync>
    </target>

    <target name="clean" description="Clean up assets">
            <delete dir="${sync.target.dir}" />
    </target>
</project>

此 Ant 任务将库项目文件夹的内容同步assets到项目assets文件夹中,并在触发 Eclipse 中的新构建时触发。要将此脚本的执行添加到您的 Android 项目,请转到Properties -> Builders并创建一个新的Ant Builder,它必须是要调用的所有构建器链中的第一个(使用向上按钮向上移动 Ant 构建器)。

在 Tab Main中选择 Buildfile${workspace_loc:/<YOUR_LIBRARY_PROJECT>/syncAssets.xml}和 Base directory ${workspace_loc:/<YOUR_PROJECT>}

在选项卡刷新中,选择完成后刷新资源特定资源,然后选择要刷新的项目文件夹。此外选择Recursively include subfolders

在选项卡目标中确保默认目标在 clean手动构建自动构建之后执行。目标 clean 应该在clean期间执行。

于 2012-04-22T12:09:44.080 回答
0

PhoneGap 的 AppLaud Eclipse 插件的源代码可在此处获得。创建项目的初始内容来自这个资源目录。您可以将内容修改为您想要的任何内容,然后重新构建插件。由于代码具有 EPL 许可证,因此您必须开源您修改的任何内容。

于 2012-04-21T21:35:50.553 回答
0

您可以将代码放在一个项目中并创建一个 jar 文件。比将此项目或 jar 作为依赖项添加到您的“自定义资源”项目。

于 2012-04-13T15:08:07.180 回答