Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: multiple project from single source

at first i have to say that i'm not very good with english, so i'm sorry if i can explain very well what i mean :)

I have one project that i need to replicate n times; every new project must have the same source code, but different resources (ex: images, html files, sounds, pdf, etc) and different class/packages names.

my project is not a standard java, but android + phonegap. I have an eclipse plugin that create an empty phonegap project... maybe there is a way to modify this plugin to create my standard project?

Is it possible? The best is to have also a system to commit source changes form the main project to the childs, but it's not mandatory.

Sorry again for my english.

EDIT:

Sorry if i edit again this question, but really i can't find a solution for my problem.

I want to integrate it with an example, maybe i can explain what i need.

imagine you have developed and android application with eclipse and phonegap, for a football team, example Barcelona.

The app mainly is in html + jquerymobile, but you have modified the activity, the android manifest, added some phonegap plugins, some media resource, etc.

Now you have to replicate this app for more teams, a lot of teams. For everyone you have to create a new phonegap project, modify every single file, add the plugins, add the assets.... those tasks can't be error free.

But the very big problem is: if you have only a little update in your code, how you can replicate it over 10/20/50/100/1000 projects?

I've added android, phonegap and cordova tags too to the post to be more specific.

Sorry again for my english.

EDIT N°2

I just played with maven android plugin for over a week now, without success. What i need is centralized code where i can switcch the app and packagename, the icons, and just little configuration files.

Android libs is not a solution, because it can't export assets files.

I started a bounty for this question looking for a detailed answer... please help :(

like image 519
Laphroaig Avatar asked Apr 13 '12 15:04

Laphroaig


People also ask

How do I import the same project twice in Eclipse?

Instead of importing the project, begin creating a new project with a different name. Then change the location of the project to your new existing project. Eclipse will then let you create the project using the existing directory and content. In my opinion this is the only answer that directly addresses the question.

Can you open multiple projects in a single studio?

To open multiple projects simultaneously in Android Studio, go to Settings > Appearance & Behavior > System Settings, in the Project Opening section, choose Open project in new window.


3 Answers

From maintenance perspective, I would not consider replicating 1000 projects base on same code base. I would use a single project manage customer-specific resources and swap the required resource at project build phase. In another word, 1 project to build 1000 apks, not 1000 projects to build 1000 apks.

Based on the details you have given in the question, the right direction (as far as I can see) to solve this kind of use scenario (build multiple applications from single source base) is to adopt external build tools like Ant or Maven dominate build process (both provide the ability to fine-control on every single step i.e. compile, dex, package and etc. during the complete build life cycle) and perhaps write shell script for batch build if needed.

I am not a fun of PhoneGap but have a quick look through its Get Started Guide, base on my understanding, it is just another programming layer stack on top of Android SDK, to provide ability to write mobile app by mainly using web programming language (HTML, CSS, Javascript), the application still keep the original project skeleton so I would not expect much trouble to write Ant/Maven script to build the PhoneGap app.

Once you have successfully built your PhoneGap app by Maven. You can start investigating how to solve you scenario, I have seen similar sort of scenario before in StackOverflow, check out this thread and this thread for some case study. You can start a Proof of Concept project for feasibility study.

I've posted some sample code shows how Maven achieve this, see below.

Sample Project directory structure:

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

Sample pom.xml file:

<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>

To build app-barcelona.apk, run mvn clean install -Pbarcelona.
To build app-realmadrid.apk, run mvn clean install -Prealmadrid.
To build other 1000 apks, write shell script.

Android Maven Plugin provides many configuration let you fine-control build process at every single phase/goal. Check out Project Documentation for full details.

like image 105
yorkw Avatar answered Oct 19 '22 23:10

yorkw


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.)

like image 43
nuala Avatar answered Oct 20 '22 00:10

nuala


As you already found out, a shared Android Library project is what you need. Unfortunately (as you already mentioned) assets are only inherited to the dependent projects when building a release with the Android Maven Plugin.

In our project we created an Ant task syncAssets.xml for developer builds via ADT out of eclipse, that resides in the base folder of the Android Library project:

<?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>

This Ant task synchronizes the content of the library projects assets-folder into a projects assets-folder and is triggered whenever a new build in Eclipse is triggered. To add execution of this script to your Android project goto Properties->Builders and create a new Ant Builder that has to be the first one in the chain of all builders to be invoked (use Up button to move the Ant builder upwards).

In Tab Main select Buildfile ${workspace_loc:/<YOUR_LIBRARY_PROJECT>/syncAssets.xml} and Base directory ${workspace_loc:/<YOUR_PROJECT>}.

In Tab Refresh select Refresh resources upon completion and Specific Resources and choose your project folder to be refreshed. Furthermore select Recursively include subfolders.

In Tab Targets ensure that the default target gets executed after clean, on manual build and on auto build. Target clean should be executes during clean.

like image 31
Andreas Klöber Avatar answered Oct 19 '22 23:10

Andreas Klöber