在Gradle中编译一次生成不同的版本,动态设定应用标题,应用图标,替换常量,andrioid gradle...

gradle里的配置

sourceSets {
        main {      //重定义主程序各数据指向,因为是用eclipse导出的gradle
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }

        productFlavors {     //分支信息,程序里添加分支项时名称及包名必须于该处定义一致
            aaa {
                minSdkVersion 10
                applicationId 'com.aaa'
                targetSdkVersion 21
                versionCode 34
                versionName '3.4'
            }

            bbb {
                minSdkVersion 10
                applicationId 'com.bbb'
                targetSdkVersion 21
                versionCode 1
                versionName '1.0'
            }
        }

        //分支数据定向    
        aaa {
            res.srcDirs = ['aaa/res', 'aaa']
            java.srcDirs = ['aaa']
        }
        bbb {
            res.srcDirs = ['bbb/res', 'bbb']
            java.srcDirs = ['bbb']
        }

        

    }



写项目的时候经常会遇到以下的情况:
1.需要生成测试版本和正式版本的apk
2.测试版本和正式版本的URL是不一样的
3.测试版本和正式版本的包名需要不一致,这样才能安装到同一部手机上面。
4.不同apk需要应用名不同,图标不同,某些常量不同....
如果你有以上的需求,看这篇文章就对了

When developing an app, you usually have many slightly different versions of this app. The most common example is probably the backend you want to use:productionorstaging.

当我们做开发的时候,经常会需要生成多个版本的app。最常见的就是测试版和正式版。

You usually define the base URLs with the other constants of the app. Switching from one environment to the other is done by (un)commenting the right lines:

我们经常需要在应用中定义一些常量,当应用正式发布的时候,常常是注释掉测试用的部分,放开正式的部分,就像下面一样:

public static String BASE_URL = "http://staging.tamere.be"
//public static String BASE_URL = "http://production.tamere.be"

The process is manual, boring and error prone but hey, changing one line is not that bad, is it?

Then you add more and more features that depends on the environment. You maybe want a different icon and then different input validation rules and then ...

That's where your build tool can help. Let's see how we can automate the process of generating different APKs for different environment with Gradle.

上面的步骤是烦躁,无味的,修改一个地方还好,代码写多了以后正过来整过去就egg_pain了,这个时候我们Gragle就闪亮登场了

Build variants

Gradle has the concepts ofBuild TypesandBuild Flavors. When combining the two, you get aBuild Variant.

There two default build types:releaseanddebug. We won't change them in our example but we will create two new flavors:productionandstaging.

Gradle默认有release和debug两个版本。我们这里增加了production和staging.两两组合就是下面4个版本了。

As a result, we'll have four different variants:

  • ProductionDebug
  • ProductionRelease
  • StagingDebug
  • StagingRelease

Sample project 示例项目

The project is pretty simple but shows how you can define for each build variant:

  • an app name
  • an icon
  • constants (in our case aBASE_URLvariable)

You can download the project onGithub.

Here are two screenshots of the generated apps. Nothing really fancy:

示例项目很简单,在不同的版本中我们需要修改项目名称,项目图标,一些常量:url...,项目可以从Github下载,效果图如下:

Android App drawer showing the two differents flavors

Android App drawer showing the two differents flavors

build.gradle file

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 18
    }

    productFlavors {
        production {
            packageName "be.tamere.gradlebuildtypesexample"
        }

        staging {
            packageName "be.tamere.gradlebuildtypesexample.staging"
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:18.0.0'
}

The definition of the flavors is super simple, all the magic will happen in their folders.

修改很简单:核心配置是productFlavors,同时需要注意production和staging,它们需要与后面的目录结构名字一致

File structure 修改后的文件结构

In thesrcfolder, we've created two directories whose names must match the flavors. We will define all the flavor-specific values. Only specific values are necessary.

修改也比较简单,在src下面(main同级的目录)新建和上面productFlavors中配置production和staging相同的目录,分别放入对应的Constants.java。这两个目录的属性和功能与系统默认创建的main是一样的。

总结一下就是:

1.production和staging两个目录,对应着各存放一份Constants.java

2.对于应用图标和应用名字信息配置在res目录下面的。这个地方针对staging重新配置了一份ic_launcher.png和string.xml。production没配置的话就是用默认main下面的res。这个比较容易理解哈。

The staging version defines new icons while both flavors defines aConstants.java. The app name is defined in thestring.xmlfiles.

├── main
│ ├── AndroidManifest.xml
│ ├── ic_launcher-web.png
│ ├── java
│ │ └── be
│ │     └── tamere
│ │         └── gradlebuildtypesexample
│ │             └── MainActivity.java
│ └── res
│     ├── drawable-hdpi
│     │ └── ic_launcher.png
│     ├── drawable-mdpi
│     │ └── ic_launcher.png
│     ├── drawable-xhdpi
│     │ └── ic_launcher.png
│     ├── drawable-xxhdpi
│     │ └── ic_launcher.png
│     ├── layout
│     │ └── activity_main.xml
│     ├── menu
│     │ └── main.xml
│     ├── values
│     │ ├── dimens.xml
│     │ ├── strings.xml
│     │ └── styles.xml
│     ├── values-v11
│     │ └── styles.xml
│     └── values-v14
│         └── styles.xml
├── production
│ └── java
│     └── be
│         └── tamere
│             └── gradlebuildtypesexample
│                 └── Constants.java
└── staging
    ├── java
    │ └── be
    │     └── tamere
    │         └── gradlebuildtypesexample
    │             └── Constants.java
    └── res
        ├── drawable-hdpi
        │ └── ic_launcher.png
        ├── drawable-mdpi
        │ └── ic_launcher.png
        ├── drawable-xhdpi
        │ └── ic_launcher.png
        ├── drawable-xxhdpi
        │ └── ic_launcher.png
        └── values
            └── string.xml

Android Studio

You can switch between the two flavors in theBuild variantstab of the IDE. Android Studio has some trouble identifying the resources for a non-active flavors.

We are using theproductionflavor, Studio does not understand that thestagingfolder contains source code. Don't worry, it's normal, it will catch up when you switch to the staging variant.

Android Studio IDE showing different build variants

Launch the app with the different flavors to see the result.



原文地址:http://www.bkjia.com/Androidjc/929410.html






在 Android Studio 使用 Gradle 构建工具进行编译和打包,生成 .so 动态链接库,可以按照以下步骤进行: 1. 在 Android Studio 创建一个 C/C++ 模块,选择 File -> New -> New Module -> C/C++ Library,然后按照向导设置模块名称、语言类型和支持的 ABI。 2. 在模块的 build.gradle 文件,添加以下配置: ```groovy android { // 指定支持的 ABI,包括 armeabi-v7a、arm64-v8a、x86、x86_64 等 ndk { abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' } // 配置 CMakeLists.txt 文件路径 externalNativeBuild { cmake { path "CMakeLists.txt" } } } // 配置 CMake 版本 cmake { version "3.10.2" } ``` 3. 在模块的 src/main 目录下创建 jni 文件夹,并将 C/C++ 代码放入该文件夹。 4. 在 jni 文件夹下创建 CMakeLists.txt 文件,并编写编译选项和链接选项,指定生成 .so 文件的名称和路径,以及引入其他依赖库等。例如: ```cmake # 设置编译选项 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # 设置源文件路径 file(GLOB_RECURSE SOURCE_FILES "*.cpp") # 设置头文件路径 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) # 配置动态库输出路径和名称 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}) set(CMAKE_LIBRARY_OUTPUT_NAME "native-lib") # 引入其他依赖库 find_library(log-lib log) # 生成动态库 add_library(native-lib SHARED ${SOURCE_FILES}) target_link_libraries(native-lib ${log-lib}) ``` 5. 点击 Android Studio 工具栏的 Build -> Make Project,进行编译和打包,生成 .so 动态链接库。 6. 在模块的 build.gradle 文件查看生成的 .so 文件路径,例如: ```groovy android { externalNativeBuild { cmake { path "CMakeLists.txt" } } sourceSets { main { jniLibs.srcDirs = ['src/main/jniLibs'] } } } ``` 以上就是在 Android Studio 使用 Gradle 构建工具进行编译和打包,生成 .so 动态链接库的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值