java - Gradle Android依赖解析,本地模块和maven lib的区别
问题描述
我做了一个测试项目来验证这个问题。
- 打开 Android Studio
- 我创建了一个新的基本项目。它的主要模块被称为
app
. - 我添加了一个库模块,名为
libA
. - 在
libA
中,我添加了对 Gson 的依赖
implementation 'com.google.code.gson:gson:2.8.5'
项,并ClassA
使用Gson
. - 在
app
中,我添加了一个依赖项libA
,
implementation project(path: ':liba')
在 MainActivity 中,我使用了来自 的类libA
,没有问题。Gson
正如预期的那样,从主要活动中无法访问。 maven-publish
我将一个使用插件的lib发布到maven local
./gradlew :liba:publishMsdkPublicationToMavenLocal
- 我已将导入从上面的一个切换到
implementation "com.ndefiorenze:lib-a:1.0.0"
现在我可以继续使用 libA 中定义的类而不会出现问题,但我也可以访问 Gson并且在主活动中我可以做
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
Log.d("classA", new Gson().toJson(list));
如何防止 Gson 在 Maven 上app
暴露liba
?
这里有一些来源:
public class ClassA {
public void fooA(){
List<String> list = new ArrayList<>();
list.add("a");
Log.d("classA", new Gson().toJson(list));
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ClassA().fooA()
}
}
liba build.gradle
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.code.gson:gson:2.8.5'
}
publishing {
publications {
msdk(MavenPublication) {
groupId 'com.ndefiorenze'
artifactId 'lib-a'
version "1.0.0"
artifact(bundleReleaseAar)
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each {
if(it.group != null && (it.name != null || "unspecified" == it.name) && it.version != null) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
repositories {
maven {
url "${System.env.HOME}/.m2/repository"
}
}
}
应用程序构建.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.ndefiorenze.dependencyagain"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(path: ':liba')
// implementation "com.ndefiorenze:lib-a:1.0.0"
}
项目 build.gradle:
buildscript {
ext.kotlin_version = '1.3.20'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenLocal()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
解决方案
我对自己最近对 gradle 所做的更改不熟悉,但如果我正确阅读了这篇文章,你想排除一个传递依赖,如图所示。
dependencies {
implementation('log4j:log4j:1.2.15') {
exclude group: 'javax.jms', module: 'jms'
exclude group: 'com.sun.jdmk', module: 'jmxtools'
exclude group: 'com.sun.jmx', module: 'jmxri'
} }
所以在你的应用程序构建文件中;
dependencies {
...
implementation project(path: ':liba'){
exclude group: 'com.google.code.gson', module:'gson'
}
应该阻止从应用程序访问 Gson。
推荐阅读
- javascript - 取决于弹出窗口的表格数据,父级的表格数据显示项目不同
- javascript - 客户端之间的firestore同步时间
- delphi - Delphi TDataSetProvider.ApplyUpdates 使用方法
- python - 检测具有特定颜色的圆形物体
- python - 比较两个字典的键。如果键匹配,则将这些键、值复制到 python 中的文件
- postgresql - Postgres 版本兼容性
- c++ - 如何使用现代 CMake 安装捆绑的接口依赖项?
- angular - Angular 5 和原生 IOS/Android 通信
- perl - websocket异步反馈长流程
- java - 为什么 ForkJoinPool::invoke() 会阻塞主线程?