Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure gradle in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces "how to configure gradle in Android" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to configure gradle in Android" can help you solve your doubts.

/ / apply plugin: 'com.android.library' / / Library configuration apply plugin:' com.android.application' / / when the application configuration repositories {/ / introduces the AAR file, you need to configure this. The AAR file is placed in the libs directory flatDir {dirs' libs'}} android {compileSdkVersion 25 / / android compiled SDK version That is, the android.jar file buildToolsVersion "25.0.2" of 4.0SDK, 5.0SDK, etc. / / use the version of useLibrary "org.apache.http.legacy" of the compilation tool in SDK / / use the httpClient package of apache on 6.0. The reason is that google has removed the http request library defaultConfig {applicationId "com.xxx" / / the package name of the application can refer to the package name minSdkVersion 15 / / minimum compatible version targetSdkVersion 25 / / target version versionCode 1 / / the version number of the application versionName SDK_VERSION / / in AndroidMainfest.xml using ${applicationId}. The version name of multiDexEnabled true / / enables multiple dex If the number of code methods in app exceeds 65535 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" / / android unit test configuration} sourceSets {/ / specify the path to the code and resources For more information, please refer to http://google.github.io/android-gradle-dsl/2.3/com.android.build.gradle.api.AndroidSourceSet.html main {manifest.srcFile 'AndroidManifest.xml' / / specify the manifest.xml path java.srcDirs = [' src'] / / the path of the java file, one layer above the package name, and multiple directories are separated by commas For example, ['src',' core'] resources.srcDirs = ['src'] / / all directories of resource resources, note that this refers to some resources contained in jar files, such as properties files Instead of the res resource aidl.srcDirs in APK = ['src'] / / the directory of the aidl file renderscript.srcDirs = [' src'] / / the path to the renderscript file res.srcDirs = ['res'] / / the resource path in assets.srcDirs = [' assets'] / / android app asset directory jniLibs.srcDirs = [' Libs'] / / path to the SO library}} lintOptions {checkReleaseBuilds false / / release disable lint at compile time to check abortOnError false / / error will not stop packaging Unless it seriously affects disable 'MissingTranslation',' ExtraTranslation' / / disables some of the options in the lint check} dexOptions {javaMaxHeapSize "4G" / / sets the maximum heap memory size when compiling project code, otherwise the project is too large Compile memory overflow} compileOptions {/ / for more information, please see http://google.github.io/android-gradle-dsl/2.3/com.android.build.gradle.internal.CompileOptions.html sourceCompatibility JavaVersion.VERSION_1_7 / / set the compiled version of the code. This is generally configured when using JDK1.8. Make compiled jar packages more generic when used by others. TargetCompatibility JavaVersion.VERSION_1_7} packagingOptions {exclude 'META-INF/DEPENDENCIES.txt' / / exclude declaration files in these third-party jar Otherwise, it is easy to cause exclude 'META-INF/LICENSE.txt' exclude' META-INF/NOTICE.txt' exclude 'META-INF/NOTICE' exclude' META-INF/LICENSE' exclude 'META-INF/DEPENDENCIES' exclude' META-INF/notice.txt' exclude 'META-INF/license.txt' exclude' META-INF/dependencies.txt' when compiling. Exclude 'META-INF/LGPL2.1'} buildTypes {debug {storeFile file ("debug.keystore") / / signature file relative path storePassword "android" / / signature password keyAlias "androiddebugkey" / / alias keyPassword "android" / / alias password buildConfigField "boolean" "FLAG_DEBUG", "true" / / in BuildConfig. Public static final boolean FLAG_DEBUG = true is automatically generated in the class of The code buildConfigField "String", "API_VERSION", "\" 1\ "ndk {abiFilters" armeabi "," armeabi-v7a "/ / retains only the SO libraries of these CPU schemas, and requires a higher version of gradle to support} / / jniDebuggable true / / enable JNI debug. Generally, it is rarely used. This option is not recommended, it will affect the debug speed of java code} release {buildConfigField "boolean", "FLAG_DEBUG", "false" buildConfigField "String", "API_VERSION", "\" 1\ "minifyEnabled true / / remove useless content in code shrinkResources true / / remove useless resources in confusion For the content in the res/ directory, there is no need to compress the size of the image proguardFiles getDefaultProguardFile ('proguard-android.txt'),' proguard-rules.pro' / / configure confusion files ndk {abiFilters "armeabi", "armeabi-v7a" / / keep only the SO libraries of these CPU schemas A higher version of gradle is required to support} dependencies {compile fileTree (include: ['* .jar'], dir: 'libs') / / Import all jar packages androidTestCompile in the libs directory (' com.android.support.test.espresso:espresso-core:2.2.2', {/ / exclude modle in group) Notice the name of group and module com.android.support:support-annotations exclude group: 'com.android.support' Module: 'support-annotations'}) compile' com.android.support:appcompat-v7:25.1.0' / / use google's appcompat-v7 package testCompile 'junit:junit:4.12' / / introduce junit unit test compile' com.android.support:multidex:1.0.0' / / join load multiple dex libraries compile files ('libs/gson.jar') / / reference libs directory Gson.jar package compile (name: 'HMS-SDK-2.4.0.300') in Ext: 'aar') / / introduce the HMS-SDK-2.4.0.300.aar file At the same time, you also need to refer to the configuration compile ('com.facebook.fresco:fresco:1.0.0') {exclude module:' support-v4'} / / in the header of the file to introduce the fresco library, but do not use the referenced support-v4 library, otherwise it will lead to repeated introduction, and the compilation error duplicate provided fileTree (dir: 'compilelibs', include: [' * .jar']) / / will be introduced into the jar file under the compilelibs directory to participate in the compilation. However, the code for these packages is not entered into APK, jar, or AAR. } / / generate jar files using the jar task AssembleRelease dependent tasktask buildJar (type: Jar, dependsOn: ['assembleRelease']) {destinationDir = file (' build/outputs/jar/') appendix = "" baseName = "" version = SDK_VERSION / / manifest information def map = ['Version': SDK_VERSION,' Gradle': project.gradle.gradleVersion, 'Vendor':' szcomtop.com' 'Date': new Date (). GetDateTimeString ()] manifest.attributes (map) / / from (' build/intermediates/classes/release/') from (project.zipTree ('build/intermediates/transforms/proguard/release/jars/3/3/main.jar')) exclude (' * / BuildConfig.class') exclude ('* / BuildConfig\ $* .class') exclude ('* / R .class') exclude ('* / R\ $* .class') include ('* * / * .class')} / / copy the content task copySDK (type: Copy) using the Copy task DependsOn: ['buildJar']) {from (' build/outputs/jar') into ('.. / app/libs/') include ("* .jar")}

Usage

This party will be updated continuously, and some configurations may change with the upgrade of android's gradle tool. For example, ndk.abiFilters needs to be used in a high version of gradle tools. How to upgrade the gradle version may also bring problems such as failure of compilation, which takes a long time to solve, so please be careful.

With regard to the use of some configurations, here is one example, and the others are the same.

For example, how do you know about this compileOptions configuration? What can be set under this configuration? How do I use these settings? What values can I give?

CompileOptions {sourceCompatibility JavaVersion.VERSION_1_7 / / sets the version of the code to be compiled, which is generally configured when using JDK1.8 to make the compiled jar package more general-purpose for others to use.

Start with the official website

Android official definition of the gradle tool to use the documentation point here, gradle official documentation point here.

There is a DSL in the open link. What is this DSL? DSL is the abbreviation of Gradle Build Language. Haha, just kidding. It's an acronym for Domain Specific Language. Domain can be understood as Project.

Digress. Go on.

CompileOptions example

Open the official documentation page of the android gradle tool and you will see the version selection page shown in the following figure:

This version corresponds to the gradle version in the build.gradle file in the root directory of the android studio project. It is also found that this version corresponds to the version of android studio. As shown in the following figure:

Click the link of the current version 2.2 to go to the following interface, as shown in the following figure, find compileOptions on the left, and click the link (you can start with the Home item on the left to familiarize yourself with this official document):

Click the link marked in the red box in the above figure, and you will jump to the configuration description of the location shown in the following figure, which is similar to Java's API, except that you should see the details at this time, but the result is not:

If you need to check the details further, you can only click the link marked in the red box above, and you will find familiar content after entering, as shown in the following figure:

Is there a feeling that I have not seen for a long time that I am about to see the truth? One more step. The options available in compileOptions and their meaning are clearly written above. Clicking sourceCompatibility will jump to the content shown in the following figure:

After reading this, the article "how to configure gradle in Android" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report