android-MNC project won’t run on devices prior to API level 'android-MNC'

五迷三道 提交于 2019-12-06 07:48:48

Quoting myself:

In the case of the M Developer Preview, the compileSdkVersion value of android-MNC causes the build process to put MNC in as the minSdkVersion and targetSdkVersion in the generated manifest.

Fortunately, manifests are text files.

So, here is an android closure that can build an app module that compiles against MNC but will run on API Level 15+ devices:

android {
    compileSdkVersion 'android-MNC'
    buildToolsVersion "23.0.0 rc1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 15
    }

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

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.processManifest.doLast {
                def manifestOutFile = output.processManifest.manifestOutputFile
                def newFileContents = manifestOutFile.getText('UTF-8').replace("MNC", "15")
                manifestOutFile.write(newFileContents, 'UTF-8')
            }
        }
    }
}

This takes a very “caveman” approach to the problem, reading in the generated manifest, replacing all occurrences of MNC with 15, and writing the adjusted manifest back out. This will fail on projects that have MNC somewhere else, like an activity’s class name. It also sets both minSdkVersion and targetSdkVersion to the same value. A more sophisticated script would replace those individual attributes — the proof of this is left as an exercise for the reader. Similarly, a more powerful script would read the desired values out of defaultConfig and apply them. And, a safety-conscious edition of this would only apply this for debuggable variants, thereby helping to reduce the impact of a drooling idiot trying to ship a release build that performs this override. This is merely a proof of concept, not implementing all possible bells and whistles.

Again, doing this and releasing the results to the Play Store or elsewhere is monumentally idiotic. Use this for testing only.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!