简体   繁体   中英

Android library Correct usage of V4 Support Library Confusion

I am trying to reduce the apk size for my project and for that I am trying to remove having the entire support-v4 library dependency, but rather only adding the modules I need for my project. According to below, it seems possible to only include the modules I need instead of importing the entire support-v4 library(1). My current project is using support-v4(entire library) in gradle. Whenever I use, in my activities classes like ActivityCompat on the import header I see: "import android.support.v4.app.ActivityCompat;". My confusion is that I have removed the support-v4 library from gradle and only including the module needed for the ActivityCompat class, that is: "com.android.support:support-compat:27.0.0". Why when I try to use ActivityCompat class it is still importing the same "import android.support.v4.app.ActivityCompat;"? Is this expected?

From Android reference: https://developer.android.com/reference/android/support/v4/app/ActivityCompat.html

(1)"Note: Prior to Support Library revision 24.2.0, there was a single v4 support library. That library was divided into multiple modules to improve efficiency. For backwards compatibility, if you list support-v4 in your Gradle script, your APK will include all of the v4 modules. However, to reduce APK size, we recommend that you just list the specific modules your app needs."

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;<--regardless of whether I include support-v4 or only the module needed from android(com.android.support:support-compat:27.0.0) same import is still being used.
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();
            }
        });

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

Per the ActivityCompat documentation (specifically, in the upper right):

belongs to Maven artifact com.android.support:support-compat:27.0.0

So the class you are using is in the support-compat dependency, as expected. The naming for the dependencies is only loosely related to the naming of the packages the classes are in (obviously, changing the packages for the classes would break everyone that upgrades, which probably wouldn't be ideal).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM