How to run Android Studio groovy program

Run the program groovy

First by a few caveats
1. The module is best not to other types of outside Java Libray module, groovy run the program, but also issues like the script little, if at the time of groovy program GroovyConsole run java class, it is not obvious executed.

Because it is not compatible with android groovy plug-ins and plug-ins can not be used on Android groovy relevant module. Here Insert Picture DescriptionEngineering build failure for the following reasons:
Caused by: com.android.build.gradle.internal.BadPluginException: The 'java' plugin has been applied, but it is not compatible with the Android plugins.

2. Although not recommended outside Java Library groovy other modules run the program, but as long as the build gradle file for the module replaced by the following content, scripts and java class or classes groovy program can do, but it also lost the module the significance of it, so it is recommended to use the Java
Library groovy run program, of course, if just learning groovy are called.

apply plugin: 'groovy'

dependencies {
       implementation fileTree(dir: 'libs', include: ['*.jar'])
       implementation localGroovy() 
} 

apply plugin: 'groovy' will introduce plug-groovy, groovy plug a java plugin.
implementation localGroovy () will introduce Gradle Groovy brought the library.

Android or above and have said groovy widget not exist, i.e., the mode is not allowed this FIG
Here Insert Picture Description
java-library groovy plug and plug can exist
Here Insert Picture Description
according to the above-described problem should be noted, are the correct steps

1. Create a Library Module1 the Java
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
2. Modify the file build gradle Module1, as
Here Insert Picture Description
the new folder groovy 3.src / main directory, and create a new file Test.groovy
Here Insert Picture Description
Here Insert Picture Description

1, running a Java-like Groovy
After completing the steps above, write demo below, click to run

class Test{
    static void main(String[] args){
        def word = "Hello World"
        println(word)
    }
}

Here Insert Picture Description
Although the operation was successful, but suggested that Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
this version uses deprecated Gradle features that make it incompatible with Gradle 6.0.

Back build.gradle files in the current module, add the following code:

task renameGroovyToJava {
    doLast{
        delete "$buildDir/classes/java"
        File file = new File("$buildDir/classes/groovy")
        println file.renameTo("$buildDir/classes/java")
    }
}

compileJava.finalizedBy compileGroovy
compileGroovy.finalizedBy renameGroovyToJava

Here Insert Picture Description
After synchronization, click Run
Here Insert Picture Description
1.1 principle
click Run, by looking android studio console information discovery, it performs compileJava Task, and then attached to the back of the compileGroovy Task compileJava, after compileGroovy task is completed, then the build / classes / groovy heavy folder named java. So that android studio class file can be executed after the groovy compiler.

Before and after comparison are as follows:
Here Insert Picture Description
Here Insert Picture Description
2, scripted Groovy

1. Create a new file in the Test.groovy groovy same path as follows
Here Insert Picture Description
Here Insert Picture Description

2. Write groovy file, as follows:
Here Insert Picture Description
3. find this place toolbar, select Edit Configurations...the pop-up page, click the plus sign in the upper left corner Add New Configuration, then select groovy
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

4. Then only need to fill in Name, Script path can, Name casually write to the same file name with groovy, Script path to choose the path above MyGroovy class.
Here Insert Picture Description
5. Now the toolbar below, click below the red section of the green triangle, groovy run the program
Here Insert Picture Description
results are as follows
Here Insert Picture Description
Note
configuration script groovy class, the same can be used to execute Java classes groovy, java class configuration but can not run groovy script class. Groovy Test.groocy above named Java class according to the class groovy script to configure the configuration operates as follows:
Here Insert Picture Description
Here Insert Picture Description

Groovy console

android studio title bar into the Tools / Groovy Console. Here you can also run directly groovy program (java script class and the class can be).
Here Insert Picture Description
Groovy console to run a script class is very simple, just click the Run button in the upper left corner can be written
Here Insert Picture Description
on the map is created after clicking the Groovy Console, click the top left corner to edit the code to run the triangle results are as follows
Here Insert Picture Description
Groovy console to run java class is more trouble, I that there is no need to run java classes in groovy console, the following pit and I still talk about the steps I stepped on it

I module created before that Android Library, the module name is groovy, then build gradle file the Android plug removed, as shown below
Here Insert Picture Description
and then we just write a class in the new Groovy Console, the code is as follows:
Here Insert Picture Description
Click Run, choose Run 'Test1.main()':
Here Insert Picture Description
Select Run 'Test1.main()'after the bomb box as follows:
Here Insert Picture Description
as already mentioned, the above operations are carried out under my name is groovy Android Library module
Here Insert Picture Description
after selecting module, the following tips:
Here Insert Picture Description
you must also create a class file named Test1, then we ask what Groovy Console also use it directly run java groovy kind of program on the list
I have here a Test.class before, directly to the console inside of a name change, as follows:
Here Insert Picture Description
Here Insert Picture Description
and then continue with the steps above, this time all OK,
Here Insert Picture Description
click after the run, run as follows:
Here Insert Picture Description
the above is a normal process, no matter what type of module, as long as the build gradle according to the above configuration, and then step by step execution are functioning properly.

But before I put groovy plugin and Android plugin put together, as follows:
Here Insert Picture Description
although the build failure follows, but I did not bother:
Here Insert Picture Description
this time we still according to the above Groovy Console execute java class program normal steps, you will be prompted as follows: Cause: compileSdkVersion is not specified. Please add it to build.gradle
That Cause: designated compileSdkVersion, add it to build.gradle
Here Insert Picture Description
Here Insert Picture Description
then we add compileSdkVersion to build gradle file, as follows:
Here Insert Picture Description
once again the implementation of java class program in accordance with the Groovy Console normal steps, the following tips: The 'java' plugin has been applied, but it is not compatible with the Android plugins.
have applied the "java" plug-in, but it Android is not compatible plug-ins. Or build a prompt start of the problem, so that the best execution groovy program in Java Library

Here Insert Picture Description
Here Insert Picture Description

Published 18 original articles · won praise 1 · views 774

Guess you like

Origin blog.csdn.net/aha_jasper/article/details/104810684