Android 仅在Kitkat上运行我的应用程序

Android 仅在Kitkat上运行我的应用程序,android,Android,我希望我的应用程序只在Kitkat版本上运行,而不在任何其他android版本上运行(既不旧也不新),我在app level build.gradle文件中做了以下更改: android { compileSdkVersion 19 defaultConfig { applicationId "com.app.tempapp" minSdkVersion 19 targetSdkVersion 19 versionC

我希望我的应用程序只在Kitkat版本上运行,而不在任何其他android版本上运行(既不旧也不新),我在app level build.gradle文件中做了以下更改:

android {
    compileSdkVersion 19
    defaultConfig {
        applicationId "com.app.tempapp"
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
但这不起作用。

解决方案1(我喜欢这个)

尝试像这样在应用程序启动时阻止

if (android.os.Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.KITKAT){
    Log.i("No support", "This version is not supported.")
    finish();
}
defaultConfig {
    applicationId "com.app.tempapp"
    minSdkVersion 19
    targetSdkVersion 19
    maxSdkVersion 19
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
解决方案2

minSdkVersion
maxSdkVersion
保持为19,如下所示

if (android.os.Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.KITKAT){
    Log.i("No support", "This version is not supported.")
    finish();
}
defaultConfig {
    applicationId "com.app.tempapp"
    minSdkVersion 19
    targetSdkVersion 19
    maxSdkVersion 19
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

您需要在两个级别处理它:

谷歌游戏商店级 你可以利用。它是一个整数,指定应用程序运行的最大API级别。如果应用程序的
maxSdkVersion
属性低于系统本身使用的API级别,则系统将不允许安装该应用程序

将此属性的值设置为19

defaultConfig {
    minSdkVersion 19
    maxSdkVersion 19
    targetSdkVersion 19
    ...
}
注意:

此方法仅在用户通过play store安装应用程序时有效

Android的未来版本(超过Android 2.0.1)将不再检查 或在安装过程中强制执行maxSdkVersion属性,或 重新验证。Google Play将继续使用该属性作为 但是,在向用户展示可用于 下载

应用程序级别 剩下的唯一选择是在代码中强制执行它。但缺点是,用户可以安装应用程序,但它只会自动停止。您可以按如下方式优雅地处理它:

if (android.os.Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.KITKAT){
    final AlertDialog compatibility = new AlertDialog.Builder(this).setMessage("This app only be executed on Kitkat version")
            .setPositiveButton("OK", null).create();
    compatibility.setCancelable(false);
    compatibility.setCanceledOnTouchOutside(false);
    compatibility.show();
    compatibility.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           compatibility.dismiss();
           finish();
        }
    });

}

将此代码添加到
onResume()
中。您可能需要添加一些额外的检查,以防用户将应用程序最小化并再次打开,同时在
onDestroy()
中关闭对话框,以避免因内存泄漏而崩溃另一种仅在kitkat中运行的方法是在您的第一个活动中的代码下方编写的

 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
           //finish activity
 }

您想分享任何日志或问题吗?只有当您使用Google Play作为分发平台时,此功能才有效。没有设备上的检查:“Android的未来版本(超过Android 2.0.1)在安装或重新验证期间将不再检查或强制执行maxSdkVersion属性。”@Christopher感谢您的帮助highlighting@Sagar谢谢你的回答,但是你提到了它的缺点,现在,如果用户从play store下载它并将apk发送给具有不同API级别的用户,他/她可以安装它,我想限制@不幸的是,悉达多已经不可能了。你能做的就是限制应用的使用
compileSdkVersion
buildToolsVersion
呢?