[英]How do I get this project structure in Android Studio (AS) 1.1.0 to work to produce two APKs?
EDIT#3--changing directory structures since advised wrong. EDIT#3-更改目录结构,因为提示错误。
Based on this link I followed from a SO question, I need my file structure to appear like this in Android Studio (AS) 1.1.0 in order to get both a free and for-pay version of my GPS (Google Play Store) app: 基于这个链接,我从一个SO问题开始跟踪,我需要我的文件结构在Android Studio (AS) 1.1.0中像这样显示,以便同时获得我的GPS (Google Play商店)应用的免费版本和付费版本:
+-- main
¦ +-- AndroidManifest.xml
¦ +-- java
¦ ¦ +-- com
¦ ¦ +-- whatever
¦ ¦ +-- kakurocombos
¦ ¦ +-- MyActivity.java
¦ +-- res
¦ +-- layout
¦ ¦ +-- activity_main.xml
+-- FreeVersion
¦ +-- java
¦ +-- com
¦ +-- whatever
¦ +-- kakurocombos
¦ +-- Free.java (where FREE = true;)
+-- Pro
+-- java
¦ +-- com
¦ +-- whatever
¦ +-- kakurocombos
¦ +-- Free.java (where FREE = false;)
+-- res
+-- values
+-- string.xml
EDIT-- All of the above structure must be under src
as pointed out below in comments. 编辑-所有上述结构必须在
src
下,如以下注释中所指出。
I chose that structure because this is how that link (above) shows its structure: 我选择了这种结构,因为这是该链接(上方)显示其结构的方式:
├── 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
Note the absence of res
under production
because it will use res
under main
. 注意
production
没有 res
,因为它将在main
下使用res
。
Note the presence of res
under staging
since it uses different resources since it's the 2nd APK/package. 请注意,
staging
存在 res
,因为它是第二个APK /程序包,它使用不同的资源。
Here's how the directory structure looks in Windows 7 Explorer: 这是Windows 7资源管理器中目录结构的外观:
Here's how it looks in AS: ( EDITED !)( TWICE ) 这是AS中的外观:( 编辑 !)( TWICE )
Here's build.gradle
: 这是
build.gradle
:
apply plugin: 'com.android.application'
android
{
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig
{
applicationId "com.dslomer64.kakurocombosbuildvariants"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes
{
release
{
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors
{
paid
{
applicationId "com.dslomer64.kakurocombos.paid"
}
free
{
applicationId "com.dslomer.kakurocombos.free"
}
}
}
dependencies
{
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
The link alluded to at beginning of post has a few errors, including failure to append .paid
to the first package in the productFlavors
block. 文章开头提到的链接有一些错误,包括无法将
.paid
附加到productFlavors
块中的第一个软件包中。
Everything related to your flavors should be inside the src
folder, eg 与您的口味有关的所有内容都应位于
src
文件夹中,例如
src/main/...
src/free/...
src/pro/...
Right now your src
folder is at the same levels as the flavor folders, which is incorrect. 现在,您的
src
文件夹与flavor文件夹处于同一级别,这是不正确的。
See this documentation for confirmation of the above. 请参阅此文档以确认上述内容。
This is what finally worked for me. 这终于对我有用。 It is based on help from @CommonsWare here .
它是基于帮助从@CommonsWare 这里 。 Short answer: build variants.
简短答案:构建变体。 It is assumed that the two variants differ only slightly.
假定两个变体仅略有不同。 In this example, one needs to be declared FREE while the other must be declared NOT FREE.
在此示例中,一个必须声明为免费,而另一个必须声明为非免费。
The two build variants in this example are named pro
and free
. 此示例中的两个构建变体分别称为
pro
和free
。 They are not exactly variants of main
, for which we define the usual resources, but very short classes encapsulating the only differences between the variants. 它们不是
main
确切变体,我们为它们定义了常规资源,而是封装了变体之间唯一区别的非常短的类。 Do NOT define ANY resources for one of the two build variants so that it will "inherit" those of main
. 不要为两个build变体之一定义任何资源,以免“继承”
main
。
I chose free
to have the resources of main
. 我选择
free
拥有main
的资源。 In order for the other build variant, pro
, to have different name and icon (among other things), sufficient resources must be defined to provide differences from the other APK being built. 为了使其他构建变体
pro
具有不同的名称和图标(除其他外),必须定义足够的资源以提供与正在构建的其他APK的区别。
Using syntax similar to what is shown in build.gradle
below, we declare both pro
and free
to be build variants inside a productFlavors
block. 使用类似于下面的
build.gradle
中所示的语法,我们将pro
和free
声明为productFlavors
块中的构建变体。
build.gradle:
apply plugin: 'com.android.application'
android
{
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig
{
applicationId "com.dslomer64.kakurocombosbuildvariants"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes
{
release
{
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors
{
pro
{
applicationId "com.dslomer64.kakurocombosbuildvariants.pro"
}
free
{
applicationId "com.dslomer64.kakurocombosbuildvariants.free"
}
}
}
dependencies
{
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
Note that the two build variants extend the package name from com.dslomer64.kakurocombosbuildvariants, appending either .pro
or .free
to get unique names. 请注意,这两个构建变量从com.dslomer64.kakurocombosbuildvariants扩展包的名字,要么追加
.pro
或.free
获得唯一的名称。 These necessarily-unique names are eventually uploaded to GPS. 这些必须唯一的名称最终会上传到GPS。
AndroidManifest.xml
(for main
and free
): AndroidManifest.xml
(用于main
和free
):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dslomer64.kakurocombosbuildvariants" >
<application
android:icon="@mipmap/ic_launcher">
android:allowBackup="true"
>
<activity
android:screenOrientation="portrait"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:name=".MyActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
We will define a different app_name
and a different ic_launcher
for the two variants. 我们将为两个变体定义不同的
app_name
和不同的ic_launcher
。
MyActivity.java
contains almost all the code for the two slightly different apps. MyActivity.java
包含两个稍有不同的应用程序的几乎所有代码。 The only code contained in the other source apps is what distinguishes one from the other. 其他源应用程序中包含的唯一代码是一个与众不同的代码。 In this case, it's one line each in a class is named
Free
in both APK sources. 在这种情况下,两个APK来源中的一个类中的每一行都被命名为
Free
。 And MyActivity.java
refers to that class and variable in what would seem an ambiguous way, except that gradle takes care of that in making the two builds. MyActivity.java
似乎以一种模棱两可的方式引用了该类和变量,只是gradle在制作两个版本时会注意这一点。
Here's what the reference looks like in MyActivity.java
: 这是
MyActivity.java
的参考MyActivity.java
:
`boolean FREE = Free.FREE;`
(I chose to use the same field name, FREE
, in all 3 .java files AND to name the two classes Free
. Probably not a good move. But it worked.) (我选择在所有3个.java文件中使用相同的字段名称
FREE
,并将这两个类命名为Free
。这可能不是一个好办法。但这确实有效。)
Here's what the identical classes named Free
look like: 以下是名为
Free
的相同类的外观:
KakuroCombosBuildVariants\\app\\src\\**free**\\java\\com\\dslomer64\\kakurocombosbuildvariants\\Free.java
: KakuroCombosBuildVariants\\app\\src\\**free**\\java\\com\\dslomer64\\kakurocombosbuildvariants\\Free.java
:
package com.dslomer64.kakurocombosbuildvariants;
public class Free
{
public static final boolean FREE = true;
}
KakuroCombosBuildVariants\\app\\src\\**pro**\\java\\com\\dslomer64\\kakurocombosbuildvariants\\Free.java
: KakuroCombosBuildVariants\\app\\src\\**pro**\\java\\com\\dslomer64\\kakurocombosbuildvariants\\Free.java
:
package com.dslomer64.kakurocombosbuildvariants;
public class Free
{
public static final boolean FREE = false;
}
If you don't care about having different icons and app names, you're finished. 如果您不关心拥有不同的图标和应用程序名称,那么您就完成了。 Just click Build |
只需单击生成| Generate signed APKs ... and you'll see this:
生成签名的APK ...,您将看到以下内容:
And here are the TWO APKs: C:\\Users\\Dov\\AndroidStudioProjects\\KakuroCombosBuildVariants\\app
这是两个APK:
C:\\Users\\Dov\\AndroidStudioProjects\\KakuroCombosBuildVariants\\app
Inside AS, you see this: 在AS内部,您会看到以下内容:
You are ready to upload both versions of the app to GPS. 您可以将两个版本的应用程序都上传到GPS。 One at a time.
一次一个。
However, if you need different icons or names for the two apps, follow the same philosophy: let the resources for free
be defined by main
and define the different resources for pro
under its directory node. 但是,如果两个应用程序需要不同的图标或名称,请遵循相同的原则:让
main
定义free
资源,并在其目录节点下为pro
定义不同资源。 Note NO res
folder under free
; 注意NO
res
文件夹下free
; it uses the res
folder info in main
: 它使用
main
的res
文件夹信息:
Big picture: 大图:
Now note the complex res
folder under pro
, which defines its different name and icon, as well as its "java difference" from the free version. 现在,请注意
pro
下复杂的res
文件夹,该文件夹定义了其不同的名称和图标,以及与免费版本的“ java区别”。
Note that the mipmap
icons for the different versions have the same NAME-- ic_launcher
--but that name is defined differently in the strings.xml
file for the setup in main
(for free
) and in the strings.xml
file for pro
. 请注意,不同版本的
mipmap
图标具有相同的名称ic_launcher
但该名称在main
( free
)中的strings.xml
文件中和pro
的strings.xml
文件中的定义不同。
Same goes for the app_names, defined in main
for free
and in pro
for itself. app_names同样适用,在
main
中free
定义,在pro
中本身定义。
For pro
: 对于
pro
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Kakuro Combos Pro </string>
</resources>
For free
: free
:
Now is the other opportunity to upload the two APKs to GPS. 现在是另一个将两个APK上传到GPS的机会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.