Android Tutorial Manifest Editor

Discussion in 'Tutorials & Examples' started by Erel, Jan 8, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Basic4android v1.8 introduces a new tool named Manifest Editor.
    Every Android application includes a file named AndroidManifest.xml.
    This is an XML file that describes the application for the OS. It includes elements such as the application components (activities, services and receivers), icons, permissions and other required information.
    You can see more information about this file here: The AndroidManifest.xml File | Android Developers

    Basic4android compiler generates this file automatically. In most cases there is no need to change anything. However in some cases, especially when using third party libraries (ads for example), the developer is required to add some elements to the manifest file.

    Until version 1.8 the solution was to mark the manifest file as read-only or check the "Do not write manifest file" option and prevent the compiler from recreating this file during compilation.
    This however means that from now on, the developer needs to maintain the manifest file and update it when the project is updated.

    The new manifest editor solves the above issue. It allows developers to add or modify elements in the manifest while also allowing the compiler to add the standard elements.

    Choosing Project - Manifest Editor will open the editor:

    [​IMG]

    As shown in the above screenshot the editor starts with several default elements.
    You can modify these elements or add other elements as needed.
    To make it easier to add multiline strings and strings that contain quote characters the manifest editor treats all characters between the open parenthesis and the closing parenthesis or comma (for commands with multiple parameters) as a single string.

    Escaping end of string characters
    If you need to write a string with a comma you should write two commas: ,,
    The same thing is true for strings with closing parenthesis: ))

    Editor commands
    There are several types of commands: commands that add an additional text inside an element, commands that set the value of an attribute (replacing the old value if it already exists) and two other commands which will be discussed later.

    List of commands

    Add text commands

    For example the AdMob library requires us to add an activity to the manifest file:
    [​IMG]

    The result is that this text is added under the 'application' element:
    [​IMG]

    Note that you can call 'add text' commands multiple times.

    AddActivityText / AddServiceText / AddReceiverText - expect two parameters: component name and the text to add.
    For example to use C2DM push framework you should add some text to the receiver. Note that a Service module in Basic4android is actually made of a native service and a native receiver. The name of the receiver is the same as the service module.
    Other "add text" commands: AddApplicationText and AddManifestText. Both expect a single parameter which is the text to add.

    Set attribute commands
    For example the following command can be used to set the orientation of a specific activity:
    Note that the attributes keys are case sensitive.

    SetActivityAttribute / SetReceiverAttribute / SetServiceAttribute - expect three parameters: component name, attribute key and attribute value.
    SetManifestAttribute / SetApplicationAttribute - expect two paramters: attribute key and attribute value.

    Other commands
    AddReplacement - this method allows you to declare a string that will be replaced with a second string. The compiler automatically adds the following declarations: $PACKAGE$ (replaced with the package name), $LABEL$ (replaced with the application label) and $ORIENTATION$ (replaced with the orientation value).
    The string replacement happens as the last step. You can use it to delete other strings by replacing them with an empty string.
    Syntax: AddReplacement (OldString, NewString)

    AddPermission - Adds a permission if it doesn't already exist. You can also add permissions using AddApplicationText. The advantage of AddPermission is that it makes sure to only add each permission once.

    Syntax: AddPermission (Permission)
    Example: AddPermission (android.permission.INTERNET)

    RemovePermission - Removes a permission. Same syntax as AddPermission.

    CreateResource - Creates a XML resource file: https://www.b4x.com/android/forum/threads/new-feature-three-birds-with-one-stone.63127/#post-398974

    Tips
    - Deleting the whole text will restore the default text (after you reopen the manifest editor).
    - As written at the beginning, in most cases you do not need to add anything to the manifest editor.
    - Open AndroidManifest.xml to better understand how it is built.
    - The "Do not modify manifest file" option is still available for backwards compatibility. It is recommended to use the new editor instead.
     
    Last edited: Feb 22, 2016
  2. stevel05

    stevel05 Expert Licensed User

    Looks really good Erel, great solution. I've got a few apps lined up to test it on in Beta.
     
  3. corwin42

    corwin42 Expert Licensed User

    Looks great.

    One question: I have once written a widget only app without any activity. Therefore I removed the whole declaration of the main activity from the manifest. Will this be possible with the AddReplacement command (I think it should be)?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    It is possible to remove all activities with the following code:
     
  5. moster67

    moster67 Well-Known Member Licensed User

    Comment XML-lines in Manifest-files

    One quick question, perhaps not so much related to the Manifest-editor since it seems you can insert comments in the same.

    What about the manifest-file itself? Is it possible to insert comments? If yes, what syntax should be used?

    Thanks!
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You can add any text that you like. For example to add the AdMob element with a comment:
    Code:
    'AdMob
    AddApplicationText(
    <!-- Required by AdMob -->
     <
    activity android:name="com.google.ads.AdActivity"
      android:configChanges=
    "keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    )
    'End of AdMob
     
  7. pluton

    pluton Active Member Licensed User

    For insert comment into manifest file

    Code:
    <!-- This is comment -->
    EDIT:
    Erel was faster with answer :)
     
  8. moster67

    moster67 Well-Known Member Licensed User

    Thank you both.

    Sent from my HTC HD2
     
  9. ondesic

    ondesic Active Member Licensed User

    I have two activities, "Main" and "Drip". Right now "Main" is the start up activity. How can I switch it to "Drip"?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    This was a bit challenging...
    You can switch the name of two activities:
    Code:
    SetActivityAttribute(Main, android:name, ".activity2")
    SetActivityAttribute(Activity2, android:name, 
    ".main")
     
    lemonisdead likes this.
  11. JesseW

    JesseW Active Member Licensed User

    Erel, I tried to use the '|' char in the rightmost arg of the SetActivityAttribute statement, and it complained until I removed it. this is what I tried

    Code:
    SetActivityAttribute(main, android:windowSoftInputMode, "stateVisible|adjustResize")
    which is perfectly allowable as it OR's the two values together.

    am I missing something? or do it wrong?

    thanks
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Which error did you get?
    I just tried it and it works correctly
     
  13. magarcan

    magarcan Active Member Licensed User

    Can anyone help me with this?
    Code:
    'This code will be applied to the manifest file during compilation.
    'You do not need to modify it in most cases.
    'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136
    AddManifestText(
    <uses-sdk android:minSdkVersion=
    "4" />
    <supports-screens android:largeScreens=
    "true" 
        android:normalScreens=
    "true" 
        android:smallScreens=
    "true" 
        android:anyDensity=
    "true"/>)
    'Airpush Code Start
    AddApplicationText(<!-- Airpush Code Start-->
    <
    activity android:name="com.airpush.android.PushAds"
    android:configChanges=
    "orientation|keyboardHidden"/>
    <receiver android:name=
    "com.airpush.android.UserDetailsReceiver"/>
    <receiver android:name=
    "com.airpush.android.MessageReceiver" />
    <receiver android:name=
    "com.airpush.android.DeliveryReceiver" />

    <!-- run on boot-->
    <
    service android:name="bootreceiver"></service>
    <receiver android:name=
    "bootreceiver$bootreceiver_BR">
    <
    intent-filter>
    <action android:name=
    "android.intent.action.BOOT_COMPLETED"/>
    </
    intent-filter>
    </receiver>
    <!--run on boot 
    end-->

    <
    service android:name="com.airpush.android.PushService">
    <
    intent-filter>
    <action android:name=
    "com.airpush.android.PushServiceStart<13184213****3944>"/>
    </
    intent-filter>
    </
    service>
    <!-- Airpush Code 
    End-->)
    SetApplicationAttribute(android:icon, 
    "@drawable/icon")
    SetApplicationAttribute(android:
    label"$LABEL$")
    'End of default text.
    AddPermission (android.permission.INTERNET)
    AddPermission (android.permission.SET_WALLPAPER)
    AddPermission (android.permission.ACCESS_NETWORK_STATE
    ")
    AddPermission (android.permission.RECEIVE_BOOT_COMPLETED")
    AddPermission (android.permission.READ_PHONE_STATE")
    AddPermission (android.permission.VIBRATE")
    AddPermission (com.android.launcher.permission.INSTALL_SHORTCUT")
    AddPermission (com.android.browser.permission.READ_HISTORY_BOOKMARKS")
    AddPermission (com.android.browser.permission.WRITE_HISTORY_BOOKMARKS")
    This is the error:
    Code:
    Compiling code.                         0.08
    Compiling layouts code.                 
    0.02
    Generating R 
    file.                      Error
    AndroidManifest.xml:
    19: error: Error parsing XML: not well-formed (invalid token)
     
    Last edited: Apr 27, 2012
  14. Erel

    Erel Administrator Staff Member Licensed User

    Not sure that this is the only problem, however permissions should be either with two quotes or without any quote. Your code only includes a quote at the end.
     
  15. Kevin

    Kevin Well-Known Member Licensed User

    I'm trying to prevent the ability to add an activity shortcut to my app in the launcher. From Stackoverflow, I found the following. It says to remove the category LAUNCHER section from your manifest. This would be for the Main activity. The thing is, I can't figure out how to remove this. I tried creating an empty value for the action, but B4A automatically puts another copy of the intent section in the manifest.

    I suppose if need be I can manually edit and lock the manifest, but I hate to do that. Any ideas on how to achieve this?

    Code:
    AddActivityText(Main,
    <
    intent-filter>
      <action android:name=
    "" />
      <category android:name=
    "android.intent.category.LAUNCHER" />
    </
    intent-filter>)
     
    Last edited: May 2, 2012
  16. NJDude

    NJDude Expert Licensed User

    Try this using the manifest editor:
    Code:
    AddReplacement(<category android:name="android.intent.category.LAUNCHER" />, <!--<category android:name="android.intent.category.LAUNCHER" /> -->)
    NOTE: You will have to uninstall and then reinstall the app in order to see the changes.
     
    Last edited: May 2, 2012
  17. Kevin

    Kevin Well-Known Member Licensed User

    Thanks! That did the trick. I figured it could be done, just couldn't figure out how. :sign0188:
     
  18. mcmanu

    mcmanu Active Member Licensed User

    Hi Erel

    How is the command to not allow the users to install my application to sd card?
    And does anybody know if i just use the manifest command and do not use google play copy protection, is the user still not permitted to install my app to sd card?

    Sorry because of my english ;)
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    You should uncheck: Project - Can install to external storage.

    However users with rooted devices will be able to install it wherever they want.
     
  20. mcmanu

    mcmanu Active Member Licensed User

    Thx erel

    Thank you :)

    Oh thats not good :-(, because i dont want that users have the ability to Recompile my application, Obfusticate doesn´t work because i use a code module which transforms "a" into "A" and " " into "" :-(
    Its because of the " " the compiler trows an exception
     
Loading...