初次探索Android Gradle

在Android开发中,Gradle作为Android代码的编译工具,有很多强大,方便的功能等待去学习,这里就先简单了解一下。首先推荐几篇参考的文章:

1、Android项目中如何用好构建神器Gradle?

2、美团Android自动化之旅—适配渠道包

3、http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Why-Gradle-

4、http://google.github.io/android-gradle-dsl/current/index.html

在项目中经常会遇到各个渠道版本有差别的情况,或者出于各种不同的目的,需要在同一手机安装两个或者多个不同的应用等等需求,利用gradle可以很简单的实现。

首先在Android studio项目中可能存在多个*.gradle文件

模块内的.gradle文件只对该模块起作用,这里主要讨论本地代码下的.gradle。

一、我们以Android studio下同一份代码,在手机上安装运行两个app为例:

首先设置本地代码下的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.example.lee.dex_demo"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false  //设法设置代码混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //代码混淆文件 的位置
        }
        debug{
            applicationIdSuffix ".debug"
        }
        //自定义包的类型  继承debug类型
        jniDebug{
            initWith(buildTypes.debug)
        }
    }

    //设置不同包名  这样就可以在手机运行两个相同的app
    productFlavors{
        apk1{
            applicationId "com.example.lee.dex_demo"
            versionName "1.0"
        }
        apk2{
            applicationId "com.example.lee.dex_demo2"
            versionName "apk2"
        }
    }

    dexOptions{
        javaMaxHeapSize "4g"
        preDexLibraries = false
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:multidex:1.0.0'
}
设置完成后    可以在Android studio左下方的Build Variants下查看

在这里选择要运行的app类型   运行安装即可

在apk1对应的资源文件strings.xml中添加     <string name="title">apk1的标题</string>

在apk2对应的资源文件strings.xml中添加     <string name="title">apk2的标题</string>

在布局文件中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.lee.dex_demo.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title"
        />

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="click"
        />
</LinearLayout>

给textview设置文本title,当运行apk1和apk2时就会显示对应的文本。


二、利用gradle dex分包,解决方法数超过65536.

关于这个问题,谷歌文档里面有官方的解决方案(Building Apps with Over 65K Methods)。

这里不在啰嗦,网上方法很多,文档里面也有。

一、http://blog.csdn.net/gaozhan_csdn/article/details/51992100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值