1

I know we can write String value in build.gradle. Now I wonder that writing some specific java code in build.gradle. is it possible to do this? I have sample code below

@Override
        protected void onCreate(Bundle savedInstanceState) {
       
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.our_activity_main);

 if (jahwil(getApplicationContext().getPackageName(),"MD5").substring(100,200).compareTo("800") != 0) {
                this.finishAffinity();
            }

  private static String jahwil(String txt, String hashType) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance(hashType);
            byte[] array = md.digest(txt.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
            }
            return sb.toString().toUpperCase();
        } catch (java.security.NoSuchAlgorithmException e) {
        }
        return null;
    }

I want to write codes

 if (jahwil(getApplicationContext().getPackageName(),"MD5").substring(100,200).compareTo("800") != 0) {
                    this.finishAffinity();
                }

  private static String jahwil(String txt, String hashType) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance(hashType);
            byte[] array = md.digest(txt.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
            }
            return sb.toString().toUpperCase();
        } catch (java.security.NoSuchAlgorithmException e) {
        }
        return null;
    }

in build.gradle. Is it possible?

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
King Maf
  • 42
  • 1
  • 9

1 Answers1

1

The short answer is no, you can't. But the reason for this is that Gradle is not written in Java code but in Domain Specific Language or DSL. Like the comment from @a_local_nobody said, Gradle is using Groovy which is a dynamic language for the Java Virtual Machine (JVM). These files are created and you usually have everything you need in them.

For more information on how to build configuration files, read this.

EDIT:

Thanks to @a_local_nobody, as he said in a comment, you can use JavaExec to execute some Java code. It's still DSL in Gradle but it will execute Java code from another place. This can be used to pass some arguments to the app before it even starts. Check this answer: https://stackoverflow.com/a/36092290/14759470

SlothCoding
  • 1,546
  • 7
  • 14
  • 1
    well, i thought so too, but [maybe...](https://stackoverflow.com/questions/36071615/how-to-embed-java-code-into-gradle-build-using-javaexec-task) – a_local_nobody Feb 01 '21 at 09:24
  • Well, that still isn't Java code in build.gradle, that's why I said short answer. What he wants to do, I guess the answer is no. Exec is used to pass some arguments before the app is started and it executes the code in a different place. So to answer "Writing java code in build.gradle" - its not possible. As much as I know. – SlothCoding Feb 01 '21 at 09:31
  • yeah, i suppose on a technical level that's true (at least as far as i know, anyway) that you can't write java in gradle but it seems like you can _execute_ java in gradle which might achieve what OP wanted... or not – a_local_nobody Feb 01 '21 at 09:33
  • True, I'll add it to my answer because it's easier to be seen there for future lads who come here. – SlothCoding Feb 01 '21 at 09:40
  • 1
    thanks for the answer. I got some points now. groovy and java are different. build.gradle uses groovy. but we can have another solution using JavaExec. I need to learn more about this. thanks guys. – King Maf Feb 01 '21 at 23:21