Android 项目的 build.gradle
内部
task runAndroidApplication(type: Exec, dependsOn: ':installDebug') {
//TODO update Activity name below or find a way to get it from AndroidManifest.xml
if (System.properties['os.name'].toLowerCase().contains('windows')) {
// windows
commandLine 'cmd', '/c', 'adb', 'shell', 'am', 'start', '-n', "com.example.androidapptorun.MainActivity"
} else {
// linux
commandLine 'adb', 'shell', 'am', 'start', '-n', "com.example.androidapptorun.MainActivity"
}
}
如何从 AndroidManifest 获取默认 Activity 的主 Activity 值? (此外,如果有多个 Activity ,选择一个 Activity 的逻辑会使它变长, 而处理将在 Android 工具中进行)
Android 插件是否有更好的方法然后解析 AndroidManifest.xml
?
更新:https://github.com/novoda/gradle-android-command-plugin可以满足某些需求,但我需要无参数版本才能通过 http://marketplace.eclipse.org/content/gradle 在 Eclipse 中快速运行
最佳答案
您可以使用 XmlSlurper
类。
例子:
AndroidManifest.xml
<manifest package="com.example.your.app">
并在gradle中获取它
def manifest = new XmlSlurper().parse(file("AndroidManifest.xml"))
// returns "com.exmaple.your.app"
manifest.@package.text()
关于android - Gradle - 如何从 AndroidManifest 获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22979277/