使用Android Studio(v2+;)在调试模式下链接共享库的变通方法

使用Android Studio(v2+;)在调试模式下链接共享库的变通方法,android,android-studio,Android,Android Studio,很久以前,由于Gradle插件不会将调试版本或发布版本传播到依赖的android库而打开: 说明: Gradle插件不会传播您是否正在执行 调试版本或发布版本到依赖的android库 版本信息: 渐变插件:0.3渐变:1.4 这是一个问题,尤其是当库的版本配置阻止代码的编辑或调试过程正常进行时(本机方法的红色高亮显示,因为IDE在正常链接时不再找到它们,Java逐步调试中断…) 这在Google代码和这里的堆栈溢出上进行了彻底的讨论,碰巧提出: 将此内容放入你的应用程序: 依赖关系{ debu

很久以前,由于Gradle插件不会将调试版本或发布版本传播到依赖的android库而打开:

说明:
Gradle插件不会传播您是否正在执行 调试版本或发布版本到依赖的android库

版本信息:
渐变插件:0.3渐变:1.4

这是一个问题,尤其是当库的
版本
配置阻止代码的编辑或调试过程正常进行时(本机方法的红色高亮显示,因为IDE在正常链接时不再找到它们,Java逐步调试中断…)

这在Google代码和这里的堆栈溢出上进行了彻底的讨论,碰巧提出:

将此内容放入你的应用程序:

依赖关系{
debugCompile项目(路径:':custom_lib',配置:“libraryDebug”)
releaseCompile项目(路径:':custom_lib',配置:“libraryRelease”)
}


在库的build.gradle中添加:

defaultPublishConfig“发布”
出版非默认真实
产品风味{
图书馆{
} 
}

我尝试了一下,但在Gradle同步时间我收到了以下消息:

Error:Configuration with name 'libraryDebug' not found. 
我只尝试了“
debug
”和“
release
”(这是我在lib中使用的构建配置名称),但结果是一样的

有没有办法让这个有趣的解决办法奏效?(我正在运行Android Studio 2.0 beta 2)

附录

  • 我的库的
    build.gradle
    文件(包括我当前使用的难看的解决方法):

    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.com/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'])     
    }
    
    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
    }
    
  • 我的项目的
    build.gradle
    文件:

    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.com/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'])     
    }
    
    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
    }
    
  • 我的渐变配置


要使其正常工作,必须同时修改两个
build.gradle
文件,才能一次性同步。我必须遵循以下步骤:

  • 步骤1:修改库的
    build.gradle
    ,正如凯恩所说:

    // "android" section:
    defaultPublishConfig 'release' 
    publishNonDefault true 
    productFlavors {
        library {
            /* This strange empty flavour is actually needed 
               for the step 3 to be successful               */
        } 
    }
    
    dependencies {
        debugCompile project(path: ':custom_lib', configuration: "libraryDebug")
        releaseCompile project(path: ':custom_lib', configuration: "libraryRelease") 
    }
    
  • 第2步:清理/重建

  • 步骤3:修改应用程序的
    build.gradle
    ,正如凯恩所说:

    // "android" section:
    defaultPublishConfig 'release' 
    publishNonDefault true 
    productFlavors {
        library {
            /* This strange empty flavour is actually needed 
               for the step 3 to be successful               */
        } 
    }
    
    dependencies {
        debugCompile project(path: ':custom_lib', configuration: "libraryDebug")
        releaseCompile project(path: ':custom_lib', configuration: "libraryRelease") 
    }
    
  • 第四步:梯度同步

因此,只需先更改库,然后在修改应用程序之前进行清理


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

无论我使用什么步骤,Shlublu的解决方案对我都不起作用

经过大量挖掘,我们发现可以定义可用的配置(至少使用实验梯度),从而避免了为了修复所有错误而必须进行清理/重建

使用AS2.1的最新gradle,我能够通过一个非常简单的依赖项内容解决这个问题:

Lib的build.gradle(使用实验性gradle 0.7.0):

应用程序的build.gradle(使用标准gradle 2.1.0):

我很确定,如果只使用调试/发布配置,则不需要库的gradle中的productFlavors,如:

Lib的build.gradle(使用实验性gradle 0.7.0):

应用程序的build.gradle(使用标准gradle 2.1.0):


请更新上面的gradle代码,以准确显示在实现我的修复后它的外观。将更容易看到可能出现的问题。@KaneO'Riley我现在已经开始工作了,不过JNI有一些问题。我现在要发布一个答案。相关: