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. Engineering 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
java-library groovy plug and plug can exist
according to the above-described problem should be noted, are the correct steps
1. Create a Library Module1 the Java
2. Modify the file build gradle Module1, as
the new folder groovy 3.src / main directory, and create a new file Test.groovy
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)
}
}
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
After synchronization, click Run
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:
2, scripted Groovy
1. Create a new file in the Test.groovy groovy same path as follows
2. Write groovy file, as follows:
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
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.
5. Now the toolbar below, click below the red section of the green triangle, groovy run the program
results are as follows
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:
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).
Groovy console to run a script class is very simple, just click the Run button in the upper left corner can be written
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
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
and then we just write a class in the new Groovy Console, the code is as follows:
Click Run, choose Run 'Test1.main()'
:
Select Run 'Test1.main()'
after the bomb box as follows:
as already mentioned, the above operations are carried out under my name is groovy Android Library module
after selecting module, the following tips:
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:
and then continue with the steps above, this time all OK,
click after the run, run as follows:
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:
although the build failure follows, but I did not bother:
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
then we add compileSdkVersion to build gradle file, as follows:
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