1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Androidアプリのgradleについて

Last updated at Posted at 2019-06-17

AndroidStudioを使ってAndroidアプリを開発する際に、自動で作成されるgradleについて。
少し気になったことがあったのでメモ。

気になったこと

buil.gradleという名前のファイルが3つある。
スクリーンショット 2019-04-17 12.01.52.png
build.gradle(Project:プロジェクト名)
build.gradle(Module:app)
build.gradle(Module:mylibrary)

名前は同じだが、内容は全て異なっている。
それぞれのbuld.gradleの内容と役割について整理してみる。

build.gradle(Project:プロジェクト名)

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

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

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

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

コメントを見てみると、このgradleがトップレベル(プロジェクトファイルの直下?)にあるっぽい。
確かにこれと同じ内容のgradleファイルを探してみると、プロジェクトファイルの直下にあった。
スクリーンショット 2019-04-18 14.16.48.png
さらに、dependencies内のコメントをみると、このgradleファイルには依存関係を書いてはいけないらしい。
各モジュールごとにgradleファイルがあるので、そっちに記述してね、と。
ライブラリを使いたい等で編集する際のgradleファイルではなさそう。

build.gradle(Module.app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.sampleapplication.sample.apitest"
        minSdkVersion 27
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation project(':mylibrary')
}

こちらにはコメントなし。
ただし、1つ目のgradleとは内容が全く異なっている。
dependenciesの内容と、ファイルの位置から、外部のライブラリを使用する場合の依存関係はここに書けば良さそうだ。
ちなみにファイルの位置はappの直下。
スクリーンショット 2019-04-18 14.45.19.png

build.gradle(Module:mylibrary)

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28



    defaultConfig {
        minSdkVersion 27
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

こちらにも特にコメントは無し。
そして、おそらくこれはプロジェクト作成時点ではなかったはず。
ライブラリを追加したからこれも追加されたのだと思う。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?