首页 > 解决方案 > Gradle Android依赖解析,本地模块和maven lib的区别

问题描述

我做了一个测试项目来验证这个问题。

  1. 打开 Android Studio
  2. 我创建了一个新的基本项目。它的主要模块被称为app.
  3. 我添加了一个库模块,名为libA.
  4. libA中,我添加了对 Gson 的依赖

    implementation 'com.google.code.gson:gson:2.8.5'

    项,并ClassA使用Gson.
  5. app中,我添加了一个依赖项libA

    implementation project(path: ':liba')

    在 MainActivity 中,我使用了来自 的类libA,没有问题。Gson正如预期的那样,从主要活动中无法访问
  6. maven-publish我将一个使用插件的lib发布到maven local

    ./gradlew :liba:publishMsdkPublicationToMavenLocal

  7. 我已将导入从上面的一个切换到

    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
}

标签: javaandroidmavengradle

解决方案


我对自己最近对 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。


推荐阅读