2014-04-10 11 views
7

एंड्रॉयड परियोजनाGradle - 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" 
    } 
} 

के लिए build.gradle अंदर कैसे डिफ़ॉल्ट गतिविधि के लिए AndroidManifest से मुख्य गतिविधि के लिए मूल्य प्राप्त करने के लिए? (इसके अलावा, अगर वहाँ कई गतिविधियों रहे हैं, तर्क, का चयन करने के लिए एक यह लंबे समय होगा जबकि प्रसंस्करण एंड्रॉयड उपकरणों के अंदर हो रहा है)

वहाँ से एंड्रॉयड प्लगइन तो AndroidManifest.xml को पार्स बेहतर तरीका है?

अद्यतन: https://github.com/novoda/gradle-android-command-plugin कर सकते हैं संभव सूट कुछ की जरूरत है, लेकिन मैं के माध्यम से http://marketplace.eclipse.org/content/gradle

+0

आप http://stackoverflow.com/questions/10187556/reading-android-manifest-file –

+0

'पढ़ने के लिए है कि देखा था रनटाइम के दौरान ऐप से AndroidManifest.xml'। सवाल बिल्ड सिस्टम ग्रेडल के बारे में है, यानी संकलन समय, कोई एंड्रॉइड क्लास नहीं है। –

+0

मिला http://stackoverflow.com/questions/11558157/reading-info-from-existing-pom-xml-file-using-gradle और XmlSluper http://groovy.codehaus.org/Reading+XML+using+Groovy % 27s + XmlSlurper। इतना साफ समाधान नहीं .... –

उत्तर

6

में मैं सिर्फ एडीटी 20 (एल), Gradle 1.12 और com.android.tools.build:gradle:0.12.2 के लिए यह लिखा।

यह स्वाद के साथ काम करता है, प्रकार बनाएं (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{[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}" 
      } 
     } 
    } 
} 

पुस्तकालयों के लिए आप applicationVariantslibraryVariants को बदलने की जरूरत है: manual देखते हैं।

अद्यतन: एडीटी 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' 
      } 
     } 
    } 
} 
+0

ग्रैडल पावर का शानदार शो-कास्ट –

10

ग्रहण में त्वरित रन बनाने के लिए आप XmlSlurper वर्ग का उपयोग कर सकते कोई तर्क संस्करण की जरूरत है।

उदाहरण:

AndroidManifest.xml

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

और इसे पुनः प्राप्त Gradle

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

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

के लिए अच्छा एपीआई देंगे धन्यवाद, यह बहुत उपयोगी था। –

+2

इसके अलावा आप स्वचालित रूप से मैनिफेस्ट फ़ाइल का पता लगाने के लिए 'AndroidManifest.xml" '' AndroidManifest.xml "के बजाय 'android.sourceSets.main.manifest.srcFile' का उपयोग कर सकते हैं –

संबंधित मुद्दे