2018-12-08 Android Studio项目的配置

2018-12-08  本文已影响0人  62kl

说明
本文主要介绍基于Android Studio开发android project的一些配置。

1、Android Studio安装及基本配置
这里用的版本是3.2

具体安装过程不再详述

2、gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=file:///d:/Application/Android/gradle/gradle-4.6-all.zip   #gradle的本地zip包
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

3、local.properties

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=D\:\\Application\\Android\\android-sdk

4、gradle.properties

## For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -X:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true
#Tue Nov 27 22:22:56 CST 2018
org.gradle.daemon=true

注意:此处不需要设置代理,设置代理后有可能会导致后面gradle及相关依赖无法下载

注意:如果提示如下错误

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=512m; support was removed in 8.0
Unrecognized option: -X:+HeapDumpOnOutOfMemoryError

可根据自己使用的jdk情况修改,如:

org.gradle.jvmargs=-Xmx2048m -Xmx512m  -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

4、Project层面的build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
#下面两个国内可以直接访问,并不需要配置代理
google()   #一定要放在第一个,否则会导致“Could not find aapt2-windows.jar”的问题
jcenter()
maven { url'http://maven.aliyun.com/repository/apache-snapshots' }
maven { url'http://maven.aliyun.com/repository/central' }
maven { url'http://maven.aliyun.com/repository/google' }
maven { url'http://maven.aliyun.com/repository/gradle-plugin' }
maven { url'http://maven.aliyun.com/repository/jcenter' }
maven { url'http://maven.aliyun.com/repository/spring' }
maven { url'http://maven.aliyun.com/repository/spring-plugin' }
maven { url'http://maven.aliyun.com/repository/public' }
maven { url'http://maven.aliyun.com/repository/releases' }
maven { url'http://maven.aliyun.com/repository/snapshots' }
maven { url'http://maven.aliyun.com/repository/grails-core' }
maven { url'http://maven.aliyun.com/nexus/content/groups/public/' }
maven{ url'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
}
dependencies {
classpath'com.android.tools.build:gradle:3.2.0'  //最好用创建项目后自动指定的版本,否则有可能会有版本兼容问题
//版本的一些兼容要求,可参考https://developer.android.google.cn/studio/releases/gradle-plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
    }
}
allprojects {
repositories {
google()
jcenter()
maven { url'http://maven.aliyun.com/repository/apache-snapshots' }
maven { url'http://maven.aliyun.com/repository/central' }
maven { url'http://maven.aliyun.com/repository/google' }
maven { url'http://maven.aliyun.com/repository/gradle-plugin' }
maven { url'http://maven.aliyun.com/repository/jcenter' }
maven { url'http://maven.aliyun.com/repository/spring' }
maven { url'http://maven.aliyun.com/repository/spring-plugin' }
maven { url'http://maven.aliyun.com/repository/public' }
maven { url'http://maven.aliyun.com/repository/releases' }
maven { url'http://maven.aliyun.com/repository/snapshots' }
maven { url'http://maven.aliyun.com/repository/grails-core' }
maven { url'http://maven.aliyun.com/nexus/content/groups/public/' }
maven{ url'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
}
}
task clean(type: Delete) {
deleterootProject.buildDir
}

备注:本文将http://maven.aliyun.com/mvn/view中的path全部配置到maven中了,如:maven { url'http://maven.aliyun.com/repository/grails-core' }

image.png

另外,本文没有用内嵌的maven repository,具体设置如下图:

image.png

5、Module层面的build.gradle

applyplugin:'com.android.application'
android {
compileSdkVersion28
    defaultConfig {
applicationId "com.taoping.testnew"
        minSdkVersion19
        targetSdkVersion28
        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-alpha1'
    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'
}

5、gradle的设置


image.png

参考资料:

https://developer.android.google.cn/studio/build/

https://www.jianshu.com/p/6f3c34b36037

https://blog.csdn.net/lengshang624/article/details/83376622

https://stackoverflow.com/questions/52541525/android-studio-3-2-could-not-find-com-android-tools-buildaapt23-2-0-4818971

上一篇下一篇

猜你喜欢

热点阅读