无法通过子模块的传递使用找到 lib

Can#39;t find lib via transitive use of sub module(无法通过子模块的传递使用找到 lib)
本文介绍了无法通过子模块的传递使用找到 lib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

Android Studio 3.4.2

Android Studio 3.4.2

我有使用模块 mytransport 的主项目(应用程序),如下所示:

I has main project (app) that use module mytransport like this:

app/build.gradle

dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"

    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 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') { transitive = true; }
    implementation 'com.google.android.material:material:1.1.0-alpha07'
    implementation "org.androidannotations:androidannotations-api:$AAVersion"

    implementation project(':mytransport')

}

mytransport/build.gradle:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.code.gson:gson:2.8.5'

}

mytransport 代码中,我成功使用了这样的代码:

In mytransport code I success use code like this:

MyProjectmytransportsrcmainjavacommycompanyandroidmytransportutilMyUtil.java

片段:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class JSONUtil {
private static GsonBuilder gsonbuilder = new GsonBuilder();

不错.

现在我想在主应用程序中使用 gson 库.所以在主应用程序我试试这个

Now I want in main app to use gson lib. So in main app I try this in

MyProjectandroidMyProjectappsrcmainjavacommycompanyandroidmyprojectmainMainApp.java

片段

import android.app.Application;
import android.content.Context;
public class MainApp extends Application {

private static GsonBuilder gsonbuilder = new GsonBuilder();

但我得到编译错误:

Cannot resolve symbol 'GsonBuilder'

为什么在主应用中找不到gson lib?我通过 mytransport 模块使用它TRANSITIVE

Why it can't find gson lib in main app? I use it TRANSITIVE by mytransport module

推荐答案

使用 api 而不是 implementation.

mytransport/build.gradle:

dependencies {
   //...
   api 'com.google.code.gson:gson:2.8.5'
}

只是一个例子.

library/build.gradle:

dependencies {
    api project(':libraryA')
}

app/build.gradle:

dependencies {
    api project(':library')
}

在您的应用中,您将能够访问 both librarylibraryA.

In your app you will be able to access both library and libraryA.

使用实现配置:

library/build.gradle:

dependencies {
    implementation project(':libraryA')
}

app/build.gradle:

dependencies {
    implementation project(':library')
}

在这种情况下,在您的应用中,您无法访问 libraryA 方法.

In this case in your app you can't access the libraryA methods.

这篇关于无法通过子模块的传递使用找到 lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)