scalapackageandroid-manifestsbtsbt-android-plugin

Getting package name from AndroidManifest.xml using SBT and Android Plugin


I'm using SBT and modifying Jan Berkel's Android Plugin in and I need the information about the package name of the Android project I'm trying to build.

I know that the package name is located in AndroidManifest.xml, but I was wondering if there is any way using SBT to get that package name?


Solution

  • Not quite sure if that is what you need but anyways. Sbt task are written in scala code, so we can just find that file and then read the content:

    import java.io.File
    import java.io.FileNotFoundException
    def recursiveListFiles(f: File): Array[File] = {
      val these = f.listFiles
      if(these != null) 
        these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles) 
      else Array[File]()
    }
    
    val projectFiles = recursiveListFiles(new File("."))
    val manifestFile = projectFiles
      .filter(f => f.getCanonicalPath.endsWith("AndroidManifest.xml"))
      .toList.headOption
      .getOrElse(
        throw new FileNotFoundException("Your project doesn't contain manifest")
      )
    
    val manifestXML = scala.xml.XML.loadFile(manifestFile)
    val pkg = manifestXML.attribute("package").getOrElse("")