Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

android - Can't find lib via transitive use of sub module

Android Studio 3.4.2

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

}

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

}

In mytransport code I success use code like this:

in MyProjectmytransportsrcmainjavacommycompanyandroidmytransportutilMyUtil.java

snippet:

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();

Nice.

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

MyProjectandroidMyProjectappsrcmainjavacommycompanyandroidmyprojectmainMainApp.java

snippet

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

private static GsonBuilder gsonbuilder = new GsonBuilder();

but I get compile error:

Cannot resolve symbol 'GsonBuilder'

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use api instead of implementation.

In mytransport/build.gradle:

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

Just an example.

In library/build.gradle:

dependencies {
    api project(':libraryA')
}

In app/build.gradle:

dependencies {
    api project(':library')
}

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

Using the implementation configuration:

In library/build.gradle:

dependencies {
    implementation project(':libraryA')
}

In app/build.gradle:

dependencies {
    implementation project(':library')
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...