如果已经包含support-design
,是否需要在gradle中包含support-v4
和appcompat-v7
?我看到当包含support-design support-v4
和appcompat-v7
时,即使它们不在 gradle 中,它们也已经是我的外部库的一部分。 我知道support-design
取决于appcompat-v7
,appcompat-v7
取决于support-v4
我只想知道从 gradle 中删除support-v4
和appcompat-v7
是否会导致任何问题。
我还想排除support-v4
(以减少apk大小(,我已经通过将其从support-design
中排除并仅从所需的support-v4
中添加模块来做到这一点,我的方向是否正确? 见下文:
格拉德尔:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.test.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'){
exclude group: 'com.android.support', module: 'support-v4'
}
compile 'com.android.support:support-fragment:26.0.1'
//compile 'com.android.support:support-compat:26.0.1'
//compile 'com.android.support:support-v4:26.0.1'
testCompile 'junit:junit:4.12'
}
我的活动:
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.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();
}
});
//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);
}
}
Android 支持库套件中的大多数库都依赖于一个或多个库。例如,几乎所有支持库都依赖于支持兼容包。通常,您无需担心支持库依赖项,因为 gradle 构建工具通过自动包含依赖库来为您管理库依赖项。
如果您想查看应用中包含哪些库和库依赖项,请在应用开发项目的构建根目录下运行以下命令,以获取该项目依赖项的报告,包括 Android 支持库和其他库:
gradle -q dependencies your-app-project:dependencies
查看安卓文档中的详细信息