In normal Java, a programmer is able to inquire with String tmpPath = aClass.getProtectionDomain().getCodeSource().getLocation().getPath();
to determine the source jar for a class.
In Android's version of Java, this has been disabled, as indicated in getClass().getProtectionDomain().getCodeSource().getLocation().getPath() Throw a null pointer exception, which indicates, and I've confirmed aClass.getProtectionDomain()
always returns null.
Although traditional jar files are not a part of the Android build process, information as to what packages and what versions are being used can often be helpful to the programmer.
How can the programmer learn the equivalent information (about the sources and versions of dependencies) in Android as they can in other Java environments?
The approach below, using a gradle dependencies listing, does not give the programmer access to the dependencies at runtime, as does the approach with other Java environments that use jar files, but should be a reflection of what dependencies and versions are in effect during runtime.
One way to get a listing of dependencies is the following:
Android Studio
Terminal
Typed Gradle Command (Windows)
gradelw -q app:dependencies
The above command will generate "too much" output, containing much more than the runtime classpath. It's possible to look through the output and find the portion of interest. In my case, it was: releaseRuntimeClasspath - Resolved configuration for runtime for variant: release
, so the way to get only the target output is using the command:
gradlew -q app:dependencies --configuration releaseRuntimeClasspath