Android Studio Gradle 不使用传递依赖项 [英] Android Studio Gradle not using transitive dependencies

查看:42
本文介绍了Android Studio Gradle 不使用传递依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个单独的项目中遇到过这个问题,我做了一个测试项目来验证这个问题.

Having experienced this problem in a separate project, I made a test project to verify the issue.

  1. 打开 Android Studio 3.0,我创建了一个新的基础项目.它的主要模块被称为app.
  2. 我添加了一个名为 libraryone 的库模块.
  3. libraryone中,我向Gson添加了一个依赖项,并使用Gson添加了一个带有单个静态方法的类.
  4. app 中,我添加了对libraryone 的依赖.我对它进行了简单的测试,并使用 transitive = true 进行了测试,因为互联网似乎模糊地同意这样做可能会有所帮助.(它没有.)
  5. appMainActivity 中,我使用了libraryone 中的类.这有效(只要我没有设置 transitive = false ,而是).
  6. 但是,我无法从 MainActivity 本身引用 Gson.它给出了无法解析符号'Gson'"错误,无论是来自Android Studio,还是来自命令行Gradle.这是......不寻常且令人讨厌,因为这意味着您必须在整个项目组中重复公共依赖项,并且无论如何都将类包含在输出中.肯定是我做错了什么,还是这是一个错误?
  1. Opening Android Studio 3.0, I created a new basic project. Its main module was called app.
  2. I added a library module, called libraryone.
  3. In libraryone, I added a dependency to Gson, and added a single class with a single static method using Gson.
  4. In app, I added a dependency to libraryone. I tested it plain, as well as with transitive = true, as the internet seemed to vaguely concur that doing so might help. (It did not.)
  5. In app, in MainActivity, I used the class from libraryone. This worked (as long as I didn't have transitive = false set, instead).
  6. However, I cannot reference Gson from MainActivity itself. It gives a "Cannot resolve symbol 'Gson'" error, both from Android Studio, as well as from the command line Gradle. This is...unusual and aggravating, as it means you have to repeat common dependencies all throughout a group of projects, and the classes are included in the output anyway. Surely either I'm doing something wrong, or this is a bug?

我的代码如下:

MainActivity.java(在app中)

MainActivity.java (in 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'"
    }
}

LibClass.java(在libraryone中)

LibClass.java (in 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);
    }
}

build.gradle (app)

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'))
}

build.gradle (libraryone)

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'
}

推荐答案

这是因为您将传递依赖声明为 implementation.使用 api 而不是 implementation 更改:

It is because you declared the transitive dependency as implementation. Use api instead of implementation change:

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

到:

api 'com.google.code.gson:gson:2.8.2'

可以在这里找到您应该在库中声明传递依赖的原因

这篇关于Android Studio Gradle 不使用传递依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆