AndroidStudioを使ってAndroidアプリを開発する際に、自動で作成されるgradleについて。
少し気になったことがあったのでメモ。
気になったこと
buil.gradleという名前のファイルが3つある。
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ファイルを探してみると、プロジェクトファイルの直下にあった。
さらに、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の直下。
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'
}
こちらにも特にコメントは無し。
そして、おそらくこれはプロジェクト作成時点ではなかったはず。
ライブラリを追加したからこれも追加されたのだと思う。