...menustart
...menuend
- download gradle 2.3
- config
export GRADLE_HOME=/<installation location>/gradle-2.3
export PATH=${PATH}:$GRADLE_HOME/bin
gradle 经常会被墙,所以需要设置代理
vim ~/.gradle/gradle.properties
systemProp.http.proxyHost=代理地址
systemProp.http.proxyPort=端口
#systemProp.http.nonProxyHosts=不用代理的Hosts
systemProp.https.proxyHost=代理地址
systemProp.https.proxyPort=端口
#systemProp.https.nonProxyHosts=不用代理的Hosts
注意: 行末不要留空格!!
gradle init --type java-library
ext.property_name = ...
使用的时候,不需要 ext.
, 直接 property_name
task buildAPI(type:Exec) {
commandLine "mv" , "-f" , _jarFile , _cpDestPath
}
jarFiles.dependsOn build
buildAPI.dependsOn jarFiles
"${project.buildDir}/..."
在下面的目录下面创建gradle.properties文件:
/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle (Windows)
org.gradle.parallel=true
org.gradle.daemon=true
添加一个 task buildAPI
, 在 原有 build
任务的基础上, 对某个目录的 java classes 进行 jar 打包,并拷贝到指定目录。
ext._path = "${project.buildDir}/intermediates/classes/release/"
ext._cpDestPath = ...
ext._jarFile = "API_MediaDecode.jar"
task jarFiles(type:Exec) {
commandLine 'jar' , "-cvf" , _jarFile , "-C" , _path , "."
}
task buildAPI(type:Exec) {
commandLine "mv" , "-f" , _jarFile , _cpDestPath
}
jarFiles.dependsOn build
buildAPI.dependsOn jarFiles
...
apply plugin: 'android'
dependencies {
// add local jar
compile fileTree(dir: '../../MobileAuth/libs', include: '*.jar')
}
android {
只需要要构建脚本中添加以下代码:
apply plugin: 'java'
- 默认情况下,Gradle会在src/main/java中查找你的源码,在src/test/java中查找你的测试代码
- src/main/resources下的文件都会被打包
- src/test/resources下的文件会被包含在classpath中用于测试
- 所有输出的文件都保存在build目录里,
- 生成的jar包则是在build/libs里面
- gradle tasks命令查看任务。
- gradle build时,Gradle会执行编译,测试,并且将源文件和资源文件打成jar包。
- clean:删除build目录和其他构建时生成的文件
- assemble:编译并打包,但不执行单元测试。
- check:编译并测试代码。
通常一个项目会有许多外部依赖,我们需要在构建脚本中告诉Gradle在哪里可以找到这些依赖。比如如果使用maven中央仓库的话,我们可以通过以下代码来添加仓库:
repositories {
mavenCentral()
}
然后再通过下面代码添加依赖:
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
上面的代码中,声明了在编译期,需要依赖 commons-collections,在测试期需要依赖 junit。
前面提到,Java插件为项目定义了许多默认配置,如果我们需要,这些配置都是可以由我们自己来定义的。如下面例子,指定项目版本号和JDK版本号,并且添加一些属性到JAR包的manifest文件中:
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
}
修改test任务来添加系统属性:
test {
systemProperties 'property': 'value'
}
以下代码是发布JAR包到本地中。发布到maven仓库或jcenter仓库以后再讨论。
project.version=1.0
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
执行gradle uploadArchives即可发布该JAR包。
uploadArchives 需要指定 project.version , 如果没有,最后的 jar 会以 "-unspecified" 结尾。
或者在 settings.gradle 文件中指定 rootProject.name
apply plugin: 'eclipse'
执行 gradle eclipse
会生成eclipse项目文件
android create project -a MainActivity -k package_path_com.example.app -t android-16 -g -v 1.1.3 -p projectName
android {
...
compileOptions {
sourceCompatibility = 'VERSION_1_6'
targetCompatibility = 'VERSION_1_6'
}
...
}
android {
....
productFlavors {
flavor1 {
...
}
flavor2 {
...
}
}
}
- A product flavor defines a customized version of the application build by the project
- It will generate another app for each flavor
To enable proguard:
android {
buildTypes {
release {
minifyEnabled true
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
productFlavors {
flavor1 {
proguardFile 'some-other-rules.txt'
}
}
}
- The ProGuard plugin is applied automatically by the Android plugin
- proguard task are created automatically if the Build Type sets minifyEnabled property true
- There are 2 default rules files:
- proguard-android.txt
- proguard-android-optimize.txt
- You can use own proguard rules, or use more 1 proguard rules through productFlavors
android {
sourceSets {
main {
manifest.srcFile 'ToDoList/src/main/AndroidManifest.xml'
java.srcDirs 'src'
res.srcDirs 'res'
}
}
}
res.srcDirs 'res'
指定 资源路径 ( src/main/res )
其他的类似可用设置:
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src/main/res']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res/main/res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
buildTypes {
release {
minifyEnabled true
proguardFile 'proguard-project.txt'
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
tasks.withType(JavaCompile) { options.encoding = "UTF-8" }
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
}
lintOptions {
abortOnError false
}
signingConfigs {
myConfig{
storeFile file("releasesig.keystore")
storePassword ""
keyAlias "release"
keyPassword ""
}
}
buildTypes{
release {
signingConfig signingConfigs.myConfig
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
}
}
another example :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 'android-23'
buildToolsVersion '23.0.2'
sourceSets {
main {
manifest.srcFile './AndroidManifest.xml'
java.srcDirs 'src'
res.srcDirs 'res'
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
}
API example:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
}
}
apply plugin: 'android'
android {
compileSdkVersion 'android-16'
buildToolsVersion '23.0.2'
compileOptions {
sourceCompatibility = 'VERSION_1_6'
targetCompatibility = 'VERSION_1_6'
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs 'src'
res.srcDirs 'res'
}
}
buildTypes {
release {
minifyEnabled true
proguardFile 'proguard-project.txt'
}
}
}
ext._proguardedFilePath = "${project.buildDir}/intermediates/classes-proguard/release/classes.jar"
ext._cpDestPath = "/Users/qibinyi/WORK/WORK/TestVideo/Assets/Plugin/Android/API_MediaDecode.jar"
task buildAPI(type:Exec) {
commandLine "mv" , "-f" , _proguardedFilePath , _cpDestPath
}
buildAPI.dependsOn build
-
use
android create
command to create a android gradle project -
add
ndk.dir=<path-to-ndk>
to local.properties file -
in build.gradle file inside of the defaultConfig closure , add ..
buildToolsVersion ... defaultConfig { // applicationId "com.apixel.luaTest" ndk { moduleName "hello-world" } } sourceSets.main { jni.srcDirs = ['jni'] }
-
create a folder
jni
, In that folder, create a file called hello-world.c -
in java Activity class, call the method in hello-world.c
// hello-world.c
#include <string.h>
#include <jni.h>
jstring
Java_com_apixel_luaTest_Main_stringFromJNI(JNIEnv* env, jobject thiz) {
return (*env)->NewStringUTF(env, "Hello world from JNI!");
}
// Main.java
public class Main extends Activity
{
static {
System.loadLibrary("hello-world");
}
public native String stringFromJNI();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String testString = stringFromJNI();
System.out.println( testString );
}
}
-
native 方法如果定义在 cpp 文件中, 需要 加上 extern "C"
-
link library :
ndk { ... ldLibs "log" }
-
这个例子中,我们演示了 java 调用 native 方法, c++ 调用 java的情况会更复杂
-
有时候,我们需要获取 JAVA VM
extern "C" {
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void *reserved)
JniHelper::setJavaVM(vm);
return JNI_VERSION_1_4;
}
}
- PS: JNI_OnLoad 方法 必须于工程的 jni 目录中定义 , 放在static library 中无效
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
}
}
apply plugin: 'android'
android {
compileSdkVersion 'android-16'
buildToolsVersion '24.0.0'
defaultConfig {
minSdkVersion 11
// applicationId "com.apixel.luaTest"
ndk {
moduleName "hello-world"
ldLibs "log" , "android" , \
"${project.rootDir}/../../luajit/prebuilt/android/\${TARGET_ARCH_ABI}/libluajit.a" , \
"${project.rootDir}/../../tolua/prebuilt/android/\${TARGET_ARCH_ABI}/libtolua.so"
cFlags "-O2 -I${project.rootDir}/../../luajit/include -I${project.rootDir}/../../tolua/include \
-I${project.rootDir}/../../luabinding/luabinding"
abiFilters "armeabi", "armeabi-v7a", "x86"// , "arm64-v8a"
stl "gnustl_static"
}
}
signingConfigs {
release {
storeFile file("${project.rootDir}/../../../androidKeystore/test.keystore")
storePassword "Guardiola7"
keyAlias "test.keystore"
keyPassword "Guardiola7"
}
}
sourceSets.main {
jni.srcDirs = ['jni','../common_src']
java.srcDirs += [ '../../tolua/src_misc/android/java' ]
assets.srcDirs += [ '../common_res' ]
jniLibs.srcDirs = [ '../../tolua/prebuilt/android' ]
}
buildTypes {
release {
minifyEnabled false
proguardFile getDefaultProguardFile('proguard-android.txt')
signingConfig signingConfigs.release
}
}
}
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'android'
android {
compileSdkVersion 26
defaultConfig {
applicationId "you-app-package-id"
minSdkVersion 21
targetSdkVersion 26
ndk {
abiFilters 'armeabi'
}
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
// implementation 'com.google.code.gson:gson:2.2.1'
implementation fileTree(include: ['*.jar'], dir: 'libs')
}
- solution:
- change
classpath 'com.android.tools.build:gradle:1.3.0'
toclasspath 'com.android.tools.build:gradle:2.0.0'
- change
- in
build.gralde
, Instead of "runProguard false" use "minifyEnabled false"
adb logcat -s TAG1*:I TAG2:*