[英]Gradle Android dependency resolution, differences between local module and maven lib
我做了一个测试项目来验证问题。
app
。 libA
的库模块。 libA
,我使用 implementation 'com.google.code.gson:gson:2.8.5'
Gson
使用单个方法添加了单个ClassA
类。 app
,我向libA
添加了一个依赖libA
implementation project(path: ':liba')
libA
使用了libA
的类。 正如预期的那样,从主要活动来看, Gson
无法到达 。 maven-publish
插件向maven local发布了lib库 ./gradlew :liba:publishMsdkPublicationToMavenLocal
implementation "com.ndefiorenze:lib-a:1.0.0"
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
Log.d("classA", new Gson().toJson(list));
liba
处于行家状态时,如何防止Gson在app
中暴露?
这里有一些资料:
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"
}
}
}
应用程序build.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从应用程序访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.