gpt4 book ai didi

java - 可以将包名称变量传递到 Android 源代码中吗?

转载 作者:行者123 更新时间:2023-11-29 19:31:06 26 4
gpt4 key购买 nike

这通常是构建 Android 代码以声明包和导入的方式:

package com.company.myApp

import com.company.myApp.SomeClass
import com.company.myApp.somePackage.SomeOtherClass

是否有任何方法可以使用 Android Studio/Java/任何方式传递“编译时”变量或 pragma 以有效地将“com.company.myApp”字符串传递给源代码?像这样的东西:

package BASE_PACKAGE_NAME

import BASE_PACKAGE_NAME.SomeClass
import BASE_PACKAGE_NAME.somePackage.SomeOtherClass

最佳答案

我个人不知道有什么适合你的但正如我在 http://tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename 中读到的那样

The final package that is used in your built .apk's manifest, and is the package your app is known as on your device and in the Google Play store, is the "application id".

The package that is used in your source code to refer to your R class, and to resolve any relative activity/service registrations, continues to be called the "package".

您可以在您的 gradle 文件中指定应用程序 ID,如下所示:

应用程序/build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 19
buildToolsVersion "19.1"

defaultConfig {
applicationId "com.example.my.app"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}

Here comes the critical part: When you've done the above, the two packages are independent. You are completely free to refactor your code - changing the internal package used for your activities and services, updating your Manifest package, and refactoring your import statements. This will have no bearing on the final id of your application, which is now always going to be the applicationId specified in the Gradle file.

You can vary the applicationId of your app for flavors and build types by using the following Gradle DSL methods:

应用程序/build.gradle:

productFlavors {
pro {
applicationId = "com.example.my.pkg.pro"
}
free {
applicationId = "com.example.my.pkg.free"
}
}

buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
....

关于java - 可以将包名称变量传递到 Android 源代码中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39951303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com