Manage code/build for Android app stores (Google/Amazon/etc)?

自作多情 提交于 2019-12-31 12:20:13

问题


I have an Android app that's downloaded primarily from Android Market (now, Google Play). We made a few tweaks to the source and also submitted to the Amazon App Store to see what sort of traction it gets. I'm now looking for a sustainable way to develop from a common code base and yet build so that I can submit to either/both.

Amazon's store has some restrictions about available APIs, and hence I'd like to conditionally remove/modify features from that version. Since Java doesn't support traditional conditional compilation, and conditionally including files in Eclipse doesn't seem trivial (is it even possible?), I wanted to ask what others are doing to solve this.

Admittedly, I'm no Eclipse/Java expert so feel free to school me.

What I'm looking for in a solution:

  • Building/debugging using Eclipse.
  • Static code files, with environment/settings toggles to control what to build.
  • No duplicate code or conditional logic in code to pick code flow at runtime

Is this something you've solved for Android apps specifically, or for other Java/Eclipse based projects? Suggestions for where to begin?


回答1:


It's quite easy to do in the newest versions of ADT (version 17), though I do find it makes compilation a bit longer:

  1. Create a new Android project (proj-A)
  2. Go to Project->Properties, select Android, and check "Is Library"
  3. Move all your common code to proj-A, import all the necessary libraries
  4. Create a new Android project for Google Play (proj-B)
  5. Go to Project->Properties, select Android, and add Proj-A to the Library
  6. Repeat #4&5 for the Amazon version

If you have some variables that should be set differently for each sub project (i.e. boolean GOOGLE_PLAY_VERSION to enable Google Play specific functions), you have to create another project to contain these values since you can't have projects that reference one-another in a circular fashion. You can solve this by adding the following steps:

  1. Pull all of your sub-project specific variables into one or more Classes that just serves as container(s) for these variables
  2. Create a "dummy" Java project (dummy)
  3. Config proj-A to add a new Source link to the bin directory of dummy
  4. Add the config Classes in each sub-project with project-specific changes
  5. Profits!

Note that the variables in dummy should not be set as final, otherwise it will override sub-project's setting.

This may seem like quite a bit of up-front work, but has worked quite well for me as far as version control goes.

Edit: Now with Google's move to Android Studio & Gradle, it may be better to move to that if you are starting a new project if you want to support multiple APKs, see Android dev site's Building Your Project with Gradle#Work with build variants. It definitely doesn't hurt to evaluate that option before deciding.




回答2:


Unfortunately, it's sort of a convention in Android to change flow at runtime based on what would be in C/C++-land conditional compilation.

Our app has to maintain different behavior for different API levels, so we've created some application-level constants that are initialized statically based on API-level information available to us, and used throughout the code. This is the way that Google does things in their examples (for example, see the ActionBarCompat compatibility library, and in particular the factory method used here).

You could create an interface CustomBuild, and implement it in AmazonBuild and GooglePlayBuild, then use a static getBuild() method to switch functionality as necessary:

if(getBuild().shouldEnableFeatureX()){
    doStuff();
} else {
    doDifferentStuff();
}

Then all you've got to worry about switching between builds is a line or two of code in the factory along with maintaining which things you want enabled in which versions. Or you could include a different version of a static class CustomBuild for each build.

I'm going to second the suggestion of others above re: switching to something like Maven for building; it should make your life much easier once you have it set up.

I'm also going to say you should make the core of the app a library as suggested above, and have two different modules (one for amazon, one for play store) that depend on the library but each only contain the one custom factory file (or just a static class for each type of build that contains the same "should I do this thing?" methods... once you have the infrastructure it's just a matter of preference).




回答3:


I haven't actually tried this yet, but it's something I've thought about.

How about using Eclipse's ability to link to files from a directory outside your workspace?

Start with one Eclipse project: for the sake of argument, say it's the Google Play version.

Now build a second project, beginning with asking Eclipse to link (not copy) the source files from your first project.

To develop the second project, add classes that subclass ones from the original project to realize your modifications. For resources, you can use some combination of includes, attribute overrides, and selectors.

Where it's not possible to subclass or extend, then obviously you'll have to just copy the original source file and hack on it. If you're really OCD about it, you can probably just maintain a patch set rather than a whole redundant set of files.

What do you think, will it work?




回答4:


You may create manually two projects in Eclipse pointing to the same source folders but with different inclusion/exclusion filters and different target directories.

Then two Ant targets using properties to switch excluded files from javac fileset are enough to generate corresponding jar files.

The aim is to get a clean application for each target, without any code from the other one.

With features listed as pluggable behaviors in a property file or XML configuration, your runtime will adapt itself with the addition of menu entries.



来源:https://stackoverflow.com/questions/9815038/manage-code-build-for-android-app-stores-google-amazon-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!