简体   繁体   English

Gradle和Android:从Android Library Project获取软件包ID

[英]Gradle and Android: obtain package ID from Android Library Project

I have an Android project created with Android Studio that uses the Gradle build system. 我有一个使用Gradle构建系统的Android Studio创建的Android项目。

I have several modules (subprojects) in this Android project, and some of them are Android Library projects (AAR). 我在这个Android项目中有几个模块(子项目),其中一些是Android库项目(AAR)。

What I need to do is obtain the Package ID (basically the value that uniquely identifies an Android app or library, the one thay is found in the AndroidManifest.xml file inside the "package" attribute) from these AAR projects from Gradle. 我需要做的是从Gradle的这些AAR项目中获得Package ID(基本上是唯一标识Android应用程序或库的值,在“ package”属性内的AndroidManifest.xml文件中找到一个)。

Now, if the project was an android app it would be trivial: the "android.defaultConfig.applicationId" property already contains the package ID. 现在,如果项目是一个android 应用程序,那将是微不足道的:“ android.defaultConfig.applicationId”属性已经包含包ID。

But for Android library projects this value is not available. 但是对于Android 项目,此值不可用。 Now, I know I can technically just use the (very flexible) groovy XML parser to read the value directly from the AndroidManifest.xml file, but I was wondering if there was a more direct and efficient way to do it, like there is for Android apps. 现在,我知道从技术上讲,我可以使用(非常灵活的)groovy XML解析器直接从AndroidManifest.xml文件中读取值,但是我想知道是否有更直接和有效的方法来完成此操作,例如Android应用。

Well, since I didn't find a more direct solution to this, here's how I did it with pure Gradle code in the end: 好吧,由于我没有找到更直接的解决方案,所以这是我最后使用纯Gradle代码实现的方法:

File pluginManifestFile = file(project.absolutePath + "/src/main/AndroidManifest.xml")
    if(pluginManifestFile.exists()){
        String manifestContents = pluginManifestFile.text
        def pluginManifestXml = new XmlSlurper().parseText(manifestContents)
        String packageName = pluginManifestXml.@package.toString()
        println 'Package name of the library is: ' + packageName
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM