Search code examples
androidandroid-studiogroovylive-templates

android - groovy script error in custom live template


I am attempting to create a custom live template to bind views in activity_main.xml to MainActivity.java (w/ Butterknife) as global variables with the view's id as the variable name.

bindScript.groovy

//Arguments to be used in live template
def cN = _1  //'MainActivity' 
def pN = _2 //'com.example.myapp'
def bool = _3 // true or false depending on whether we want the id or the view type  


//Extract app Name from package. Assume that package name is name of project directory
def dN = pN.substring(pN.lastIndexOf(".") + 1).trim()

//Extract words from MainActivity camel-case. Assume layout file is activity_main.xml 
layoutFileWords = cN.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")

//Make layout name corresponding to activity class
def layoutName = layoutFileWords[layoutFileWords.size()-1].toLowerCase()
for (int i = 0; i<layoutFileWords.size() - 1; i++) {
    layoutName = layoutName + "_" + layoutFileWords[i].toLowerCase()
}
layoutName = layoutName + ".xml"

//Create layout directory path from package name
def layoutDir = "C:\\Users\\" + userName + "\\AndroidStudioProjects\\" + dN 
+ "\\app\\src\\main\\res\\layout\\"

//Full layout name and qualified paths
def layoutFile = layoutDir + layoutName


//Read Layout File, parse XML and get Elements that have attribute "android:id" 

// Assume we want to inflate all views with attribute "android:id"d
def String fileContents = new File(layoutFile).text

def layout = new XmlSlurper().parseText(fileContents)

def nodes = layout.depthFirst().findAll{
    it["@android:id"].text().startsWith("@+id/")
}

def viewTypes = []
def ids = []
nodes.each { node ->
//    println "Node Element: ${node.name().substring(node.name().lastIndexOf(".") + 1).trim()}"

//    println "Node id: ${node["@android:id"].text().substring(5)}"
    viewTypes.add(node.name().substring(node.name().lastIndexOf(".") + 1).trim()) 

    ids.add(node["@android:id"].text().substring(5))
}

//return either View Type or id, depending on live template "bool" argument
if (bool) {
    viewTypes[0]
} else {
    ids[0]
}

and here is the live template

@Bind(R.id.$ID$)
$VIEWTYPE$ $ID$;

Name     |  Expression 
ID       |  groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), false)
VIEWTYPE |  groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), true)

and here is the activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="85dp"
        android:layout_height="43dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Players"
        android:textAlignment="center"/>
</LinearLayout>

When I run the groovy script in Android studio's groovy console with the hardcoded arguments, I return exactly what is expected. However, when I try to run the live template I get this in the MainActivity

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.No such property:UserssnajerabindScript for class:Script1)
    No such property:UsersuserNamebindScript for class:Script1   
    No such property:UsersuserNamebindScript for class:Script1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
}

I am pretty sure I am passing my groovy script arguments correctly, but I must admit that I am new to groovyScripts in Live Templates as per the docs so any help resolving this error is much appreciated.


Solution

  • I think you need to escape the backslashes:

    ID       |  groovyScript("C:\\Users\\userName\\bindScript.groovy", className(), currentPackage(), false)
    VIEWTYPE |  groovyScript("C:\\Users\\userName\\bindScript.groovy", className(), currentPackage(), true)