manifest.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "encoding/xml"
  7. "errors"
  8. "fmt"
  9. "html/template"
  10. )
  11. type manifestXML struct {
  12. Activity activityXML `xml:"application>activity"`
  13. }
  14. type activityXML struct {
  15. Name string `xml:"name,attr"`
  16. MetaData []metaDataXML `xml:"meta-data"`
  17. }
  18. type metaDataXML struct {
  19. Name string `xml:"name,attr"`
  20. Value string `xml:"value,attr"`
  21. }
  22. // manifestLibName parses the AndroidManifest.xml and finds the library
  23. // name of the NativeActivity.
  24. func manifestLibName(data []byte) (string, error) {
  25. manifest := new(manifestXML)
  26. if err := xml.Unmarshal(data, manifest); err != nil {
  27. return "", err
  28. }
  29. if manifest.Activity.Name != "org.golang.app.GoNativeActivity" {
  30. return "", fmt.Errorf("can only build an .apk for GoNativeActivity, not %q", manifest.Activity.Name)
  31. }
  32. libName := ""
  33. for _, md := range manifest.Activity.MetaData {
  34. if md.Name == "android.app.lib_name" {
  35. libName = md.Value
  36. break
  37. }
  38. }
  39. if libName == "" {
  40. return "", errors.New("AndroidManifest.xml missing meta-data android.app.lib_name")
  41. }
  42. return libName, nil
  43. }
  44. type manifestTmplData struct {
  45. JavaPkgPath string
  46. Name string
  47. LibName string
  48. }
  49. var manifestTmpl = template.Must(template.New("manifest").Parse(`
  50. <manifest
  51. xmlns:android="http://schemas.android.com/apk/res/android"
  52. package="{{.JavaPkgPath}}"
  53. android:versionCode="1"
  54. android:versionName="1.0">
  55. <application android:label="{{.Name}}" android:debuggable="true">
  56. <activity android:name="org.golang.app.GoNativeActivity"
  57. android:label="{{.Name}}"
  58. android:configChanges="orientation|keyboardHidden">
  59. <meta-data android:name="android.app.lib_name" android:value="{{.LibName}}" />
  60. <intent-filter>
  61. <action android:name="android.intent.action.MAIN" />
  62. <category android:name="android.intent.category.LAUNCHER" />
  63. </intent-filter>
  64. </activity>
  65. </application>
  66. </manifest>`))