在Android Studio3.0开始的时候就将 compile 改为 implementation 关键字了,当时,项目中大部分的compile改为了implementation,但是还有一部分没有改过来,但是仍然可以使用,但是现在呢?尼玛直接给编译就报错!这不是强制性的让使用 implementation 么?报的错中提示2018年底才移除,这还没到年底呢,就不能用了?
还是乖乖的改吧,由于是我们的依赖的library中使用了compile,相当于是library依赖了一个第三方库,直接改为implementation就相当于屏蔽掉了app对library依赖的第三方库api的访问权限,而我们的app刚好用到了library依赖的第三方库的api,所以直接改为implementation是不行的,要想让app能访问到就将其改为 api就可以了,其实这不是一个好的解决办法,最好的解决办法是,library中封装一层app访问library依赖的第三方库api的方法,这样的话app是和library依赖的第三方库是没有任何关系的,这样就降低了app与library依赖的第三方库的耦合性,这样也体现了“第三方库隔离”的原则,如果以后依赖的库变了,你只需要修改library中依赖的库及app调用的方法即可,不用动app里面的代码了~
以下提供我项目中的代码作为例子
//升级前的配置文件
apply plugin: 'com.android.application'
def getGitVersion() {
try {
return 'git rev-parse --short HEAD'.execute().text.trim()
} catch (Throwable th) {
return "";
}
}
android {
def globalConfiguration = rootProject.ext.android
compileSdkVersion globalConfiguration.AndroidCompileSdkVersion
buildToolsVersion globalConfiguration.AndroidBuildToolsVersion
useLibrary 'org.apache.http.legacy'
signingConfigs {
debug {
storeFile file('../ticserver.jks')
storePassword "xxx"
keyAlias "key0"
keyPassword "xxx"
}
release {
storeFile file('../ticserver.jks')
storePassword "xxx"
keyAlias "key0"
keyPassword "xxx"
}
}
defaultConfig {
applicationId globalConfiguration.applicationId
minSdkVersion globalConfiguration.AndridMinSdkVersion
targetSdkVersion globalConfiguration.AndroidTargetSdkVersion
versionCode globalConfiguration.versionCode
versionName globalConfiguration.versionName
testInstrumentationRunner globalConfiguration.testInstrumentationRunner
multiDexEnabled true
ndk {
abiFilters "armeabi","armeabi-v7a","x86_64"
}
buildConfigField "String", "GIT_REVISION", "\"${getGitVersion()}\""
buildConfigField "String", "BUILD_DATE", "\"${new Date().toLocaleString()}\"";
manifestPlaceholders = [
GETUI_APP_ID : "xxx",
GETUI_APP_KEY : "xxx",
GETUI_APP_SECRET : "xxx"
]
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/main/aidl']
jniLibs.srcDirs = ['libs']
}
}
}
apply plugin: 'android-apt'
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
def googleDependencies = rootProject.ext.goodleDependencies
def otherDependencies = rootProject.ext.otherDependdencies
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile otherDependencies.junit
compile otherDependencies.fastjson
}
升级后的配置文件
apply plugin: 'com.android.application'
def getGitVersion() {
try {
return 'git rev-parse --short HEAD'.execute().text.trim()
} catch (Throwable th) {
return "";
}
}
android {
def globalConfiguration = rootProject.ext.android
compileSdkVersion globalConfiguration.AndroidCompileSdkVersion
buildToolsVersion globalConfiguration.AndroidBuildToolsVersion
useLibrary 'org.apache.http.legacy'
signingConfigs {
debug {
storeFile file('../ticserver.jks')
storePassword "xxx"
keyAlias "key0"
keyPassword "xxx"
}
release {
storeFile file('../ticserver.jks')
storePassword "1245104255"
keyAlias "key0"
keyPassword "1245104255"
}
}
defaultConfig {
applicationId globalConfiguration.applicationId
minSdkVersion globalConfiguration.AndridMinSdkVersion
targetSdkVersion globalConfiguration.AndroidTargetSdkVersion
versionCode globalConfiguration.versionCode
versionName globalConfiguration.versionName
testInstrumentationRunner globalConfiguration.testInstrumentationRunner
multiDexEnabled true
ndk {
abiFilters "armeabi","armeabi-v7a","x86_64"
}
buildConfigField "String", "GIT_REVISION", "\"${getGitVersion()}\""
buildConfigField "String", "BUILD_DATE", "\"${new Date().toLocaleString()}\"";
manifestPlaceholders = [
GETUI_APP_ID : "xxx",
GETUI_APP_KEY : "xxx",
GETUI_APP_SECRET : "xxx"
]
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/main/aidl']
jniLibs.srcDirs = ['libs']
}
}
}
//apply plugin: 'android-apt'
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
def googleDependencies = rootProject.ext.goodleDependencies
def otherDependencies = rootProject.ext.otherDependdencies
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation googleDependencies.support_v4
implementation googleDependencies.appcompat_v7
testImplementation otherDependencies.junit
}
升级3.1后,对很多东西进行了重新定义
3.1中所有模块的compile 对应字段都要替换成 implementation,如果不替换掉会在gradle的时候报错,运行不上项目。(强制使用implementation, 不过报错也不够精准。需要你仔细观察gradle的输出) 如果你的项目使用多个依赖,而且依赖之间又有相互依赖,你想在主项目里只依赖一个库,那就需要在其他依赖库里使用api引用库,这样所引用的库就可在当前module外使用了