build.gradle warning 'avoid using + in version numbers'

馋奶兔 提交于 2019-12-22 07:19:10

问题


This is my build.gradle app-level file.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "it.myco.app.myproj"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

Android Studio highlights the line

compile 'com.android.support:appcompat-v7:26.+'

with the message

avoid using + in version numbers can lead to unpredictable and unrepeatable builds

This line is auto-generated by Android Studio. Why this warning message? What I need to write to solve the warning ?


回答1:


Someday, someone else may do a git pull on your code and then try build an apk. This apk may have different behavior or even get a build error or run time error, because the dependencies version can be different than the dependencies used when you created this code.

You should use an explicit dependency version. I suggest use the newest version. You can search on bintray jcenter to see the newest version. If you are using Android Studio, File -> Project Structure -> Modules -> (your module, app usually) -> Dependencies -> Add library dependency (click the + sign) will give you the newest version, even add library automatically.




回答2:


Replace it with the latest version of the library. Usually AS shows the latest version when you alt + enter on the warning.

More info on the reasons




回答3:


You can use something like

compile 'com.android.support:appcompat-v7:26.1.0'



回答4:


Don't use + in dependency version. it means Android studio will use the latest library, which may cause problem in future. In android studio it's recommended to use all android library with same version, suppose you are using + and if any single library got updated then android studio will use that updated library, so the version for this library will be change. that cause problem to build application.

That's why gradle giving this warning. it's best practice to write full version number instead of +.




回答5:


Why this warning message?

Here you can find a good blog about this topic.

Dynamic versions add nondeterminism to your build:

  • Dependencies can unexpectedly introduce behavior changes to your app.

  • The same source built on two different machines can differ.

  • Similarly, builds built on the same machine but at different times can differ.

  • Past builds cannot be reproduced perfectly. This makes it difficult to revert safely.

  • There are security implications if a bad actor introduces a malicious version of a dependency.

What I need to write to solve the warning ?

Always specify your dependency versions explicitly.




回答6:


It's a normal warning.

It's better you to choose a concrete version than going that way. This is because if you use "+" it will choose the newest, so in the new version it's possible you to get some deprecated or unexpected changes that will do your app to die. Think that if you are using it in a huge project you will have a lot of dependencies so it will be a chaotic environment.

Use specific versions and if there is a new one... update it manually.

To solve the warning and choose the last version, if you are using Windows, click Alt+Enter and it will choose the latest version.

If it doesn't work you will have to search for it on the Internet or in Project Structure > Modules >(your module) > Dependencies > +



来源:https://stackoverflow.com/questions/46793888/build-gradle-warning-avoid-using-in-version-numbers

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