Disclaimer: This article is entirely from this article, thanks to the help of the great God. This article is purely for backup.
This article is about how to use Android Sudio to import open source projects with the hottest materialdesign library on GitHub, and if you've just been transferred from eclipse to as, like me, this post is perfect for you.
If you do not introduce any third-party libraries, do not do automated sub-channel packaging, and so on, it can be completely do not understand gradle in the case of the development of Android projects. But if you want to import hot items on GitHub, you must first familiarize yourself with Gradle.
1. Gradle
Gradle is a groovy-based automated building tool for Java applications, based on DSL syntax. is a new tool that Google introduced to replace Ant and Maven, and it relies on maven and ivy compatibility.
Simply put, Gradle is an automated engineering tool introduced by Andriod Studio that solves the build of Android projects. May be able to solve the following development process pain points:
- don't download the third-party library . Before using eclipse, you had to download the third-party library and then introduce the third-party libraries into your project. But with Gradle, a few lines of code take care of third-party libraries, reducing coupling.
- instead of manually modifying the N-times code, the code is sent n packets to n markets . Before you might need to send different apk packages for different channels, you can now generate multiple channel packages at once with just a few lines of code in Gradle. Refer to this article for details: How to implement a set of code to develop different features of the APK through Gradle.
If you create a new project with AS, the default project structure for this project, the file opened in the diagram is Testme/build.gradle, and the meaning of each sentence has been given.
The new project named Testme in the diagram uses the directory structure of Project. The following gradle files are visible under this directory, respectively:
- Testme/build.gradle, Global Gradle, the settings in this file will be common throughout the project.
- Testme/app/build.gradle, the Gradle of the module, is only common in the app module.
- Testme/settings.gradle, which specifies which directory is the module model. The contents of the content is by default
include ‘:app‘
, which means that the app folder is a module.
The contents of the Testme/app/build.gradle file are explained in detail in the notes in the illustration.
2. Import Materialdesignlibrary Project
Our goal is to successfully run the Materialdesignlibrary project in as and run it successfully on the phone.
2.1 First Step
First, download the materialdesignlibrary to local using the git clone command.
2.2 Step Two
Go to As,file->import Project, and then locate the Materialdesignlibrary directory, and double-click Build.gradle in the directory to import the project.
2.3 Step Three
You can see the Materialdesignlibrary project on the imported GitHub contains two module, including Materialdesigndemo and a materialdesign.
- Materialdesign's Build.gradle file first behavior:
apply plugin: ‘com.android.library‘
, indicates that this is a library.
- Materialdesigndemo Build.gradle File The first behavior:
apply plugin: ‘com.android.application‘
, indicates that this is an application.
His project uses a reference to the local materialdesign library, and the third part of this article is to refer to the Materialdesign Library of the online Center library. Both methods are possible, but the latter is more convenient and more common, and the first method is less common. After the second step, many errors can occur, usually due to configuration problems, which are described in the following common error section of this article.
Note: Be sure to ensure that the root directory is inside the Setting.gradle include ‘:MaterialDesign‘, ‘:MaterialDesignDemo‘
. Indicates that all two of these folders are module.
3. Refer to Materialdesignlibrary in your own project
Or the previous Testme project, only need to add in the Testme/app/build.gradle dependencies compile ‘com.github.navasmdc:MaterialDesign:[email protected]‘
, as follows:
1234567 |
dependencies { // 编译libs目录下的所有jar包 compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) //多个文件 //compile files(‘libs/foo.jar‘) //单个文件这样写 compile ‘com.android.support:appcompat-v7:22.0.0‘ //编译第三方库 compile ‘com.github.navasmdc:MaterialDesign:[email protected]‘}
|
4. Common errors
As in importing open source projects or third-party libraries, there are often errors as follows. In fact, a lot of the tools are the version of the problem.
4.1
error:failed to find Build Tools revision 21.1.1
Install Build Tools 21.1.1 and Sync Project
It is because the version of build tools that the open source library corresponds to is not in your as. The workaround is to click on his link to download it. or change the build tolls version in the Build.gradle file in each model to the native buildToolsVersion "21.1.1"
.
4.2
error:failed to find target android-18:/users/xujin/develop/adt-bundle-mac-x86_64-20140702/sdk
Install missing platform (s) and Sync project
It is because the open source library corresponds to the version of the Android SDK that is not in your machine. Workaround, either download or change the configuration. Change the version of the SDK in the Build.gradle file inside each model compileSdkVersion 21
.
4.3
Error: (44, 30) errors: The diamond operator is not supported in-source 1.6 (use-source 7 or later to enable the diamond operator)
The reason for the error is List<Car> cars = new ArrayList<>();
that Java introduced the diamond operator in 1.7 in the code, and <>
there are two workarounds. One is to change the version of the compiled Java SDK, instead of changing the error code toList<Car> cars = new ArrayList<Car>();
4.4
Error: (PNS) No resource Identifier found for attribute ' checked ' on package ' com.gc.materialdesign '
Because the 37th line materialdesign:checked="true"
, the namespace materialdesign there is no checked attribute, changed to check, the code is as follows. (I've met before, let's talk about it.) )
12345678 |
<com.gc.materialdesign.views.CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#1E88E5" materialdesign:check="true" />
|
Resources
How to import open source projects with Android Studio and solutions for common mistakes