Android Studio Gradle不使用传递依赖项

Erh*_*nis 6 dependencies android transitive-dependency android-gradle-plugin

在一个单独的项目中遇到此问题后,我进行了一个测试项目以验证该问题。

  1. 打开Android Studio 3.0,我创建了一个新的基本项目。其主要模块称为app
  2. 我添加了一个名为的库模块libraryone
  3. 在中libraryone,我向Gson添加了一个依赖项,并使用Gson使用单个静态方法添加了一个类。
  4. 在中app,我向添加了一个依赖项libraryone。我和一起进行了简单的测试transitive = true,因为互联网似乎模糊地同意这样做可能会有所帮助。(它没有。)
  5. app,在MainActivity,我使用的班级libraryone。这行得通(只要我没有transitive = false设置)。
  6. 但是,我无法从MainActivity自身引用Gson 。无论是从Android Studio还是从命令行Gradle中,它都会给出“无法解析符号'Gson'”错误。这是不寻常的,而且令人不快,因为这意味着您必须在整个项目组中重复通用的依赖关系,并且这些类仍会包含在输出中。当然是我做错了什么,还是这是个错误?

我的代码如下:

MainActivity.java(在中app

package com.erhannis.test.dependencytest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.erhannis.test.libraryone.LibClass;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String s = LibClass.convert(new Object());
        System.out.println("out: " + s);

        new com.google.gson.Gson(); // This line gives a compile-time error: "Cannot resolve symbol 'Gson'"
    }
}
Run Code Online (Sandbox Code Playgroud)

LibClass.java(在中libraryone

package com.erhannis.test.libraryone;

import com.google.gson.Gson;

public class LibClass {
    public static String convert(Object o) {
        Gson gson = new Gson();
        return gson.toJson(o);
    }
}
Run Code Online (Sandbox Code Playgroud)

build.gradle(app

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.erhannis.test.dependencytest"
        minSdkVersion 18
        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 {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    implementation (project(path: ':libraryone'))
}
Run Code Online (Sandbox Code Playgroud)

build.gradle(libraryone

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    defaultConfig {
        minSdkVersion 18
        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 {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    implementation 'com.google.code.gson:gson:2.8.2'
}
Run Code Online (Sandbox Code Playgroud)

Non*_*hoi 13

我认为是因为implementation。尝试api而不是implementation更改:

implementation (project(path: ':libraryone'))
Run Code Online (Sandbox Code Playgroud)

至 :

api project(path: ':libraryone')
Run Code Online (Sandbox Code Playgroud)

和:

implementation 'com.google.code.gson:gson:2.8.2'
Run Code Online (Sandbox Code Playgroud)

至:

api 'com.google.code.gson:gson:2.8.2'
Run Code Online (Sandbox Code Playgroud)


Gab*_*tti 7

您正在使用:

implementation (project(path: ':libraryone'))
implementation 'com.google.code.gson:gson:2.8.2'
Run Code Online (Sandbox Code Playgroud)

检查官方文档

当您的模块配置implementation依赖项时,它会让Gradle知道该模块不想在编译时将依赖项泄漏给其他模块。即,该依赖关系仅在运行时可用于其他模块。

在这里您可以找到一个很好的博客