Gradle - 如何从AndroidManifest获取值?

7
在 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的主要活动值? (如果有多个活动,则制定选择一个的逻辑会使其变得很长, 而处理将在Android工具内进行)
是否有比解析AndroidManifest.xml更好的android插件方法?
更新:https://github.com/novoda/gradle-android-command-plugin可能适合某些需求,但我需要无参数版本以通过http://marketplace.eclipse.org/content/gradle快速运行Eclipse。

你看到了 http://stackoverflow.com/questions/10187556/reading-android-manifest-file 吗? - Shayan Pourvatan
这是在应用程序运行时从AndroidManifest.xml读取。问题涉及构建系统gradle,即编译时间,不涉及Android类。 - Paul Verest
发现了 https://dev59.com/NGbWa4cB1Zd3GeqPVEvr 和 XmlSluper http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper。这不是一个很干净的解决方案... - Paul Verest
gradle-android-command-plugin[1]可以为您完成此操作。但是目前它不支持Windows。[1]https://github.com/novoda/gradle-android-command-plugin - rciovati
感谢提供这个好链接。也许将来他们会为 manifest.xml 提供一个不错的 API。 - Paul Verest
2个回答

11
你可以使用 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()

谢谢,这非常有帮助。 - Miles Peele
4
您可以使用android.sourceSets.main.manifest.srcFile代替"AndroidManifest.xml"自动定位清单文件。 - Ilia Kurtov

6

我刚刚为ADT 20(L),Gradle 1.12和com.android.tools.build:gradle:0.12.2编写了这个。

它适用于flavors,build types(不要忘记myBuildType.initWith(existingBuildType))和applicationIdSuffix

将以下内容放在android { ... }的末尾:

applicationVariants.all { variant ->
    if (variant.install) {
         tasks.create(name: "run${variant.name.capitalize()}", type: Exec, dependsOn: variant.install) {
            description "Installs the APK for ${variant.description}, and then runs the main launcher activity."
            def getMainActivity = { file ->
                new XmlSlurper().parse(file).application.activity.find{ it.'intent-filter'.find{ filter ->
                    return filter.action  .find{it.@name.text() == 'android.intent.action.MAIN'} \
                        && filter.category.find{it.@name.text() == 'android.intent.category.LAUNCHER'}
                }}.@name
            }
            doFirst {
                def activityClass = getMainActivity(variant.processManifest.manifestOutputFile)
                commandLine android.adbExe, 'shell', 'am', 'start', '-n', "${variant.packageName}/${activityClass}"
            }
        }
    }
}

对于库,您需要将applicationVariants更改为libraryVariants:请参见手册

更新:ADT 20、Gradle 2.1和com.android.tools.build:gradle:0.13.1

applicationVariants.all { variant ->
    if (variant.install) {
        tasks.create(name: "run${variant.name.capitalize()}", type: Exec, dependsOn: variant.install) {
            description "Installs the APK for ${variant.description}, and then runs the main launcher activity."
            def getMainActivity = { file ->
                new XmlSlurper().parse(file).application.activity.find{ it.'intent-filter'.find{ filter ->
                    return filter.action  .find{it.'@android:name'.text() == 'android.intent.action.MAIN'      } \
                        && filter.category.find{it.'@android:name'.text() == 'android.intent.category.LAUNCHER'}
                }}.'@android:name'
            }
            doFirst {
                def activityClass = getMainActivity(variant.outputs.processManifest.manifestOutputFile)
                commandLine android.adbExe, 'shell', 'am', 'start', '-n', "${variant.applicationId}/${activityClass}"

                // or without the XML hacking: commandLine android.adbExe, 'shell', 'monkey', '-p', variant.applicationId, '1'
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接