为什么我仍然可以使用support-v4库而无需在gradle上导入它?

时间:2018-01-28 03:08:06

标签: java android android-studio gradle support-v4

我很困惑,在我的gradle中我没有导入support-v4,但我似乎能够使用该库,默认情况下是support-v4作为依赖项包含在内吗?见下文?同样在我的项目文件夹中,每次创建项目和各个模块时,我都能看到jar支持v4?

项目gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven{
            url "https://maven.google.com"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的应用程序gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.ok.applicationtest_v4lib"
        minSdkVersion 19
        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 {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.0.1'
    testCompile 'junit:junit:4.12'
}
下面的

-activity能够导入support-v4而无需在gradle中导入support-v4:

package com.example.ok.applicationtest_v4lib;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        //I am able to use below class yet I have not imported v4 library.
        ActivityCompat
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:2)

如果您有appcompat-v7,则表示您有appcompat-v4

运行./gradlew app:dependencies,您会看到

+--- com.android.support:appcompat-v7:25.4.0
|    +--- com.android.support:support-annotations:25.4.0
|    +--- com.android.support:support-v4:25.4.0 (*)
|    +--- com.android.support:support-vector-drawable:25.4.0
|    |    +--- com.android.support:support-annotations:25.4.0
|    |    \--- com.android.support:support-compat:25.4.0 (*)
|    \--- com.android.support:animated-vector-drawable:25.4.0
|         +--- com.android.support:support-vector-drawable:25.4.0 (*)
|         \--- com.android.support:support-core-ui:25.4.0 (*)
相关问题