首先生成两个打包的key一个debug和一个release放在app下面,然后在app下的gradle中的android节点下配置
signingConfigs {
release {
keyAlias "release"
keyPassword "release123456"
storeFile file("ReleaseKey.jks")
storePassword("release123456")
v1SigningEnabled true
v2SigningEnabled true
}
debug {
keyAlias "debug"
keyPassword "debug123456"
storeFile file("DebugKey.jks")
storePassword("debug123456")
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
debug {
minifyEnabled false // 混淆
zipAlignEnabled false // Zipalign优化
shrinkResources false // 移除无用的resource
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' // 混淆配置
signingConfig signingConfigs.debug
buildConfigField "String", "IP_SERVER", "\"http://192.168.1.251:10002/\"" //测试环境
}
release {
minifyEnabled false // 混淆
zipAlignEnabled false // Zipalign优化
shrinkResources false // 移除无用的resource
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' // 混淆配置
signingConfig signingConfigs.release
buildConfigField "String", "IP_SERVER", "\"https://www.wenyonghong.com/\"" //正式环境
}
}
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
tv.setText(BuildConfig.IP_SERVER); //通过BuildConfig.IP_SERVER获取Gradle中配置的IP地址
}
}
通过Build Variant切换dubug 或者release环境:
然后Build就能生成对应的环境的apk包:
接下来配置对应不同打包的渠道的区分,一次性打好所有平台的包:
同样在app下的gradle中的android节点下配置
productFlavors {
huawei {
buildConfigField "String", "RELEASE_CHANNEL", "\"1\""
}
xioami {
buildConfigField "String", "RELEASE_CHANNEL", "\"2\""
}
oppo {
buildConfigField "String", "RELEASE_CHANNEL", "\"3\""
}
vivo {
buildConfigField "String", "RELEASE_CHANNEL", "\"4\""
}
}
并在android->defaultConfig节点下配置
defaultConfig {
applicationId "com.signingconfigs"
minSdkVersion 19
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
flavorDimensions "default"
}
不配置flavorDimensions "default" 可能报以下错误
接下来
接下来
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
tv.setText(BuildConfig.IP_SERVER); //通过BuildConfig.IP_SERVER获取Gradle中配置的IP地址
tv.setText(BuildConfig.RELEASE_CHANNEL); //通过BuildConfig.RELEASE_CHANNEL获取区分不同平台的配置
}
}
通过点击Gradle->assembleRelease就可以一次性打包所有的区分平台的包
打包结果:
完整代码地址:https://gitee.com/JiGeXC/signingconfigs.git
代码下载后可能需要多次的clean project 和 rebuild project
可能还有许多足之处请多指教,谢谢!