简体   繁体   English

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

[英]Workaround to link a shared library in debug mode with Android Studio (v2+)

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: 很久以前, Google代码票据被打开,因为Gradle插件不会传播,无论您是对依赖的android库进行调试构建还是发布构建:

DESCRIPTION: 描述:
Gradle plugin does not propagate whether you are doing a debug build or a release build to a dependent android library. 无论您是对依赖的android库进行调试构建还是发布构建,Gradle插件都不会传播。

VERSION INFORMATION: 版本信息:
gradle plugin: 0.3 gradle: 1.4 gradle插件: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...). 这是一个问题,特别是当库的release配置阻止代码版本或调试过程正常发生时(本机方法的红色突出显示,因为IDE在正常链接时不再找到它们,断续续续的Java步骤 - 步调试......)。

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 : 在Google Code和Stack Overflow上对此进行了彻底的讨论, Kane O'Riley提出了一个非常有趣(干净)的解决方法

Put this in your app: dependencies { debugCompile project(path: ':custom_lib', configuration: "libraryDebug") releaseCompile project(path: ':custom_lib', configuration: "libraryRelease") } 把它放在你的应用程序中: 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 { } } 并在您的库的build.gradle中添加: defaultPublishConfig 'release' publishNonDefault true productFlavors { library { } }

I gave a try, but I have the following message at Gradle sync time: 我试了一下,但是我在Gradle同步时有以下消息:

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. 我尝试使用“ debug ”和“ release ”(这是我在lib中使用的构建配置名称)但结果是一样的。

Any idea to make this interesting workaround working? 有什么想让这个有趣的解决方案有效吗? (I'm running Android Studio 2.0 beta 2) (我正在运行Android Studio 2.0 beta 2)

APPENDIX : 附录

  • my lib's build.gradle file (including the ugly workaround I'm currently using): 我的lib的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']) } 
  • my project's build.gradle file: 我的项目的build.gradle文件:

     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 : 我的Gradle配置

我的Gradle配置

For this to work, the two build.gradle files must not be modified at the same time before syncing once for all. 为此,在同步一次之前, 不得同时修改两个build.gradle文件。 I had to follow the following steps: 我必须遵循以下步骤:

  • Step 1: modify the lib's build.gradle , exactly as Kane said: 第1步:修改lib的build.gradle ,正如凯恩所说:

     // "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 第2步: 清理/重建

  • Step 3: modify the app's build.gradle , also as Kane said: 第3步:修改应用程序的build.gradle ,同时Kane说:

     dependencies { debugCompile project(path: ':custom_lib', configuration: "libraryDebug") releaseCompile project(path: ':custom_lib', configuration: "libraryRelease") } 
  • Step 4: Gradle sync. 第4步:Gradle同步。

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 !). 我检查了由debugrelease构建模式生成的APK,并且每个都包含lib的正确变体,与应用此变通方法之前不同,所以它确实有效(感谢Kane!)。

Shlublu's solution didn't work for me, no matter what steps I used. 无论我使用什么步骤,Shlublu的解决方案对我都无效。

After much digging, found out that one can define available configurations (at least using experimental gradle), and this in turn avoided having to clean/rebuild in hope of fixing all errors. 经过大量挖掘,发现可以定义可用的配置(至少使用实验gradle),这反过来避免了必须清理/重建以希望修复所有错误。

Using latest gradle with AS2.1, I was able to solve this issue with a very simple dependency content: 使用最新的gradle和AS2.1,我能够通过一个非常简单的依赖内容来解决这个问题:

Lib's build.gradle (uses experimental gradle 0.7.0): Lib的build.gradle(使用实验gradle 0.7.0):

model {
  android {
...
    productFlavors {
       create('all') {
         ...
       }
    }
    publishNonDefault true
  }
}
configurations {
  allDebug
  allRelease
}

App's build.gradle (uses standard gradle 2.1.0): App的build.gradle(使用标准gradle 2.1.0):

debugCompile project(path: ':lib', configuration: 'allDebug')
releaseCompile project(path: ':lib', configuration: 'allRelease')

I'm pretty sure the productFlavors in the lib's gradle is not needed if using debug/release configurations only, like this: 我非常确定如果仅使用调试/发布配置,则不需要lib的gradle中的productFlavors,如下所示:

Lib's build.gradle (uses experimental gradle 0.7.0): Lib的build.gradle(使用实验gradle 0.7.0):

model {
...
}
configurations {
  debug
  release
}

App's build.gradle (uses standard gradle 2.1.0): App的build.gradle(使用标准gradle 2.1.0):

debugCompile project(path: ':lib', configuration: 'debug')
releaseCompile project(path: ':lib', configuration: 'release')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM