android - groovy 自定义实时模板中的脚本错误
android - groovy script error in custom live template
我正在尝试创建一个自定义实时模板,以将 activity_main.xml 中的视图绑定到 MainActivity.java(带 Butterknife)作为全局变量,并将视图的 ID 作为变量名称。
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]
}
这是实时模板
@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)
这里是 activity_main.xml 文件
<?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>
当我 运行 在 Android 工作室的 groovy 控制台中使用硬编码参数 groovy 脚本时,我 return 完全符合预期。但是,当我尝试 运行 实时模板时,我在 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);
}
}
我很确定我正确地传递了我的 groovy 脚本参数,但我必须承认我是 groovy 根据 docs 实时模板中的脚本的新手所以任何非常感谢帮助解决此错误。
我认为你需要转义反斜杠:
ID | groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), false)
VIEWTYPE | groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), true)
我正在尝试创建一个自定义实时模板,以将 activity_main.xml 中的视图绑定到 MainActivity.java(带 Butterknife)作为全局变量,并将视图的 ID 作为变量名称。
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]
}
这是实时模板
@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)
这里是 activity_main.xml 文件
<?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>
当我 运行 在 Android 工作室的 groovy 控制台中使用硬编码参数 groovy 脚本时,我 return 完全符合预期。但是,当我尝试 运行 实时模板时,我在 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);
}
}
我很确定我正确地传递了我的 groovy 脚本参数,但我必须承认我是 groovy 根据 docs 实时模板中的脚本的新手所以任何非常感谢帮助解决此错误。
我认为你需要转义反斜杠:
ID | groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), false)
VIEWTYPE | groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), true)