Tertium
Tertium

Reputation: 6308

android: use project's final static variable from inside the library, or some other tricks to exclude unused code

I use android library (library project, not jar) to maintain 2 target projects (free and paid) with 1 code base. There is no code at all in target projects, all code is in library.

I need to add code blocks inside library:

if (main.liteVersion)
{
  //lite version code here
}
else
{
  //full version code here
}

As I know if variable in condition would be final static java compiler will exclude unused code (full version code if main.liteVersion is true). That's what I need. Because I don't want sobody decompile lite version and see code of the paid one. I know it's not a def, but I just don't include paid version code in free version binary.

But I can't add final static var in target project and then see it in library.

There are some ways to determine in which target project we are. We could read current package name to determine which version is running. If we add config class in target project we could use reflection in library to get a cfg value. Also we can add resource in target project, but it will be added to com.tertium.lite.R, but in library we can read only com.tertium.library.R (of course using reflection we can read anything). But such "dynamic" methods will not make compiler exclude unused code (in condition will be run-time calculated value).

We can rewrite config class inside library project while build (with ant or shell task) and then we'll have "natural" static setup, but I'd prefer not to touch library.

Just in case - I use eclipse helios, android versions - 1.6-4.2.

Any ideas? Thanks!

UPDATE: it's not actual anymore, since I've refused from using project as a library and have developed seamless ant-based resource-compile-and-build workflow which supports all needed configurations. Moreover, as far as I know my original question has no valid answer.

Upvotes: 0

Views: 588

Answers (2)

Philippe Banwarth
Philippe Banwarth

Reputation: 17725

You can remove unused code using ProGuard (on the application projects, not on the library project). If there is no code at all in the application projects, you may need to resort to different proguard-project.txt files in each application project and make sure you don't keep any paid entry point in the free version

ProGuard rules are a bit tricky, you can see wich classes and methods have been kept in the proguard/mapping.txt file

Upvotes: 0

xvorsx
xvorsx

Reputation: 2442

I recommend you make it step of you build system (ant, maven, make, etc). I hope, there are only few such pieces of source, so you can add file replacement manual as build step.

Don't make this compiler work: it's hard to control in long-terms. Compiler options/version will change and behaviour can change as well.

Upvotes: 1

Related Questions