2014-04-10 13 views
7

Innen build.gradle für Android-ProjektGradle - Wie bekomme ich Werte von AndroidManifest?

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

Wie Wert für Hauptaktivität von AndroidManifest für die Standard-Aktivität zu bekommen? (auch wenn es mehr Aktivitäten, Logik machen wählen Sie eine lange würde es machen, , während die Verarbeitung innerhalb Android-Tool sein)

Gibt es eine bessere Art und Weise von android-Plugin dann AndroidManifest.xml Parsen?

UPDATE: https://github.com/novoda/gradle-android-command-plugin Dose möglich Anzug einige Bedürfnisse, aber ich brauchte schnellen Lauf in Eclipse über http://marketplace.eclipse.org/content/gradle

+0

hast du http://stackoverflow.com/questions/10187556/reading-android-manifest-file –

+0

das zu lesen ist ' AndroidManifest.xml "aus der App während der Laufzeit. Die Frage bezieht sich auf Build System Gradle, d. H. Kompilierzeit, keine Android-Klassen. –

+0

Gefunden http://stackoverflow.com/questions/11558157/reading-info-from-existing-pom-xml-file-using-gradle und XmlSluper http://groovy.codehaus.org/Reading+XML+mit+Groovy % 27s + XmlSlurper. Nicht so saubere Lösung .... –

Antwort

6

Ich schrieb dies für ADT 20 (L), Gradle 1.12 und com.android.tools.build:gradle:0.12.2.

Es funktioniert mit Aromen, Build-Typen (nicht vergessen myBuildType.initWith(existingBuildType)) und applicationIdSuffix.

gebe folgende am Ende 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{[email protected]() == 'android.intent.action.MAIN'} \ 
         && filter.category.find{[email protected]() == 'android.intent.category.LAUNCHER'} 
       }}[email protected] 
      } 
      doFirst { 
       def activityClass = getMainActivity(variant.processManifest.manifestOutputFile) 
       commandLine android.adbExe, 'shell', 'am', 'start', '-n', "${variant.packageName}/${activityClass}" 
      } 
     } 
    } 
} 

Für Bibliotheken müssen Sie applicationVariants-libraryVariants ändern: siehe manual.

aktualisieren: ADT 20, Gradle 2.1 und 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' 
      } 
     } 
    } 
} 
+0

Große Show-Cast von Gradle Power –

10

Sie kein Argument Version verwenden können XmlSlurper Klasse zu machen.

Beispiel:

AndroidManifest.xml

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

und Abrufen es in gradle

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

// returns "com.exmaple.your.app" 
[email protected]() 
+0

Danke, das war sehr hilfreich. –

+2

Sie können 'android.sourceSets.main.manifest.srcFile' anstelle von' "AndroidManifest.xml" 'verwenden, um Manifest-Datei automatisch zu finden –

Verwandte Themen