額..今天學習的過程中,就是在導入別人module的時候稍微遇到2個問題。
一、先說第一個,以往導入的module基本都不需要project的gradle,久而久之就成了module裏面導module這種錯誤的模式,不過因爲不影響學習,也沒那麼在意。
然後,今天遇到的問題就是一個文件夾下緊接着就是兩個module,在module中導入第一個module的時候還是正常的,在第二個module導入的時候提示叫我導入module,這就是說,此時在導入第二個module的時候,AS並沒有識別出我指定的這個module是個module..有點繞口。簡而言之,就是我在一個存在的module下連續導入了兩個module,但是第二個沒有被識別出來。
然後解決辦法是新建了一個project,先運行看看能不能編譯運行先,然後再導入兩個module,此時還沒完。
在setting.gradle裏改
include ':app',':module~1',':module~2'
然後關閉,重啓。
然後可以了。
結論就是一定要project下導入module,這樣一來網上大部分要這樣那樣的gradle的教程都能解決很多問題了,不要嘗試一些非主流,不過新手確實也非常容易犯這種錯誤。
二、再說第二個問題,前面說了要新建一個project然後運行看一下,build.gradle能build它不。我覺得一般都不會,然後之前就覺得有問題,因爲有很多教程都說在project的build.gradle做下面的改動
repositories {
google()
jcenter()
maven { url 'https://maven.google.com'}
}
那個maven默認是沒有的,然後看樣子應該不是個例,很多人都存在這個情況,默認自動創建的Activity,HelloWorld都沒法build…..額..有點..那啥,google在配置build.gradle上面還是有些不盡人意啊..
然後貼上我的project的build.gradle信息還有app的build.gradle,可能對某些道友會有幫助
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven { url 'https://maven.google.com'}
}
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
//這裏寫你自己的build.gradle版本
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "com.example.XXXX"
minSdkVersion 14
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 {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.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'
}