In Java, a class dependency is required to perform a compilation of a source code file. However, running a compiled file without the reference will give is a ClassNotFoundException
. Consider the following:
import de.robv.android.xposed.XposedBridge;
................
................
private boolean testUseClassDirectly() {
try {
XposedBridge.log("blah");
return true;
}
catch(Throwable unused_ex) {
return false;
}
}
In this case it would compile only if the de.robv.android.xposed.XposedBridge
reference is found. I'm able to provide the reference in a java, class or jarfile for compilation purposes. However, generating the exception and handling it is a normal flow of the design of the app that performs certain checks and used the class directly or proceed in a certain way if it doesn't based on those checks.
How to I build the app in an Android environment (ideally Android studio) without including the class dependency in the APK file?
Unfortunately, there is no easy way to exclude just one third-party class. Nevertheless, you can exclude whole dependency.
If you want a library to be present at compile time but not at runtime, include it with compileOnly
instead of implementation
in your build.gradle
.
If you want to make 2 different builds (one with the library and another without it), you can create buildType
for it. For more details, you can see the docs on build variants.