在调试模式下将共享库链接到 Android Studio (v2+) 的解决方法

编程入门 行业动态 更新时间:2024-10-15 12:29:00
本文介绍了在调试模式下将共享库链接到 Android Studio (v2+) 的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

很久以前,

解决方案

为了使这个工作,两个build.gradle文件必须不能同时修改在同步一次之前.我必须按照以下步骤操作:

第一步:修改lib的build.gradle,正如凯恩所说:

//"android" 部分:defaultPublishConfig '发布'发布非默认为真产品风味{图书馆 {/* 这个奇怪的空味其实是需要的使第 3 步成功 */}}

第 2 步:清理/重建

第三步:修改应用的build.gradle,也如凯恩所说:

依赖项{调试编译项目(路径:':custom_lib',配置:libraryDebug")发布编译项目(路径:':custom_lib',配置:libraryRelease")}

第 4 步:Gradle 同步.

所以这只是先改变库,然后在修改应用程序之前清理的问题.

我检查了由 debugrelease 构建模式生成的 APK,它们中的每一个都包含 lib 的正确变体,这与应用此解决方法之前不同,因此它确实有效(感谢凯恩!).

Long ago, a Google Code ticket was opened as the Gradle plugin does not propagate whether you are doing a debug build or a release build to a dependent android library:

DESCRIPTION:
Gradle plugin does not propagate whether you are doing a debug build or a release build to a dependent android library.

VERSION INFORMATION:
gradle plugin: 0.3 gradle: 1.4

This is a problem, especially when the release configuration of the library prevents the edition of the code or the debug process to occur normally (red highlighting of native methods as the IDE does not find any longer while they link normally, broken Java step-by-step debugging...).

This was quite thoroughly discussed on Google Code and here on Stack Overflow, and it happened that Kane O'Riley proposed a really interesting (and clean) workaround:

Put this in your app: dependencies { debugCompile project(path: ':custom_lib', configuration: "libraryDebug") releaseCompile project(path: ':custom_lib', configuration: "libraryRelease") }

and in your library's build.gradle add: defaultPublishConfig 'release' publishNonDefault true productFlavors { library { } }

I gave a try, but I have the following message at Gradle sync time:

Error:Configuration with name 'libraryDebug' not found. 

I tried with just "debug" and "release" (which are the build config names I use in my lib) but the result is the same.

Any idea to make this interesting workaround working? (I'm running Android Studio 2.0 beta 2)

APPENDIX:

my lib's build.gradle file (including the ugly workaround I'm currently using):

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    // THIS IS WHERE I INSERT THE THREE STATEMENTS PERTAINING
    // TO THE LIB FROM Kane O'Riley'S WORKAROUND ABOVE

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        /*
        release {
            // UNCOMMENT BEFORE MAKING AN ACTUAL RELEASE !!! (duh...)
            ndk {
                moduleName "mylib"
                ldLibs "log"
                cFlags "-fvisibility=hidden -g0 -DSHLUBLU_ACTUAL_RELEASE -O3"
            }

            buildConfigField "boolean", "actualRelease", "true"
            debuggable false
            jniDebuggable false
            minifyEnabled false
        }
        */

        release {
            // COMMENT THIS WHOLE BLOCK BEFORE MAKING AN ACTUAL RELEASE !!!
            // (this is a copy of the "debug" config below... did I say 'duh' ?)
            ndk {
                moduleName "mylib"
                ldLibs "log"
                cFlags "-g"
            }

            buildConfigField "boolean", "actualRelease", "false"
            debuggable true
            jniDebuggable true
            minifyEnabled false
        }

        debug {
            // IF ONLY THIS ONE WAS USED WHEN DEBUGGING!
            // (Only the IDE uses that, but the release build is actually linked,    
            // see https://code.google/p/android/issues/detail?id=52962 )
            ndk {
                moduleName "mylib"
                ldLibs "log"
                cFlags "-g"
            }

            buildConfigField "boolean", "actualRelease", "false"
            debuggable true
            jniDebuggable true
            minifyEnabled false
        }
    }     
}    

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])     
}

my project's build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 27
        versionName "1.4"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        release {
            debuggable false
            jniDebuggable false
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-shlublu.txt'), 'proguard-rules.pro'
        }

        debug {
            debuggable true
            jniDebuggable true
            minifyEnabled false
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':mylib')
    // THE LINE ABOVE IS WHAT I REPLACE BY Kane O'Riley's WORKAROUND
    // DESCRIBED AT THE BEGINNING OF THIS POST
}

my Gradle config:

解决方案

For this to work, the twobuild.gradle files must not be modified at the same time before syncing once for all. I had to follow the following steps:

Step 1: modify the lib's build.gradle, exactly as Kane said:

// "android" section:
defaultPublishConfig 'release' 
publishNonDefault true 
productFlavors {
    library {
        /* This strange empty flavour is actually needed 
           for the step 3 to be successful               */
    } 
}

Step 2: clean/rebuild

Step 3: modify the app's build.gradle, also as Kane said:

dependencies {
    debugCompile project(path: ':custom_lib', configuration: "libraryDebug")
    releaseCompile project(path: ':custom_lib', configuration: "libraryRelease") 
}

Step 4: Gradle sync.

So it was just a matter of changing the library first, and then cleaning before modifying the app.

I checked the APK produced by the debug and release build modes, and each of them contains the proper variant of the lib, unlike before applying this workaround, so it does work (thanks Kane !).

这篇关于在调试模式下将共享库链接到 Android Studio (v2+) 的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-15 05:00:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/857504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:解决方法   链接   模式下   Android   Studio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!