OlivierM
OlivierM

Reputation: 771

How to know which Android API an APK was built against

When an APK is generated with a given API Level, is there a way to know which API level was used for compiling just having the APK file?

The minSdkVersion does not necessarily match the API Level used for compiling the project, it is just a note for the Android installer to block the app if minSdkVersion > current version

Upvotes: 7

Views: 6515

Answers (5)

GuilhE
GuilhE

Reputation: 11861

BuildConfig.VERSION_CODE;

and

getPackageManager().getPackageInfo(...).

will give us:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
          android:versionCode="1"
          android:versionName="...">

the versionCode, 1 in this example.

The API Level used to compile the .apk I could not find a way of consult it in runtime, but if you use gradle you can check it:

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
...
}

if you are using .iml, configured with the IntelliJ project configurations GUI:

<component name="NewModuleRootManager" inherit-compiler-output="true">
    ...
    <orderEntry type="inheritedJdk" />
    ...
</component>

project.properties:

# This file is automatically generated by IntelliJ IDEA
# Project target.
target=Google Inc.:Google APIs:22

but since build.gradle, project.iml and project.properties aren't shipped with .apk I can't think of a way to get the compileSdkVersion in runtime or by unzipping the .apk

Upvotes: 2

Jorgesys
Jorgesys

Reputation: 126455

This is another way to get the targetSdkVersion and the versionCode of my application:

try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            int versionCode = packageInfo.versionCode;
            int targetSdkVersion = packageInfo.applicationInfo.targetSdkVersion;

        }
        catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, e.getMessage());
        }

using Android Studio, the values are defined into my build.gradle file

   defaultConfig {
        applicationId "com.tuna.hello.androidstudioapplication"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 12
        versionName "1.0"
    }

Upvotes: 2

FeleMed
FeleMed

Reputation: 621

Everything that you could get should be on ApplicationInfo that you get from PackageManager, so you should get ApplicationInfo and then see what you can get out of it, i.e.:

getPackageManager().getPackageInfo(...).targetSdkVersion

Reference: http://developer.android.com/reference/android/content/pm/ApplicationInfo.html

Check in the "Fields"section of the reference. Have a look at "sourceDir" field.

I hope this is of any help for you, and please vote up/down whatever helped/missdirected you.

Best regards.

Upvotes: 1

Jorgesys
Jorgesys

Reputation: 126455

How to know which Android API your application is running:

int myAPI = Build.VERSION.SDK_INT;

How to know the APK´s versionCode was built:

String versionCode = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionName;

Note: ctx is the application Context.

But to be more specific, change the extension of your APK file to .zip, and extract the Manifest.xml then you can see the versionCode, minSdkVersion, targetSdkVersion etc...

Upvotes: 0

Droid Chris
Droid Chris

Reputation: 3783

Try this:

android.os.Build.VERSION.SDK_INT

Upvotes: 0

Related Questions