Gradle编译java,Eclipse项目以及遇到的问题

来源:互联网 发布:linux pe结构 编辑:程序博客网 时间:2024/04/28 08:22
使用Gradle创建一个简单java项目
步骤:
1、 首先创建一个空的文件夹(名字随便起),我起名加gradlework
2、 进入空文件夹,在命令行使用gradle init 初始化gradle结构
3、 打开编辑build.gradle文件,添加java插件
并添加eclipse插件,然后添加创建java文件目录

代码如下

apply plugin: 'java'apply plugin: 'eclipse' //申明java主类jar {    manifest {        attributes 'Main-Class': 'net.petrikainulainen.gradle.HelloWorld'    }}//创建目录task createJavaProject << {    sourceSets*.java.srcDirs*.each{ it.mkdirs() }    sourceSets*.resources.srcDirs*.each{ it.mkdirs()}}



4、 然后执行gradle build 或者gradle assemble
5、 执行 gradle createJavaProject ecplise 创建java项目的src文件目录
6、 在src\main\java 目录下添加java代码文件 文件名为HelloWorld.java
public class HelloWorld {     public static void main(String[] args) {        System.out.println("Hello World!");    }}



在执行gradle build ,然后切换到build/libs文件夹下边执行.jar文件
命令 java -jar "替换为jar包的名称"
就能看到执行结果
输出Hello world!




使用Gradle编译Eclipse编写的Android项目:

编译Eclipse开发的android项目跟java项目一样十分简单,如果只是项目本身的话,那么只要在项目的跟目录下创建俩个文件就可以了;
步骤:
1、首先要保证Gradle的环境配置没有问题,然后要确保现在使用的Gradle不是3.3及以上,因为我测试过程中3.3编译不过去
2、在项目根目录下创建文件build.gradle 用来编写编译代码
代码:
buildscript {    repositories {        jcenter()mavenCentral()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.1.0'    }}apply plugin: 'com.android.application'android {compileSdkVersion 19    buildToolsVersion "22.0.1"    sourceSets {        main{            manifest.srcFile 'AndroidManifest.xml'            java.srcDirs = ['src']            aidl.srcDirs = ['src']            renderscript.srcDirs = ['src']            res.srcDirs = ['res']            assets.srcDirs = ['assets']            jniLibs.srcDirs = ['libs']        }      // Move the tests to tests/java, tests/res, etc...          instrumentTest.setRoot('tests')            // Move the build types to build-types/<type>          // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...          // This moves them out of them default location under src/<type>/... which would          // conflict with src/ being used by the main source set.          // Adding new build types or product flavors should be accompanied          // by a similar customization.          debug.setRoot('build-types/debug')          release.setRoot('build-types/release')      }    defaultConfig {        applicationId "com.oppo.examples"        minSdkVersion 14        targetSdkVersion 23        versionCode 1        versionName "1.0"    }    buildTypes {        release {            //是否开启混淆            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }    compileOptions{        sourceCompatibility org.gradle.api.JavaVersion.VERSION_1_7        targetCompatibility org.gradle.api.JavaVersion.VERSION_1_7    }    //移除lint检查的错误error    lintOptions{        abortOnError false    }}dependencies {  compile fileTree(dir:'libs', include:['*.jar'])}



直接复制,然后按你的Gradle修改classpath 'com.android.tools.build:gradle:2.1.0'   为你的版本。
还可以修改sdk的版本,jdk版本等
  4、在根目录下创建local.properties,用来指定sdk的路径,如果是ndk的话也在这配置

## This file is automatically generated by Android Studio.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!## This file must *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.#Tue Jun 14 12:00:41 CST 2016sdk.dir=D\:\\tools\\android_sdk


5、然后在根目录下打开命令行,直接输入 gradle build等着就好了,等完成以后就可以看到项目里边多了好多文件夹,然后打开\build\outputs\apk
就可以看到编译好的apk。
如果还有依赖的库的话那么就多创建一个settings.gradle   里边就项目所包含的类库
include ':Example',':libraries:lib_shareSDK'    project(':libraries:lib_shareSDK').projectDir = new File('E:/workfile/AndroidWorkspace/lib_shareSDK') 




使用中出现的问题:

1、使用中出现:
Error:Unable to start the daemon process.    This problem might be caused by incorrect configuration of the daemon.    For example, an unrecognized jvm option is used.    Please refer to the user guide chapter on the daemon at http://gradle.org/docs/2.2.1/userguide/gradle_daemon.html    Please read the following process output to find out more:    -----------------------    Error occurred during initialization of VM    Could not reserve enough space for object heap    Error: Could not create the Java Virtual Machine.    Error: A fatal exception has occurred. Program will exit.    


出现这个问题有可能是分配的内存有点小,但是单纯的使用Gradle的时候可以通过在文件夹下添加gradle.properties
在文件中添加如下配置信息:

org.gradle.jvmargs=-Xmx512m    


如果没有用那么就要检查下是不是jdk的问题,必须使用jdk 1.7或者以上的才行,而且在第一次使用的时候jdk可能防火墙会提示是否允许,一定不能拒绝,否则也会出现这个问题。
2、 错误:找不到或无法加载主类xxxx
问题是在build.gradle中没有指定主类,或者指定的主类与代码中的包名不对,必须要保持一致。
3、
org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection



该问题是因为最新版本出现的问题,还没有发现解决办法,除非使用低版本,(最新3.3,使用2.xxx的就可以了)但不是最好的解决办法。


4、更新Gradle的版本,更新版本可以随便找一个项目,打开它的目录gradle/wrapper  修改


gradle-wrapper.properties文件中最后一行的版本信息为你要更新的版本信息。
然后在项目根目录下打开命令行,执行gradlew 类型的任务都会进行下载,下载以后会自动解压。如果嫌下载太慢可以在网上下载好,然后
复制到相应的目录下边。
我的地址在


C:\Users\xxxxxx\.gradle\wrapper\dists\gradle-3.3-all\55gk2rcmfc6p2dg9u9ohc3hw9  下,最后这个文件夹是gradle自己创建的,放到这个里边然后在执行
gradlew相应的命令就可以自动解压了。
5、出现以下错误:
Error:(50, 0) Error: NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin. For details, see http://tools.android.com/tech-docs/new-build-system/gradle-experimental. Set "Android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration.
解决办法就是在gradle.properties文件中添加  android.useDeprecatedNdk=true,如果没有这个文件就在跟目录下创建并添加。就可以正常编译了。
使用Gradle 来创建一个简单的java项目。
步骤:
1、 首先创建一个空的文件夹(名字随便起),我起名加gradlework
2、 进入空文件夹,在命令行使用gradle init 初始化gradle结构
3、 打开编辑build.gradle文件,添加java插件
并添加eclipse插件,然后添加创建java文件目录
代码如下

4、 然后执行gradle build 或者gradle assemble
5、 执行 gradle createJavaProject ecplise 创建java项目的src文件目录
6、 在src\main\java 目录下添加java代码文件 文件名为HelloWorld.java
在执行gradle build ,然后切换到build/libs文件夹下边执行.jar文件
命令 java -jar "替换为jar包的名称"
就能看到执行结果
0 0