【问题标题】:Android Studio 2.3.3 using gradle component model was creating .aar automagically; Android Studio 3.4.1 now using CMake or NDK-Build does not使用 gradle 组件模型的 Android Studio 2.3.3 自动创建 .aar; Android Studio 3.4.1 现在使用 CMake 或 NDK-Build 不
【发布时间】:2019-07-02 00:33:05
【问题描述】:

我的 wavvoxlibrary/build.gradle 不仅服务于主程序 (app/build.gradle),而且还创建共享库 .aar。 这与 Android Studio 2.3.3 和 - 对于库 - gradle-experimental 0.9.3 配合得很好。然而,gradle 组件模型不支持 abifilters,比如在 64 位中构建 JNI / c/c++ 程序。

所以我升级到了 Android Studio 3.4.1,我的 wavvoxlibrary/build.gradle 离开了上述组件模型,并获得了适当的 CMakeList.txt 文件来列出 c/c++ 程序和任何曾经是一部分的编译器选项较旧的 build.gradle。我还在 Application.mk 和 Android.mk 制作文件中添加了 NDK-Build 选项。

到目前为止一切顺利。我的应用程序已正确构建并运行 - 包括 armabi-v7a(32 位)和 arm64-v8a(64 位)的共享库。

缺少什么:要创建 .aar 文件,我必须显式调用 gradle 任务 wavvoxlibrary .. assemble。

在构建我的签名 apk 期间,有什么方法可以生成 .aar 吗?

我已经删除了 gradle 组件模型,以便从 gradle:2.3.3 和 gradle-experimental:0.9.3 使用“apply plugin: 'com.android.model.library'” 使用“apply plugin: 'com.android.library'” 到 gradle:3.4.1 并调用 cmake(或者,可选地 ndkBuild)来列出 c/c++ 文件和编译器选项。

详情见我的博客:http://jurgenmenge.com/blog/computer/migrate-a-library-in-android-studio/

import java.text.SimpleDateFormat

// migrate from Android Studio with gradle:2.3.3 + gradle-experimental:0.9.3
//           to Android Studio with gradle:3.4.1 + Cmake + ndkBuild
// jm*20190614

apply plugin: 'com.android.library'
//apply plugin: 'com.android.model.library'

/**
 * Each subversion code should be exactly 2 numeric digits or we will get wrong versions published
 * in the Google Play Store; buildType to switch between "cmake" and "ndkBuild"
 */
ext.majorVersion = "03"
ext.minorVersion = "08"
ext.patchVersion = "03"

static String buildKind() {    // neat trick I created  :-)   // jm*20190614
    //return "gradle"  // model - experimental
    //return "cmake"
    return "ndkBuild"
}

/**
 * The name of the app version, using the Semantic Versioning format (major.minor.patch)
 * @return
 */
String appVersionName() {
    String appVersion = removeLeadingZeros(ext.majorVersion) + "." \
                      + removeLeadingZeros(ext.minorVersion) + "." \
                      + ext.patchVersion

    System.out.println("  ************************************************")
    System.out.println("  *** WavvoxLibrary  version:  " + appVersion + " ")
    System.out.println("  ***                buildKind " + buildKind() )
    System.out.println("  ************************************************")
    return appVersion
}

//model {
    android {
        compileSdkVersion 28
        buildToolsVersion "28.0.3"  // "25.0.3"

        defaultConfig {
            minSdkVersion 16       /*** was: 8 ***/
            targetSdkVersion 28
            //
            versionName appVersionName() + "_" + buildKind()
            setProperty("archivesBaseName", "wavvoxlibrary-$versionName")
            buildConfigField "String", "VERSION_NAME_WAVVOX_FORMAT", versionNameWavvoxFormat()

            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        //}   // defaultConfig

        ndk {
            moduleName "wavvox-decoder"
            ldLibs "log", "android"
            // these platforms cover 99% percent of all Android devices
            abiFilters "arm64-v8a", "armeabi-v7a"
                   //, "x86", "x86_64"
        }   // ndk

        externalNativeBuild {
            switch (buildKind()) {
                case "ndkBuild":
                    ndkBuild {
                        cFlags "-std=c99", "-O3", "-Ofast", "-mfpu=neon"            // jm*20190616
                            // "-Wno-error=format-security", "-mfloat-abi=softfp:, "-g"
                        abiFilters "armeabi-v7a", "arm64-v8a"
                    }
                    break
                case "cmake":
                    cmake {
                        cFlags "-std=c99", "-O3", "-Ofast", "-mfpu=neon"            // jm*20190616
                            // "-Wno-error=format-security", "-mfloat-abi=softfp:, "-g"
                        abiFilters 'armeabi-v7a', 'arm64-v8a'
                    }
                    break
                default:
                    println "**** unknown buildType value: " + buildType() + " ****"
            }
        }   // externalNativeBuild

        }   // defaultConfig

        buildTypes {
            release {
                minifyEnabled true
                proguardFiles "proguard-rules.pro"
            }
        }   // buildTypes
        // }   // android   // jm*20190405

        // android.ndk {   // jm*20190405

        // C source files to include in the build script
        // android.sources.main.jni {   // jm*20190405

  /*    if (buildKind() == "gradle")    // using "model"  in gradle_experimental
        sourceSets {
            main {
                jniLibs.srcDirs = ['src/main/jni']    // for pre-built .so files

                jni {
                    //source {
                        include "wavvoxNativeApp.c"
                        // ... and all the other c/c++ programs

                        srcDir "jni"
                    //}
                }   // jni
            }   // main
        }   // source
*/
        externalNativeBuild {
            switch (buildKind()) {
                case "ndkBuild":
                    ndkBuild {
                        path 'src/main/jni/Android.mk'
                    }
                    break
                case "cmake":
                    cmake {
                        version '3.10.2'
                        path 'src/main/jni/CMakeLists.txt'
                    }
                    break
                default:
                    println "**** unknown buildType value: " + buildType() + " ****"
            }
        }   // externalNativeBuild

    }   // android   // jm*20190405
//}   // model

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    implementation 'com.android.support:support-annotations:28.0.0'

    testImplementation 'junit:junit:4.12'
    testImplementation 'org.hamcrest:hamcrest-junit:2.0.0.0'
}

/**
 * Workaround for using ProGuard with the experimental Gradle plugin.
 *
 * Trouble seems to be that unless process(Debug|Release)Resources task is needed, it is not created
 * and VariantOutputScope.setProcessResourcesTask in TaskManager.createProcessResTask() is not
 * called so when TaskManager.applyProguardConfig() calls
 * BaseVariantOutputData.processResouresTask.getProguardOutputFile() it does so on a null object.
 * So, by making transformClassesAndResourcesWithProguardFor(Debug|Release) depend on
 * process(Debug|Release)Resources I force the task to be created
 *
 * More discussions here: https://issuetracker.google.com/issues/37079003
 */

tasks.all { task ->
    def match = task.name =~ /^transformClassesAndResourcesWithProguardFor(.*)$/
    if (match) {
        task.dependsOn "process${match.group(1)}Resources"
        return
    }
}

// ToDo:  get automatic build of .aar libraries working;
//        currently, double-click on Gradle "wavvoxlibrary / Tasks / build / assemble"

}

我想在构建签名的应用程序 (.apk) 时自动创建 .aar 文件,而不需要额外的步骤来运行 gradle task wavvoxlibrary assemble。

我错过了什么?

谢谢,jm。

【问题讨论】:

    标签: android-studio android-gradle-plugin shared-libraries build.gradle


    【解决方案1】:

    我想你可能已经尝试过这个,但仍然添加这个作为答案,如果它没有帮助,将删除这个答案。

    将你的库模块添加到你的 settings.gradle 文件中。

    include ':app', ':my-library-module'
    

    https://developer.android.com/studio/projects/android-library#AddDependency

    【讨论】:

    • 谢谢;但情况已经如此——否则应用程序将无法从所需的共享库中获取 .so 文件。 ;-) 它是 .aar 的 自动 构建(除了构建 .apk)在 Android Studio 2.3.3 环境中有效,但在 Android Studio 3.2、3.3、3.4 中无效;这里我需要专门运行 gradle 任务 wavvoxlibrary ... assemble。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多