Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - How to get values from AndroidManifest?

Inside build.gradle for Android project

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"
    }
}

How to get value for main Activity from AndroidManifest for the default Activity? (Also if there are several activities, making logic to select one would make it long, while processing is to be inside Android tools)

Is there better way from android plugin then parsing AndroidManifest.xml ?

UPDATE: https://github.com/novoda/gradle-android-command-plugin can possible suit some needs, but I needed no-argument version to make quick run in Eclipse via http://marketplace.eclipse.org/content/gradle

like image 530
Paul Verest Avatar asked Apr 10 '14 05:04

Paul Verest


1 Answers

You can use XmlSlurper class.

example:

AndroidManifest.xml

<manifest package="com.example.your.app">

and retrieve it in gradle

def manifest = new XmlSlurper().parse(file("AndroidManifest.xml"))

// returns "com.exmaple.your.app"
[email protected]()
like image 155
hidekuro Avatar answered Nov 01 '22 04:11

hidekuro