【问题标题】:build.gradle in the project vs. build.gradle in the app项目中的 build.gradle 与应用程序中的 build.gradle
【发布时间】:2014-11-10 21:01:35
【问题描述】:

我在 Android Studio 中使用 IntelliJ 启动了一个项目。

该项目包括两个名为build.gradle 的文件。一个在文件夹app 下,一个在我的项目名称的主文件夹下,比如MyProject。

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

【问题讨论】:

标签: android android-studio gradle android-gradle-plugin build.gradle


【解决方案1】:

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 文件。 此文件包含有关 此 模块的所有信息(因为一个项目可以包含更多模块),如配置、构建 tyoes、用于签署您的 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 依赖始终在模块中
  • 第一个示例中顶级 build.gradle 中的 dependencies { classpath com.android.tools.build:gradle:1.1.0' } 部分怎么样?
  • 也许在您的答案中添加 Gradle 构建文件的格式? domain-specific language?
【解决方案2】:

这就是它的答案,当你以这种方式使用它时效果很好

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

    );
  }
}



【讨论】:

    【解决方案3】:

    build.gradle (Project:My-app)

    顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。

    每个项目都包含一个顶级 Gradle 文件。它通常包含所有模块的通用配置。无论这个顶级 Gradle gile 中包含什么,它都会影响所有模块。

    例子:

    // 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.0.0-alpha3'
    
            //Maven plugin
            classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            jcenter()
            maven { url "https://jitpack.io" }
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    build.gradle(模块:app)

    特定模块的构建文件(您可以在其中添加依赖项、签名配置、构建类型、风味等)

    所有模块都有一个特定的 Gradle 文件。无论这个 gradle 文件中包含什么,它只会影响包含的模块。

    例子:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
    
        defaultConfig {
            applicationId "com.hrskrs.gesturefun"
            minSdkVersion 10
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                zipAlignEnabled true
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            debug {
                debuggable true
                zipAlignEnabled true
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile project(':gesture-fun')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.1.1'
        compile 'com.android.support:design:23.1.1'
        compile 'com.jakewharton:butterknife:7.0.1'
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-02
      • 2013-06-18
      • 2019-05-19
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      相关资源
      最近更新 更多