Gradle Cheat Sheet (16): Creating a Java Library
Author: nex3z
2016-06-17
1. 建立Library
首先配置一个Java项目作为Library。创建一个Android App工程,在工程根目录/javaJokes/src/main/java/com/udacity/gradle/jokes/下建立Joker.java,内容为:
package com.udacity.gradle.jokes;
return "This is totally a funny joke";
package com.udacity.gradle.jokes;
public class Joker {
public String getJoke(){
return "This is totally a funny joke";
}
}
package com.udacity.gradle.jokes;
public class Joker {
public String getJoke(){
return "This is totally a funny joke";
}
}
在/javaJokes/src/test/java/com/udacity/gradle/jokes/test/下建立JokerTest.java,内容为:
package com.udacity.gradle.jokes.test;
import com.udacity.gradle.jokes.Joker;
Joker joker = new Joker();
assert joker.getJoke().length() != 0;
package com.udacity.gradle.jokes.test;
import com.udacity.gradle.jokes.Joker;
import org.junit.Test;
public class JokerTest {
@Test
public void test() {
Joker joker = new Joker();
assert joker.getJoke().length() != 0;
}
}
package com.udacity.gradle.jokes.test;
import com.udacity.gradle.jokes.Joker;
import org.junit.Test;
public class JokerTest {
@Test
public void test() {
Joker joker = new Joker();
assert joker.getJoke().length() != 0;
}
}
在/javaJokes/下建立build.gradle,内容为:
testCompile 'junit:junit:4.11'
sourceCompatibility = 1.7
apply plugin:"java"
repositories {
jcenter()
}
dependencies {
testCompile 'junit:junit:4.11'
}
sourceCompatibility = 1.7
apply plugin:"java"
repositories {
jcenter()
}
dependencies {
testCompile 'junit:junit:4.11'
}
sourceCompatibility = 1.7
这里添加了JUnit作为依赖,并配置sourceCompatibility 为1.7,使用Java 7编译。此时目录结构为如图1所示。

图1
运行:
$ ./gradlew test
确认无误。
2. 加入依赖
在/app/build.gradle加入刚才的Library作为依赖,在dependencies {} 块中加入compile project(‘:javaJokes’) :
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.3.0'
compile project(':javaJokes')
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.3.0'
compile project(':javaJokes')
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.3.0'
compile project(':javaJokes')
}
Java插件会定义一个名为default 的Configuration,其中包含了项目的输出(jar文件),这里加入对project(‘:javaJokes’) 的依赖,实际上是加入了对javaJokes的default Configuration的依赖,也就加入了对javaJokes输出的jar的依赖。
然后就可以在代码中使用javaJokes,如;
import com.udacity.gradle.jokes.Joker;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.textView);
Joker myJoker = new Joker();
textView.setText(myJoker.getJoke());
import com.udacity.gradle.jokes.Joker;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.textView);
Joker myJoker = new Joker();
textView.setText(myJoker.getJoke());
return rootView;
}
import com.udacity.gradle.jokes.Joker;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.textView);
Joker myJoker = new Joker();
textView.setText(myJoker.getJoke());
return rootView;
}
在Gradle控制台的输出中可以看到javaJokes被加入了编译:
:app:preDebugBuild UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:preReleaseBuild UP-TO-DATE
:javaJokes:processResources UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72210Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42210Library UP-TO-DATE
:app:prepareDebugDependencies
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:javaJokes:compileJava
:javaJokes:processResources UP-TO-DATE
:javaJokes:classes
:javaJokes:jar
:app:prepareComAndroidSupportAppcompatV72210Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42210Library UP-TO-DATE
:app:prepareDebugDependencies
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:javaJokes:compileJava
:javaJokes:processResources UP-TO-DATE
:javaJokes:classes
:javaJokes:jar
:app:prepareComAndroidSupportAppcompatV72210Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42210Library UP-TO-DATE
:app:prepareDebugDependencies
除了像上面那样手动操作,也可以使用File > New > New Module > Java Library,通过向导加入Java Library。
本部分的完整代码可以在这里找到。