NullPointerException
NullPointerException

Reputation: 37635

Is necessary to import support-v4 and appcompat-v7 to use them?

I'm using AppCompatActivity and some more things like fragments etc... These are some of the imports of my activity:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;

I copyed from the google samples these dependencies:

dependencies {
    compile 'com.google.android.gms:play-services-ads:9.4.0'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

Now I'm trying to delete them and i noticed that I can delete these:

compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:appcompat-v7:23.4.0'

Deleting them, my project works perfectly... why?

I'm compiling with this:

compileSdkVersion 23
buildToolsVersion "23.0.3"
minSdkVersion 14
targetSdkVersion 22

Upvotes: 5

Views: 2793

Answers (1)

stkent
stkent

Reputation: 20128

The support design library itself depends on the appcompat-v7 and support-v4 libraries. So the explicit dependencies you removed are pulled in automatically as transitive dependencies. Running ./gradlew app:dependencies confirms this:

_releaseCompile - ## Internal use, do not manually configure ##
+--- com.android.support:design:23.4.0
|    +--- com.android.support:recyclerview-v7:23.4.0
|    |    +--- com.android.support:support-annotations:23.4.0 -> 24.0.0
|    |    \--- com.android.support:support-v4:23.4.0 -> 24.0.0 (*)
|    +--- com.android.support:appcompat-v7:23.4.0 -> 24.0.0
|    |    +--- com.android.support:support-v4:24.0.0 (*)
|    |    +--- com.android.support:support-vector-drawable:24.0.0
|    |    |    \--- com.android.support:support-v4:24.0.0 (*)
|    |    \--- com.android.support:animated-vector-drawable:24.0.0
|    |         \--- com.android.support:support-vector-drawable:24.0.0 (*)
|    \--- com.android.support:support-v4:23.4.0 -> 24.0.0 (*)

It's still generally considered a good practice to explicitly declare these dependencies in your build.gradle file (paraphrasing the linked answer):

If your project has direct dependencies on "B" then you should declare "B" as an explicit dependency even if "B" is a transitive dependency of some other explicit dependency "A". Future versions of "A" may no longer depend on "B", and updating to one of these versions of "A" would break your build.

Upvotes: 9

Related Questions