2016-05-16 51 views
1

我正在使用Android Studio 2.1与NDK。但我面临着如何配置versionCode的问题。来自this androidbycode博客链接和this来自Stackoverflow的答案我试图编辑我的原始bulid.gradle,但它显示同步错误。Android NDK版本代码方案发布APK每架构

的build.gradle(它的正常工作,当没有版本合并)

apply plugin: 'com.android.model.application' 

model { 
    android { 
     compileSdkVersion 23 
     buildToolsVersion "23.0.3" 

     defaultConfig { 
      applicationId "com.mytestapp" 
      minSdkVersion.apiLevel 15 
      targetSdkVersion.apiLevel 23 
      versionCode 101 
      versionName "1.0.1" 
     } 

     buildTypes { 
      release { 
       minifyEnabled true 
       proguardFiles.add(file("proguard-rules.pro")) 
       signingConfig = $("android.signingConfigs.myConfig") 
      } 
      debug { 
       debuggable true 
      } 
     } 

     productFlavors { 
      create("armeabi") { 
       ndk { 
        abiFilters.add("armeabi") 
        versionCode 1 
       } 
      } 
      create("armeabi-v7a") { 
       ndk { 
        abiFilters.add("armeabi-v7a") 
        versionCode 3 
       } 
      } 
      create("x86") { 
       ndk { 
        abiFilters.add("x86") 
        versionCode 6 
       } 
      } 
      create("fat") { 
       ndk { 
        abiFilters.add("armeabi") 
        abiFilters.add("armeabi-v7a") 
        abiFilters.add("x86") 
        versionCode 0 
       } 
      } 
     } 
    } 

    // You can modify the NDK configuration for each variant. 
    components.android { 
     binaries.afterEach { binary -> 
      binary.mergedNdkConfig.cppFlags.add(
        "-DVARIANT=\"" + binary.name + "\"") 
     } 
    } 


    android.signingConfigs { 
     create("myConfig") { 
      storeFile "MyKeyStoreLocation" 
      storePassword "MyStorePassword" 
      keyAlias "mykeyAlias" 
      keyPassword "mykeyPassword" 
      storeType "jks" 
     } 
    } 

    android.ndk { 
     moduleName = "settings-jni" 
    } 

} 

repositories { 
    mavenCentral() 
    maven { 
     url "http://dl.bintray.com/lukaville/maven" 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.4.0' 
    compile 'com.android.support:design:23.4.0' 
    compile 'com.google.android.gms:play-services-ads:8.4.0' 
    compile 'net.zetetic:android-database-sqlcipher:[email protected]' 
    compile 'com.nbsp:library:1.08' 
    compile 'com.android.support:recyclerview-v7:23.4.0' 
    compile 'com.android.support:cardview-v7:23.4.0' 
    compile 'com.google.code.gson:gson:2.6.2' 
    compile 'com.mcxiaoke.volley:library:1.+' 
    compile 'com.daimajia.numberprogressbar:library:[email protected]' 
    compile 'com.github.paolorotolo:appintro:3.4.0' 
} 

如上建议我不能找到一种方法来合并为ABI版本,的代码片段SO提到回答

applicationVariants.all { variant -> 
     // get the version code of each flavor 
     def abiVersion = variant.productFlavors.get(0).versionCode 

     // set the composite code 
     variant.mergedFlavor.versionCode = abiVersion * 100000 + defaultConfig.versionCode 
    } 

或者,如上面提到的博客链接的建议,我build.gradle -

project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9] 

    android.applicationVariants.all { variant -> 
     variant.outputs.each { output -> 
      output.versionCodeOverride = 
       project.ext.versionCodes.get(output.getFilter(
        com.android.build.OutputFile.ABI), 0) * 10000000 + android.defaultConfig.versionCode 
     } 
    } 

回答

1

你可以按照对应于您所遇到的问题这个错误:https://code.google.com/p/android/issues/detail?id=192943

我给了一个解决方案,您正在使用的口味,将仍然为你工作 - 定义versionCodeBase和用它来生成每种风味的版本代码:

model { 
    def versionCodeBase = 11; 
    def versionCodePrefixes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]; 

    //... 

    android.productFlavors { 
     create ("armv7") { 
      ndk.abiFilters.add("armeabi-v7a") 
      versionCode = versionCodePrefixes.get("armeabi-v7a", 0) * 1000000 + versionCodeBase 
     } 
     create ("x86") { 
      ndk.abiFilters.add("x86") 
      versionCode = versionCodePrefixes.get("x86", 0) * 1000000 + versionCodeBase 
     } 
    } 
} 
+0

谢谢你,你的答案奏效(尽管我必须修改它,有点)。我有一些疑问 - 1.我无法从'model'内定义'versionCodeBase'和'versionCodePrefixes'。在'model'之外定义它们工作正常。为什么会这样,会导致未来的问题? 2.由于版本现在只能从这两个变量计算出来,我可以并且应该从我的'defaultConfig'中删除'versionCode'吗? (..还有一个问题,出现字符限制) –

+0

3.正如我在OP中提到的,我也使用了“fat”发行版。我应该在'versionCodePrefixes'中指定它。目前我只是在我的'胖'productFlavor中使用以下代码行=>'versionCode = versionCodePrefixes.get(“fat”,0)* 1000000 + versionCodeBase'但是这会输出简单的'versionCodeBase'(例如11)因为我没有为'fat'释放定义任何值。正在使用这种方法对'fat'进行版本控制吗?因为它会输出2位数字的版本,而其他的会以7位数字的形式发布?(Upvoted you,只要我的疑问至少在某种程度上得到解决,将其标记为答案) –

+0

对于#1,如果您需要知道,我正在使用'gradle-experimental:0.7.0' –