I was wondering whether you could use a trick of the compiler to include different functions for a free and paid version of the app. For instance:
public static final boolean paid = false;
if (paid){
runPaidMethod();
}
else {
runFreeMethod();
}
The compiler will look at that and say that it doesn’t need the first branch of the if statement so it won’t compile it. Furthermore, it should look at the program and see that runPaidMethod() is no longer referenced from anywhere and remove it.
Therefore the question is: is it feasible to just have this flag, compile it once for free, swap the flag then compile it again for paid?
Using a final boolean variable is good because the Java compiler is smart enough to see that your condition is always false. If you decompile the compiled class (you can try it, with the
javap -ccommand) you will see that your code :will be compiled to a single call to :
The compiler removes any unreachable code, so nobody will be able to reverse engineer your app. But be careful, you have to declare
runPaidMethod()as a private method, or its content will still appear in the compiled class.However from a maintenance point of view, it is still better to use Library Projects to handle multiple app versions.