Android: 使用gradle构建android app

gralde是什么

Gradle is an open-source build automation tool focused on flexibility and performance. 
Gradle build scripts are written using a Groovy or Kotlin DSL.

是类似make的build工具,但编译脚本是用Groovy类java语音写的

基于gradle 构建Android工程:


Android工程一般结构如下:

AndroidApp
    |--build.gradle
    |--gradle.properties
    |--local.properties
    |--setting.gradle
    |
    |__app
       |--build.gradle
       |--src
       |--libs

根目录下的build.gradle是


Top-level build file where you can add configuration options common to all sub-projects/modules.
其中 classpath中的gradle版本号可能更改?
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
    }

gradle.properties:


# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
org.gradle.jvmargs=-Xmx1536m

local.propertis


## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!

# Location of the SDK. This is only used by Gradle.
#Thu Nov 30 16:55:46 CST 2017
sdk.dir=/home/xxx/Android/Sdk

settings.gradle


通过该文件向top-project添加sub-project:
include ':app', 就是上面的子目录app
也可创建多个sub-project
include ':app', ':baselib', ':qualcomm', ':nuance'

每个子目录下的build.gradle定义了编译使用的SDk版本/applicationId
依赖关系等
 

控制源码编译的build.gradle

android {
    compileSdkVersion 27
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "io.github.introml.activityrecognition"
        minSdkVersion 26
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

首先区分下两个版本:Android SDK Tools verison/ Android Platform Version

Android Platform version指的是API Level

Android SDK Tools verison指的是buildToolsVersion "26.0.1"改成“26.1.1”是否也可以?

可以通过Android Studio升级 SDK Build Tools (按提示点击)

jniLibs.srcDirs = ['libs']

指示动态库的路径

System.loadLibrary("tensorflow_inference");
https://pqpo.me/2017/05/31/system-loadlibrary/
深入理解 System.loadLibrary

libcore/ojluni/src/main/java/java/lang/System.java
    /**
     * Maps a library name into a platform-specific string representing
     * a native library.
     *
     * @param      libname the name of the library.
     * @return     a platform-dependent native library name.
     * @exception  NullPointerException if <code>libname</code> is
     *             <code>null</code>
     * @see        java.lang.System#loadLibrary(java.lang.String)
     * @see        java.lang.ClassLoader#findLibrary(java.lang.String)
     * @since      1.2
     */
    public static native String mapLibraryName(String libname);

主要传入的参数是tensorflow_inference实际上对应的动态库文件是libtensorflow_inference.so,下面的函数进行的名字处理

/data/app/io.github.introml.activityrecognition-lZQ-W6blE-lddohnWyOb6Q==/lib/arm # ls -al
-rwxr-xr-x 1 system system 9603116 1979-11-30 00:00 libtensorflow_inference.so


libcore/ojluni/src/main/native/System.c
#define JNI_LIB_PREFIX "lib"
#define JNI_LIB_SUFFIX ".so"
JNIEXPORT jstring JNICALL
System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)
{
    int len;
    int prefix_len = (int) strlen(JNI_LIB_PREFIX);
    int suffix_len = (int) strlen(JNI_LIB_SUFFIX);

    jchar chars[256];

    cpchars(chars, JNI_LIB_PREFIX, prefix_len);
    (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len);
    len += prefix_len;
    cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len);
    len += suffix_len;

    return (*env)->NewString(env, chars, len);
}

注释掉jniLibs.srcDirs = ['libs']后的错误

app/build.gradel:如果没有下面jniLibs.srcDirs指示的路径,则该路径下面的
*.so文件编译到app中,当程序运行到loadLibrary会出现找不到文件的错误
    sourceSets {
        main {
            //jniLibs.srcDirs = ['libs']
        }

而库的依赖关系:本地依赖的库compile fileTree
compile fileTree(dir: 'libs', include: ['*.jar'])
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}
io/github/introml/activityrecognition/TensorFlowClassifier.java    
错误: 程序包org.tensorflow.contrib.android不存在    
错误: 找不到符号
符号:   类 TensorFlowInferenceInterface

   

第三方依赖的库使用compile


compile 'com.android.support:appcompat-v7:25.3.1'

如果手机上对应的API Level小于生成apk时使用API level 则安装错误

Failure [INSTALL_FAILED_OLDER_SDK: Failed parse during installPackageLI:

猜你喜欢

转载自blog.csdn.net/u011279649/article/details/81708094