Android Meta-Data: large int is not treated as String

Android has a solution to define meta-data

in the manifest. I think this is a very good solution for people building SDKs. Why? ... If you need to define a specific set of keys in a String file, it would be best to just set them in the manifest. They are more closely related to the application and more friendly for the developer implementing your SDK in their project, as they only need to add a few lines to the manifest instead of editing two or more files just for api key/sender_id

.

So to the point

I want the user to post a large amount in their manifest so that I can download and extract from the SDK.

<meta-data
        android:name="com.example.projectId"
        android:value="385167817594" />

      

This is my code for extracting metadata. Where META_PROJECT_ID = com.example.projectId

String pkgName = mContext.getPackageName();
Bundle bundle = mContext.getPackageManager().getApplicationInfo(pkgName, PackageManager.GET_META_DATA).metaData;
String projectId = bundle.getString(META_PROJECT_ID); 

      

This code works with all my other values โ€‹โ€‹defined in the manifest. But the funniest part is when extracting the values โ€‹โ€‹from the manifest, it looks like some parsing is going on in between. Even if I do getString(..)

, it will return int

instead String

, since the projectId only contains numbers and not alphanumric

. If I add one letter to the projectId value defined in the manifest, it returns a string. Example: 385167817594 a

I think this is not a problem for most of us. They will do getInt and then convert it to String when needed. But this int is too big. Bundle doesn't even show the correct value in the debugger. Its showing a large negative number ( Integer.MIN_VALUE

), which probably indicates that the parsing went wrong?

How did others do it?

Facebook

faced the same problem i am thinking. They did the following:

Manifesto (for example)

<meta-data
        android:name="com.example.projectId"
        android:value="@string/project_id" />

      

strings.xml

<string name="project_id">385167817594</string>

      

Does anyone know why Facebook

I decided to solve it like this? I mean, now you need to edit two files and a link from manifest to line manifest.

Is there really no solution or what has Facebook done best practice

?

For now I will do it like Facebook did, but they are really curious if there is any solution out there and if what Facebook is doing is best practice.

+3


source to share


2 answers


Change

<meta-data android:name="com.example.projectId" android:value="385167817594" />

      

to be



<meta-data android:name="com.example.projectId" android:value="38516781759\u0034" />

      

replace number 4 with its Unicode equivalent \ u0034

0


source


I checked this with Facebook SDK, they don't use it as int, it's String. check this below code from facebook sdk for android.

public static String getMetadataApplicationId(Context context) {
    Validate.notNull(context, "context");

    try {
        ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
                context.getPackageName(), PackageManager.GET_META_DATA);
        if (ai.metaData != null) {
            return ai.metaData.getString(Session.APPLICATION_ID_PROPERTY);
        }
    } catch (PackageManager.NameNotFoundException e) {
        // if we can't find it in the manifest, just return null
    }

    return null;
}

      



if you want to use it use it as String. then try converting it to double or long type. I hope this helps you.

-1


source







All Articles