0

Let's say we have Android app, compiled from files A, B, C. I want to print on screen source code from file A and if in the future i will make some changes in file A I want to print new state of this file . Let's assume that it's permissionable to print from file B or C (file A doesn't have to print itself). What is the most straightforward way to do this?

TIP. From my research i think it's possible with gradle task, which fill copy files from src/main/java to assets before compilation. Than we can read these files from assets in "standard" way. But this sound for me like a workaround. I don't want to have duplicates of java/kotlin files in my app. If i have classes A,B,C under src/main/java i want to print e.g source of class A from class B without these operations.

Mkr
  • 428
  • 2
  • 5
  • 16
  • 1
    Having it done with a Gradle task sounds like a reasonable solution - otherwise your app would only contain the compiled and dexed version of your code, but not the original sources. – zsmb13 Apr 25 '18 at 05:35
  • 1
    Yes, data like the classname and the method names and such are available at runtime, but these are read from the compiled code, your application doesn't contain the original source code as plain text (with all the formatting, comments, etc). – zsmb13 Apr 25 '18 at 06:03
  • [answer above should be probably below this question :)] I understand, but on the other side we can easy print e.g. class name like in sample here https://www.tutorialspoint.com/java/lang/class_getsimplename.htm So why going this way we can't print whole class with all functions and variables etc.? – Mkr Apr 25 '18 at 06:09
  • 1
    You can access and print the contents of classes through reflection, you just won't be able to access them in the exact same format as if you had the source code, and you'll have to spend quite a bit of time on figuring out how to format it if you want it to look similar to the original source. There will also be things you can't find at all, for example method bodies are [very difficult to get to](https://stackoverflow.com/questions/2946279/how-do-i-print-the-method-body-reflectively). – zsmb13 Apr 25 '18 at 06:29

1 Answers1

1

So why going this way we can't print whole class with all functions and variables etc.?

Simple answer: because it is not necessary for the program to run and therefore is stripped away by the compiler and even more by tools like proguard. So the code (of which you want the plaintext) you want to access might not even be in the resulting .aar any more at runtime.

Java is compiled to bytecode in a class file, you can read everything that is in there in some way.

From my research i think it's possible with gradle task, which fill copy files from src/main/java to assets before compilation. Than we can read these files from assets in "standard" way. But this sound for me like a workaround. I don't want to have duplicates of java/kotlin files in my app. If i have classes A,B,C under src/main/java i want to print e.g source of class A from class B without these operations.

This is not a workaround. You want to keep the information necessary to compile the program in the program, then you have to keep there it yourself (i.e. in a .zip file or sources.jar).

we can easy print e.g. class name like in sample here tutorialspoint.com/java/lang/class_getsimplename.htm

That is because this information is necessary for the public class to be instantiated i.e. by dependency injection.

leonardkraemer
  • 6,573
  • 1
  • 31
  • 54
  • Ok, so as I understand from all answers gradle and copying files isn't bad solution here. But to complete this topic and answer question from the beginning: "What is the most straightforward way to do this?" i'm going to investigate also if it wouldn't be the best just to add to project a simple java file with main() method, which will work as independent program copying source code to assets and relieve gradle in his hard life :). – Mkr Apr 25 '18 at 07:37
  • 1
    see https://stackoverflow.com/questions/11474729/how-to-build-sources-jar-with-gradle on how to build a sources.jar with gradle, then put this into assets and open and display from there. – leonardkraemer Apr 25 '18 at 07:43