3

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 MyProject\mytransport\src\main\java\com\mycompany\android\mytransport\util\MyUtil.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

MyProject\android\MyProject\app\src\main\java\com\mycompany\android\myproject\main\MainApp.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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Alex
  • 1,857
  • 4
  • 17
  • 34

1 Answers1

7

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • What about this : dependencies { annotationProcessor "org.androidannotations:androidannotations:$AAVersion" ? This is in library – Alex Jul 13 '19 at 09:22
  • @Alex your main app/build.gradle has both of these dependencies. annotationProcessor "org.androidannotations:androidannotations:$AAVersion" implementation "org.androidannotations:androidannotations-api:$AAVersion". remove implementation. – Ranjan Kumar Jul 16 '19 at 07:08
  • 2
    This is the most descriptive answer and easy to understand and it works!!! Thanks @gabriele-mariotti – mochadwi Nov 12 '19 at 08:43