Android N-尽管minSDK设置为14,但无法在较低的API上运行 [英] Android N - Cannot run on lower API though minSDK set to 14

查看:167
本文介绍了Android N-尽管minSDK设置为14,但无法在较低的API上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将compileSdkVersion更新为N后,我试图在API 22设备上运行APK,但无法执行此操作.

I am trying to run the APK on API 22 device after updating compileSdkVersion to N but unable to do so.

compileSdkVersion 'android-N'
buildToolsVersion "24.0.0 rc1"

defaultConfig {
       minSdkVersion 14
       targetSdkVersion 'N'
}

推荐答案

开箱即用,构建工具被设置为阻止您在较旧的设备上运行N Developer Preview应用程序.据推测,这是Google试图阻止人们运送预览版以外的内容的草率方法.在过去的两个开发人员预览中也使用了这种方法.因此,Google不想让您在Android 5.0(API级别22)设备上测试N Developer Preview应用,以查看您是否正确处理了向后兼容性.

Out of the box, the build tools are set up to block you from running N Developer Preview apps on older devices. Presumably, this is a sloppy way for Google to try to prevent people from shipping stuff built off of the preview. This approach was also used in the past two developer previews. So, Google does not want you testing your N Developer Preview app on your Android 5.0 (API Level 22) device to see if you are handling backwards compatibility correctly.

¯\ _(ツ)_/¯

幸运的是,您可以破解一个解决方案:

Fortunately, you can hack in a solution:

android {
    compileSdkVersion 'android-N'
    buildToolsVersion "24.0.0 rc1"

    defaultConfig {
      minSdkVersion 15
      targetSdkVersion 'N'
    }

    // based on http://stackoverflow.com/a/27372806/115145

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.processManifest.doLast {
                def manifestOutFile = output.processManifest.manifestOutputFile
                def xml = new XmlParser().parse(manifestOutFile)
                def usesSdk = xml.'uses-sdk'

                usesSdk.replaceNode{
                    'uses-sdk'('android:minSdkVersion': '15',
                               'android:targetSdkVersion': '15')
                }

                def fw=new FileWriter(manifestOutFile.toString())

                new XmlNodePrinter(new PrintWriter(fw)).print(xml)
            }
        }
    }
}

工具需求是targetSdkVersion 'N',这就是破坏您在较旧设备上运行该应用程序的能力的原因.因此,这种大量的Groovy代码允许构建工具以targetSdkVersion 'N'开头,然后在打包之前将另一个targetSdkVersion值交换到生成的清单中.在这种情况下,我将minSdkVersiontargetSdkVersion设置为15,但是您可以用自己的值代替.另外,您将需要重建或清理项目以使更改生效.

What the tools demand is targetSdkVersion 'N', and that's what breaks your ability to run the app on older devices. So, this hunk of Groovy code allows the build tools to start with targetSdkVersion 'N', then swaps in another targetSdkVersion value into the generated manifest, before packaging. In this case, I am setting the minSdkVersion and targetSdkVersion to 15, but you can substitute in your own values. Also, you will need to rebuild or clean your project for the change to take effect.

从好的方面来说,可以在较旧的Android设备上安装并运行所得的应用程序(尽管可能只能通过命令行构建;我认为Android Studio不喜欢这种技术).

On the plus side, the resulting app can be installed and run on an older Android device (though possibly only through a command-line build; I think Android Studio didn't like this technique).

在不利的一面,您的targetSdkVersion实际上不会是N,这可能会影响N Developer Preview设备上的某些行为.您可能想要设置用于向后兼容性测试的特定构建类型,而只对这种构建类型执行"hack-themanifest"技巧.然后,您可以使用官方的N Developer Preview设置进行测试,并在一个版本中执行某种向后兼容性测试.

On the minus side, your targetSdkVersion will not actually be N, which may impact some behaviors on N Developer Preview devices. You might want to set up a particular build type that you use for the backwards-compatibility testing and only do my hack-the-manifest trick on that build type. Then, you can test both using an official N Developer Preview setup and perform some measure of backwards compatibility testing in one build.

这篇关于Android N-尽管minSDK设置为14,但无法在较低的API上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆