Gradle First Use

Source: Internet
Author: User

The sanctuary of calm and ambition | Technology will be eliminated, but thought will remain forever (think more, summarize, share more)

Gradle's introduction, here is not wordy, Google on a lot of, here say its basic use it (can be simply understood as it is Maven advanced version, not so mysterious).

Basic usage Flow:

1. Download Gradle to local and configure the environment variables.

2. Create a Build.gradle file in your project

3. Writing Build.grade Scripts

4. Execute Grade Command

Quick ways to use:

Download Android Studio and let it get everything done for us.

Specify dependencies:

1. Dependencies on the jar in the warehouse

1 compile ‘group:name:version‘

2. Dependent on the local Jar folder

1 compile fileTree(dir, ‘libs‘,include : ‘*.jar‘)

3. Dependent on a single local jar file

1 compile file(‘libs/xxx‘)

3. Dependency Engineering

1 compile project(:xx:xx)

4. Android Library Dependent

You need to apply plugin: ' Android ' to apply plugin: ' Android-library ' in the library's Build.gradle file, otherwise the following error will be reported:

? Gradle error– Duplicate Files copied in APK androidmanifest.xml

Warehouse Configuration:

1. Download from the Central Warehouse: Mavencentral ()

2. Use the local Maven repository: maven {? url "file://F:/githubrepo/releases"???}

3. Specify the remote repository: Maven{url "Https://xxx"}

Multi-project Management

1. Join our project structure as follows:

myproject/

+ App/

?? +? libraries/

????? + LIB1/

???? ? +? lib2 /

2. The outer root configuration Setting.gradle, the introduction of various projects

Include ': App ',? ': libraries:lib1′,? ': libraries:lib2′

3. Each project will contain a build.gradle file that declares the compilation process for the project

myproject/

| Settings.gradle

+ app/

? ? |? build. Gradle

+? libraries/

+ lib1/

? ? ? ? ? ? |? build. Gradle

???? +? lib2/

? ? ? ? ? ? |? build. Gradle

4. What we need to do next is to list the items that depend on the app:

Dependencies? {

Compile? Project (': libraries:lib1′)

Compile? Project (': libraries:lib2′)

}

Gradle Compilation of Android projects:

We usually configure the dependent Gradle version, the Android build version, and the Android Builder version in Build.gradle, which will cause the Gradle build to fail if there are inconsistencies with the local presence.

1. The Gradle version relies on the following configuration:

Classpath ' com.android.tools.build:gradle:0.12.+ '

We need to see if our locally installed Gradle version is greater than or equal to this version number. (Android Studio recommends using 0.12+, so it is recommended to use Android Studio, which will help us download the response version of Android Studio)

2. Android compiled version

? ? Compilesdkversion 19

Ensure that the appropriate version of the SDK API is nearly downloaded in the local SDK

3. Android Build tool version

Buildtoolsversion "19.1.0″

For Android compiled version, it is also recommended to use Android Studio, we compile through Gradle, if the version does not match, it will prompt no this version of the tool, and prompted to download, of course, you can also go to the SDK manual download (not recommended).

Use Gradle to load the local. So library in Android Studio: Method One: Add custom tasks:
01 task nativeLibsToJar(type: Zip, description: ‘create a jar archive of the native libs‘) {
02 ????destinationDir file("$buildDir/native-libs")
03 ????baseName ‘native-libs‘
04 ????extension ‘jar‘
05 ????from fileTree(dir: ‘libs‘, include: ‘**/*.so‘)
06 ????into ‘lib/‘
07 }
08 ?
09 tasks.withType(Compile) {
10 ????compileTask -> compileTask.dependsOn(nativeLibsToJar)
11 }

The above means that, in the process of compiling the Nativelibstojar task, the task is to create a new Native-libs folder in the build directory, and then rename to Native-libs.jar, so files are added to the jar file.

Method two: Manually generated. Jar files are automatically unpacked to the APK file

This method needs to be done manually. So file compression, in the following steps: Compress all required. So files into a. zip file (the file directory structure in the zip is:? lib/armeabi/*.so) and then change the zip file suffix to. Jar and then put it in Libs to generate the apk on OK

Method Three: Gradle official in the new version has been automatically implemented packaging. So file

Add the code in Sourcesets, main:

jniLibs.srcDirs = [‘libs‘]

This will not require a manual copy. My project is a multi-project dependency, Android-library project contains so files, using method one, the app package can not be loaded so file, crying, because the project only used the black box, the problem of troubleshooting for several days, shite, So the recommended Android items to load so files are all using JNI methods.

Reference: http://blog.csdn.net/qiujuer/article/details/24209457

Add the compiled jar package to the dependencies:
1 dependencies {
2 ????compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
3 ????compile fileTree(dir: "$buildDir/native-libs", include: ‘native-libs.jar‘)
4 }

Reference: Http://stackoverflow.com/questions/16683775/include-so-library-in-apk-in-android-studio

Use Gradle to package Android Lib and make a dependent jar:
android.libraryVariants.all { variant ->
????defname = variant.buildType.name
????deftask = project.tasks.create "jar${name.capitalize()}", Jar
????task.dependsOn variant.javaCompile
????task.from variant.javaCompile.destinationDir
????task.from configurations.compile.findAll{
????????it.getName() != ‘android.jar‘&& !it.getName().startsWith(‘junit‘) && !it.getName().startsWith(‘hamcrest‘)
????}.collect{
????????it.isDirectory() ? it : zipTree(it)
????}
????artifacts.add(‘archives‘, task);
}

Note:

The generated Lib folder may not be visible in Android studio, so it is recommended to look at the generated jar file in the form of the asking file directory.

Gradle First Use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.