项目中的 build.gradle 与应用程序中的 build.gradle 有何区别?

8

我在Android Studio中使用IntelliJ开始了一个项目。

该项目包含两个名为build.gradle的文件。一个在app文件夹下,另一个在主文件夹下,也就是我的项目名称下,比如说MyProject

为什么需要两个?这两个build.gradle有什么区别?


与https://dev59.com/pV4c5IYBdhLWcg3wHXJS类似的问题。 - Suragch
2个回答

9

Android Studio 项目由模块、库、清单文件和 Gradle 构建文件组成。

每个项目都包含一个顶层 Gradle 构建文件。此文件名为 build.gradle,可在顶级目录中找到。

该文件通常包含所有模块的公共配置和公共函数等。

例如:

  //gradle-plugin for android
  buildscript {
    repositories {
        mavenCentral()  //or jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2'        
    }
  }

  // common variables
  ext {
     compileSdkVersion = 19
     buildToolsVersion = "20.0.0"
  }

  // a custom function
  def isReleaseBuild() {
     return version.contains("SNAPSHOT") == false
  }

  //common config for all projects
  allprojects {
     version = VERSION_NAME

     repositories {
       mavenCentral()
     }
  }

所有模块都有一个特定的build.gradle文件。该文件包含有关模块的所有信息(因为项目可能包含更多模块),如配置、构建类型、签名apk的信息、依赖项等。

例如:

apply plugin: 'com.android.application'


android {
    //These lines use the constants declared in top file
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionName project.VERSION_NAME  //it uses a property declared in gradle.properties
        versionCode Integer.parseInt(project.VERSION_CODE) 
    }

    // Info about signing
    signingConfigs {
        release
    }

    // Info about your build types
    buildTypes {
        if (isReleaseBuild()) {
            release {
                signingConfig signingConfigs.release
            }
        }

        debug {
            applicationIdSuffix ".debug"
            versionNameSuffix "-debug"
        }
    }

    // lint configuration
    lintOptions {
        abortOnError false
    }
}

//Declare your dependencies  
dependencies {
    //Local library
    compile project(':Mylibrary')
    // Support Libraries
    compile 'com.android.support:support-v4:20.0.0'
    // Picasso
    compile 'com.squareup.picasso:picasso:2.3.4'

}

你可以在这里找到更多信息: http://developer.android.com/sdk/installing/studio-build.html

有帮助的答案,谢谢。我从来没有做过带有多个模块的项目。在只有一个模块的情况下,如果要使用依赖项,是否需要使用哪个 build.gradle 文件无所谓? - Suragch
@Suragch 的依赖项始终在模块中。 - Gabriele Mariotti
你的第一个示例中,顶层 build.gradle 中的 dependencies { classpath com.android.tools.build:gradle:1.1.0' } 部分怎么处理? - Suragch
也许你可以在回答中加入Gradle构建文件的格式是什么?是一种领域特定语言吗? - Peter Mortensen

0

这是它的答案,当您以这种方式使用它时,它可以很好地工作。

import 'package:webapp/layout.dart';

const int largeScreenSize = 1366;
const int mediumScreenSize = 768;
const int smallScreenSize = 360;
const int customScreenSize = 1100;

class ResponsiveWidget extends StatelessWidget {
  final Widget largeScreen;
  final Widget? mediumScreen;
  final Widget? smallScreen;

  const ResponsiveWidget({
    Key? key,
    required this.largeScreen,
    this.mediumScreen,
    this.smallScreen,}) : super(key: key);

  static bool isSmallScreen(BuildContext context) =>
      MediaQuery.of(context).size.width < smallScreenSize;

  static bool isMediumScreen(BuildContext context) =>
      MediaQuery.of(context).size.width <= mediumScreenSize &&
      MediaQuery.of(context).size.width < largeScreenSize;

  static bool isLargeScreen(BuildContext context) =>
      MediaQuery.of(context).size.width <= largeScreenSize;

  static bool isCustomScreen(BuildContext context) =>
      MediaQuery.of(context).size.width >= mediumScreenSize &&
      MediaQuery.of(context).size.width <= customScreenSize;

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints){
        double _width = constraints.maxWidth;
        if(_width >= largeScreenSize){
          return largeScreen;
        }
        else if(_width < largeScreenSize && _width >= mediumScreenSize){
          return mediumScreen ?? largeScreen;
        }
        else {
          return smallScreen ?? largeScreen;
        }
      }

    );
  }
}




网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接