Android Example Read Key/Token from the B4A Manifest

Johan Schoeman

Expert
Licensed User
I was busy with a project and wanted to allow users to enter their own Key/Token via the B4A manifest file and then have access to the key/token via B4A code. I did a quick search and could not find something immediately and then did the below/attached via inline Java code that I found somewhere on the web.

The B4A Manifest File : note the meta-data tag that holds the key/token
B4X:
'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: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.

'Put your key here!!!!!!!!!!!  
AddApplicationText(<meta-data android:name="my_api_key" android:value="132bca45189df6g24mocc7b12341234abcde-2222-1313-5678-abcd" />
)
The B4A Sample code (only uses B4A core and JavaObject libs):

B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aManifestKey
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region


#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim nativeMe As JavaObject
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    nativeMe.InitializeContext
    Label1.Text = nativeMe.RunMethod("getKey", Array(nativeMe))
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
End Sub

#If Java

import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import java.lang.NullPointerException;
import android.app.Activity;
   
    public String getKey(Activity me) {
    String myApiKey = "";
        try {
            ApplicationInfo ai = getPackageManager().getApplicationInfo(me.getPackageName(), PackageManager.GET_META_DATA);
            Bundle bundle = ai.metaData;
            myApiKey = bundle.getString("my_api_key");
            BA.Log("KEY = " + myApiKey);
        } catch (NameNotFoundException e) {
            BA.Log("Failed to load meta-data, NameNotFound: " + e.getMessage());
        } catch (NullPointerException e) {
            BA.Log("Failed to load meta-data, NullPointer: " + e.getMessage());          
        }
        return myApiKey;

    }      
#End If
The label shows the key/token that was read from the B4A manifest file.

1.png
 

Attachments

Erel

Administrator
Staff member
Licensed User
Same implementation based on JavaObject:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(GetValueFromManifest("my_api_key"))
End Sub

Sub GetValueFromManifest(key As String) As String
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim ApplicationInfo As JavaObject = ctxt.RunMethodJO("getPackageManager", Null).RunMethod("getApplicationInfo", _
       Array(Application.PackageName, 0x00000080))
   Dim bundle As JavaObject = ApplicationInfo.GetField("metaData")
   Return bundle.RunMethod("getString", Array(key))
End Sub
 
Top