Reputation: 1380
using Eclipse (to develop on Android) I want to create a second project that it is the same as a first project but some classes are differents.
Exactly, I'm creating an Android App and I want to offer a free version and a premium version. Actually, the code of the projects are identical but changes some classes.
The problem is I don't want support two projects. If I modify a class, then I have to modify the same change on the other project. It is very redundant.
Besides, the project are pushed to a remote GIT respository.
And one last note, an Andriod App is identify using his root package, for example: com.example.myapp
So, I need two differents roots package (so I need create new folders), for example com.example.myappfree and com.example.myapppremium
Which is the best solution to create two java projects in Eclipse sharing the package and classes, but changing two o three classes?
Upvotes: 0
Views: 279
Reputation: 75635
This is not that trivial as it should be, but you may want to utilize Library feature of Android projects with Eclipse:
http://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject
Upvotes: 1
Reputation: 133560
Make your project that you wish to refer in other andorid projects as a library project. You cn refer to the library projects in your android projects
To make your project a library project. Rigth click on project. Goto properties choose android. Select Is Library. click apply and click ok
Hers's a example in the link where i have posted snap shots of how to refer actionbarsherlock whcih is a library project in an Android project.
import .R cannot be resolved when I import actionbarsherlock.
A link to the developer site
http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject
Upvotes: 1
Reputation: 7798
You can use a Library Project to do that. Basically you will take your main project which will take all the code and convert it into a Library. Create two new projects, the free and the paid version.
Read here how you can set up a library project.
You have to make sure both free and paid projects have different package names, and on your library project, you read the package name and verify if you are running the free or the paid version.
Here you can see a working example I have published. The whole source is on the library project and the others only use different resources.
Check out the PackageSelector class. It shows how to detect which version is running, and then it's simple to change the app accordingly.
public class PackageSelector extends Application {
public int getVersion() {
if (getPackageName().toLowerCase().contains("pro"))
return LanguagePickActivity.FULL;
else if (getPackageName().toLowerCase().contains("macedonian"))
return LanguagePickActivity.MACEDONIAN;
else if (getPackageName().toLowerCase().contains("german"))
return LanguagePickActivity.GERMAN;
else // if (getPackageName().toLowerCase().contains("spanish"))
return LanguagePickActivity.SPANISH;
}
}
On your activity:
public void getVersion() {
Country = ((PackageSelector)getApplication()).getVersion();
}
And when you have something relevant to a single version:
if (Country == LanguagePickActivity.FULL ) {
//Do something only the paid version has
finish();
}
else {
//Do something on the free version
finish();
}
Upvotes: 1
Reputation: 5876
Create a common code project and reference the project from the two other projects.
Upvotes: 2