0

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?

Dale
  • 5,520
  • 4
  • 43
  • 79
  • There is no source JAR in an Android app. There is no JAR of any type in an Android app. There are JARs in the project, but at runtime those are all gone, replaced by 1+ DEX files in the APK. So, what do you consider "the equivalent information" to be? – CommonsWare Jan 13 '22 at 17:26
  • 1
    Even in a standard JDK, there was never a guaranty that a class has been loaded from a jar file. – Holger Jan 14 '22 at 14:58
  • I clarified the question to explicitly identify specifics about dependencies as the goal. Getting the path to a jar file in a standard Java program was included in the question just to show that, at runtime, the programmer in that environment has access to specifics about dependencies. – Dale Jan 14 '22 at 19:22

1 Answers1

0

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
Dale
  • 5,520
  • 4
  • 43
  • 79