List of new Android Gradle build tools: New DSL structure and Gradle 2.5

Source: Internet
Author: User

Translator Address: "Turn" list of new Android Gradle build tools: New DSL structure and Gradle 2.5

Original: First look at new Android Gradle Build tools:the new DSL structure and Gradle 2.5

Bilingual address: "Turn-bilingual" list new Android Gradle build tool: New DSL structure and Gradle 2.5

  • Translation: MRFU
  • Check: MRFU

The Android Studio 1.3 platform is already close to a stable release, with new features being rolled out, including the perfect support for the NDK. It seems that some major changes are also awaiting the right incubation time, such as: New Gradle build tools and newly designed DSL (Gradle script code structure)

After one hours of playing with me, I found it to be very interesting, so I decided to write this blog to tell you guys about the changes that the build tool is about to make ready for you.

What is the Android Gradle build tool (Android Gradle build tools)?

In case you don't know it yet! The Android Gradle build tool is a runtime used to process files under module build.gradle , before this file is passed to Gradle for further operation.

The Gradle Build Tools declaration in the project build.gradle is as follows:

{    classpath ‘com.android.tools.build:gradle:1.2.3‘}

Each version of Gradle build Tools works in conjunction with the supported Gradle versions listed below.

Android Gradle Plugin Gradle
1.0.0-1.1.3 2.2.1-2.3
1.2+ 2.2.1+

The syntax rules are defined in Android Gradle Build Tools, and we use the syntax rules build.gradle to write Gradle scripts in a file (and the syntax we used these days to write Gradle S Cript in Build.gradle file was defined in Android gradle build Tools.). We call it DSL (Domain-specific Language).

New Android Gradle Build tool

Since the advent of Gradle build Tools 1.0, the DSL has not been passive and the Android Studio team has decided to make significant changes to the new Gradle build tools. It is still in the experimental phase, by altering its underlying Gradle's new component model mechanism, which significantly reduces the time spent on configuration. However, the development team is trying to remove these generic changes to minimize the migration process from the traditional plugin in the future. (However development teams is working hard trying-remove these current changes-minimize the migration process from The traditional plugin in the future.)

In any case, frankly, the new DSL looks pretty good. I must say I am convinced of these changes because this new DSL structure and naming is more meaningful than it is now.

Try the new Gradle build tools simply by changing the build tools version in the project's build.gradle file:

{    classpath ‘com.android.tools.build:gradle-experimental:0.1.0‘}

Note that the new build tools will work with the Gradle 2.5 you just released, so you'll need to first install Gradle2.5 and modify the line under your project's gradle/gradle-wrapper.properties file distributionUrl :

distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-bin.zip

Go to the Settings page ( Windows system in File, Settings or Mac OS X in Android Studio, Preferences ), and make sure that you choose to use the default configuration of Gradle wrapper.

Then modify the module's build.gradle file from this:

Apply plugin:' Com.android.application 'Android {Compilesdkversion ABuildtoolsversion"23.0.0 rc3"Defaultconfig {ApplicationID"Com.inthecheesefactory.hellojni25"Minsdkversion theTargetsdkversion AVersioncode1Versionname"1.0"} buildtypes {release {minifyenabledfalseProguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro '}}}dependencies {Compile Filetree (dir:' Libs ', include: [' *.jar ']) Compile' com.android.support:appcompat-v7:22.2.0 '}

Change to this:

Apply plugin:' Com.android.model.application 'Model {android {compilesdkversion = ABuildtoolsversion ="23.0.0 rc3"Defaultconfig. with{ApplicationID ="Com.inthecheesefactory.hellojni25"Minsdkversion.apilevel = theTargetsdkversion.apilevel = AVersioncode =1Versionname ="1.0"}} android.buildtypes {release {isminifyenabled =falseProguardfiles + =file(' Proguard-rules.pro ')}}}dependencies {compile Filetree (dir:' Libs ',include: [' *.jar ']) Compile' com.android.support:appcompat-v7:22.2.0 '}

You can notice a big difference in structure. com.android.applicationnow instead com.android.model.application . Most properties require = operators, and += operators are used to add elements (perhaps multiple) to the collection. The names of some of the previous attributes are not very clear, and are now adjusted, for example: Now it's minSdkVersion become minSdkVersion.apiLevel

OK, let's use the Gradle file Sync project to apply these changes.

Then you just need to run it. After using more meaningful grammar rules, everything works as expected. Build with the freshly baked Gradle 2.5!

Try the NDK support

Android Studio 1.3 Hooves has announced full support for the NDK. So let's try it out with a very simple example of native code. First, you need to local.properties define an NDK directory in the project's file. Please note that the NDK r10e you can display in the Android NDK Downloads Page and the NDK Bundle shown in SDK Manager are all available.

ndk.dir=PATH_TO_NDK_ROOT

Create HelloJni.java them under your Java package.

publicclass HelloJni {    publicstringFromJNI();}

Create a jni folder in the src/main directory and create a file that reads hello-jni.c :

Hello-jni.c

#include <string.h>#include <jni.h>Jstringjava_com_inthecheesefactory_hellojni25_hellojni_stringfromjni (jnienv* env, Jobject thiz) {#if defined (__arm__)  #if defined (__arm_arch_7a__)    #if defined (__arm_neon__)      #if defined (__ARM_PCS_VFP)        #define ABI "Armeabi-v7a/neon (hard-float)"      #Else         #define ABI "Armeabi-v7a/neon"      #endif     #Else       #if defined (__ARM_PCS_VFP)        #define ABI "armeabi-v7a (hard-float)"      #Else         #define ABI "armeabi-v7a"      #endif     #endif   #Else    #define ABI "Armeabi"  #endif #elif defined (__i386__)   #define ABI "x86"#elif defined (__x86_64__)   #define ABI "x86_64"#elif defined (__MIPS64)/* mips64el-* toolchain defines __mips__ too * *   #define ABI "Mips64"#elif defined (__mips__)   #define ABI "MIPS"#elif defined (__aarch64__)   #define ABI "arm64-v8a"#Else    #define ABI "Unknown"#endif     return(*env)->newstringutf (env,"Hello from JNI!!" Compiled with ABI "Abi".");}

Please do not forget to com_inthecheesefactory_hellojni25 change the package name to the corresponding Hellojni.java, otherwise it is not working.

For those who are familiar with the NDK, you may notice that makefiles is no longer needed.

This is the final file directory:

Now, let's MainActivity.java test this JNI code in, putting the following code into the last line of the mainactivity class:

publicclass MainActivity extends AppCompatActivity {    ...    static {        System.loadLibrary("hello-jni");    }}

Modify onCreate It like this:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    Toast.makeText(MainActivity.this,                    new HelloJni().stringFromJNI(),                    Toast.LENGTH_LONG)            .show();}

Duang~ done! Now, you can use the Java code to native code, run it and try it.

With the amazing full support of Ndk,java code now and Native code on Android Studio you can work with more seamless. If you debug your code in Java, it will jump to the correct location of the native code. (Translator: This is really amazing!) )

Anyway, it's still in the experimental phase, and some of the features are still under development. For serious users, it's best to wait until it's finally released.

Conclusion

I have to say that the new Gradle Build Tools are very interesting. The main changed DSL looks very promising, and has far-reaching implications for the present. Good code should tell us what it can do (translator: can literally understand the meaning of this code), right?

Then it's still in the experimental phase. This DSL is not the final version, we'd better just learn and understand his presence, instead of switching to this new version right now. Anyway, I believe it won't be long before it releases a stable version that can be actually used. Get ready for it!

You can find more information here >>experimental Plugin User Guide

=)


Author:nuuneoi (Android GDE, CTO & CEO at the Cheese Factory)
A Full-stack developer with + than 6 years experience on Android application development and more than all years in Mobi Le application development industry. Also have skill in infrastucture, Service Side, Design, Ui&ux, Hardware, optimization, Cooking, photographing, Blogging , Training, public Speaking and does love to share things to people in the world!

List of new Android Gradle build tools: New DSL structure and Gradle 2.5

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.