android菜单变化

一、版本演变

android5.0以后,ActionBarActivity被淘汰,通过AppCompatActivity支持库,使低版本的android也支持material design。
并且,Toolbar也代替了ActionBar。

二、使用Toolbar

配置No Action Bar

在Manifest.xml里配置application使用的theme。
在theme对应的style里,增加下面item:
false
true
这样,应用就使用toolbar,而不是以前的actionbar了。

配置Toolbar

在主Activity的XML布局配置里加上:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".ui.login.LoginActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/MyPopupMenuTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:titleTextColor="@android:color/white"
        android:background="?attr/colorPrimary"
        tools:ignore="MissingConstraints">
    </androidx.appcompat.widget.Toolbar>
......
</androidx.constraintlayout.widget.ConstraintLayout>

在app对应的theme里增加配置项:

<item name="actionOverflowMenuStyle">@style/MyPopupMenuTheme</item>

这样,才能使下拉菜单定制的style生效。

android在API29版本支持深色模式,深色模式在安卓上可以分为以下四种场景:

强制深色模式

强制浅色模式

跟随系统

低电量自动切换深色

下面的kotlin代码用来判断当前配置环境是否支持night:

/**
 * 判断当前是否深色模式
 *
 * @return 深色模式返回 true,否则返回false
 */
fun isNightMode(): Boolean {
  return when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
    Configuration.UI_MODE_NIGHT_YES -> true
    else -> false
  }
}

Tips: 对于一些从网络接口服务获取的需要对深色模式区分的色值或者图片,可以使用上述的判断来获取对应的资源。

判断当前深色模式场景
通过AppCompatDelegate.getDefaultNightMode()可以获取五种深色模式场景:

MODE_NIGHT_AUTO_BATTERY 低电量模式自动开启深色模式
MODE_NIGHT_FOLLOW_SYSTEM 跟随系统开启和关闭深色模式(默认)
MODE_NIGHT_NO 强制使用notnight资源,表示非深色模式
MODE_NIGHT_YES 强制使用night资源
MODE_NIGHT_UNSPECIFIED 配合 setLocalNightMode(int) 使用,表示由Activity通过AppCompactActivity.getDelegate()来单独设置页面的深色模式,不设置全局模式

模式设置
深色模式设置可以从三个层级设置,分别是系统层、Applcation层以及Activity层。底层的设置会覆盖上层的设置,例如系统设置了深色模式,但是Application设置了浅色模式,那么应用会显示浅色主题。

系统层是指系统设置中,根据不同产商的手机,可以在设置->显示中修改系统为深色模式。

Application层通过AppCompatDelegate.setDefaultNightMode()设置深色模式。

Activity层通过getDelegate().setLocalNightMode()设置深色模式。

当深色模式改变时,Activity会重建,如果不希望Activity重建,可以在AndroidManifest.xml中对对应的Activity设置android:configChanges=“uiMode”,不过设置之后页面的颜色改变需要Activity在中通过监听onConfigurationChanged来动态改变。

通过AppCompatDelegate.setDefaultNightMode(int)可以设置深色模式,源码如下:

public static void setDefaultNightMode(@NightMode int mode) {
  if (DEBUG) {
    Log.d(TAG, String.format("setDefaultNightMode. New:%d, Current:%d",
                             mode, sDefaultNightMode));
  }
  switch (mode) {
    case MODE_NIGHT_NO:
    case MODE_NIGHT_YES:
    case MODE_NIGHT_FOLLOW_SYSTEM:
    case MODE_NIGHT_AUTO_TIME:
    case MODE_NIGHT_AUTO_BATTERY:
      if (sDefaultNightMode != mode) {
        sDefaultNightMode = mode;
        applyDayNightToActiveDelegates();
      }
      break;
    default:
      Log.d(TAG, "setDefaultNightMode() called with an unknown mode");
      break;
  }
}

从源码可以看出设置 MODE_NIGHT_UNSPECIFIED 模式是不会生效的。

Tips:注意,深色模式变化会导致Activity重建。

适配方案

自定义适配

  1. 主题
    将Application和Activity的主题修改为集成自Theme.AppCompat.DayNight或者Theme.MaterialComponents.DayNight,就可以对于大部分的控件得到较好的深色模式支持。我们看下DayNight主题的定义:

​ res/values/values.xml


<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:ns1="urn:oasis:names:tc:xliff:document:1.2">		
    &lt;-- ... -->
    <style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat.Light"/>
    <style name="Theme.AppCompat.DayNight.DarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar"/>
    <style name="Theme.AppCompat.DayNight.Dialog" parent="Theme.AppCompat.Light.Dialog"/>
    <style name="Theme.AppCompat.DayNight.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog.Alert"/>
    <style name="Theme.AppCompat.DayNight.Dialog.MinWidth" parent="Theme.AppCompat.Light.Dialog.MinWidth"/>
    <style name="Theme.AppCompat.DayNight.DialogWhenLarge" parent="Theme.AppCompat.Light.DialogWhenLarge"/>
    <style name="Theme.AppCompat.DayNight.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar"/>
    &lt;-- ... -->
</resources>
​ res/values-night-v8/values-night-v8.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat"/>
    <style name="Theme.AppCompat.DayNight.DarkActionBar" parent="Theme.AppCompat"/>
    <style name="Theme.AppCompat.DayNight.Dialog" parent="Theme.AppCompat.Dialog"/>
    <style name="Theme.AppCompat.DayNight.Dialog.Alert" parent="Theme.AppCompat.Dialog.Alert"/>
    <style name="Theme.AppCompat.DayNight.Dialog.MinWidth" parent="Theme.AppCompat.Dialog.MinWidth"/>
    <style name="Theme.AppCompat.DayNight.DialogWhenLarge" parent="Theme.AppCompat.DialogWhenLarge"/>
    <style name="Theme.AppCompat.DayNight.NoActionBar" parent="Theme.AppCompat.NoActionBar"/>
    <style name="ThemeOverlay.AppCompat.DayNight" parent="ThemeOverlay.AppCompat.Dark"/>
</resources>



​ res/values/values.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:ns1="urn:oasis:names:tc:xliff:document:1.2" xmlns:ns2="http://schemas.android.com/tools">
    &lt;-- ... -->
    <style name="Theme.MaterialComponents.DayNight" parent="Theme.MaterialComponents.Light"/>
    <style name="Theme.MaterialComponents.DayNight.BottomSheetDialog" parent="Theme.MaterialComponents.Light.BottomSheetDialog"/>
    <style name="Theme.MaterialComponents.DayNight.Bridge" parent="Theme.MaterialComponents.Light.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.DarkActionBar" parent="Theme.MaterialComponents.Light.DarkActionBar"/>
    <style name="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog" parent="Theme.MaterialComponents.Light.Dialog"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.Alert" parent="Theme.MaterialComponents.Light.Dialog.Alert"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.Alert.Bridge" parent="Theme.MaterialComponents.Light.Dialog.Alert.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.Bridge" parent="Theme.MaterialComponents.Light.Dialog.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.FixedSize" parent="Theme.MaterialComponents.Light.Dialog.FixedSize"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.FixedSize.Bridge" parent="Theme.MaterialComponents.Light.Dialog.FixedSize.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.MinWidth" parent="Theme.MaterialComponents.Light.Dialog.MinWidth"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.MinWidth.Bridge" parent="Theme.MaterialComponents.Light.Dialog.MinWidth.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.DialogWhenLarge" parent="Theme.MaterialComponents.Light.DialogWhenLarge"/>
    <style name="Theme.MaterialComponents.DayNight.NoActionBar" parent="Theme.MaterialComponents.Light.NoActionBar"/>
    <style name="Theme.MaterialComponents.DayNight.NoActionBar.Bridge" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge"/>
    &lt;-- ... -->
</resources>
​ res/values-night-v8/values-night-v8.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.MaterialComponents.DayNight" parent="Theme.MaterialComponents"/>
    <style name="Theme.MaterialComponents.DayNight.BottomSheetDialog" parent="Theme.MaterialComponents.BottomSheetDialog"/>
    <style name="Theme.MaterialComponents.DayNight.Bridge" parent="Theme.MaterialComponents.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.DarkActionBar" parent="Theme.MaterialComponents"/>
    <style name="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge" parent="Theme.MaterialComponents.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog" parent="Theme.MaterialComponents.Dialog"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.Alert" parent="Theme.MaterialComponents.Dialog.Alert"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.Alert.Bridge" parent="Theme.MaterialComponents.Dialog.Alert.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.Bridge" parent="Theme.MaterialComponents.Dialog.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.FixedSize" parent="Theme.MaterialComponents.Dialog.FixedSize"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.FixedSize.Bridge" parent="Theme.MaterialComponents.Dialog.FixedSize.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.MinWidth" parent="Theme.MaterialComponents.Dialog.MinWidth"/>
    <style name="Theme.MaterialComponents.DayNight.Dialog.MinWidth.Bridge" parent="Theme.MaterialComponents.Dialog.MinWidth.Bridge"/>
    <style name="Theme.MaterialComponents.DayNight.DialogWhenLarge" parent="Theme.MaterialComponents.DialogWhenLarge"/>
    <style name="Theme.MaterialComponents.DayNight.NoActionBar" parent="Theme.MaterialComponents.NoActionBar"/>
    <style name="Theme.MaterialComponents.DayNight.NoActionBar.Bridge" parent="Theme.MaterialComponents.NoActionBar.Bridge"/>
    <style name="ThemeOverlay.MaterialComponents.DayNight.BottomSheetDialog" parent="ThemeOverlay.MaterialComponents.BottomSheetDialog"/>
    <style name="Widget.MaterialComponents.ActionBar.PrimarySurface" parent="Widget.MaterialComponents.ActionBar.Surface"/>
    <style name="Widget.MaterialComponents.AppBarLayout.PrimarySurface" parent="Widget.MaterialComponents.AppBarLayout.Surface"/>
    <style name="Widget.MaterialComponents.BottomAppBar.PrimarySurface" parent="Widget.MaterialComponents.BottomAppBar"/>
    <style name="Widget.MaterialComponents.BottomNavigationView.PrimarySurface" parent="Widget.MaterialComponents.BottomNavigationView"/>
    <style name="Widget.MaterialComponents.TabLayout.PrimarySurface" parent="Widget.MaterialComponents.TabLayout"/>
    <style name="Widget.MaterialComponents.Toolbar.PrimarySurface" parent="Widget.MaterialComponents.Toolbar.Surface"/>
</resources>

Tips: MaterialComponents.Bridge继承自AppCompat主题,并增加了Material Components的主题属性,如果项目之前是用的AppCompat,那么使用对应的Bridge主题可以快速切换到Material Design。

从上面的分析可以看出,DayNight就是在values以及values-night中分别定义了浅色和深色的主题。如果我们的主题直接继承DayNight主题,那么就不需要重复地声明对应的night主题资源了。

如果我们想对深色模式主题添加自定义属性,那么我们可以不继承DayNight主题,并显示地声明主题对应的night资源,例如

​ res/values/themes.xml


<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
    &lt;-- ... -->
    <item name="android:windowLightStatusBar">true</item>
</style>
​ res/values-night/themes.xml

<style name="Theme.MyApp" parent="Theme.MaterialComponents">
    &lt;-- ... -->
    <item name="android:windowLightStatusBar">false</item>
</style>

Tips: 若需要动态修改主题要在调用inflate之前调用,否则不会生效。

对于overflow弹出式菜单的字体显示控制,需要的配置项style name可能与下面的相关:
textAppearanceLargePopupMenu
textAppearanceSmallPopupMenu
popupMenuStyle

对于系统定义的appcompat text appearance配置项,主要有:

style TextAppearance_AppCompat
style TextAppearance_AppCompat_Body1
style TextAppearance_AppCompat_Body2
style TextAppearance_AppCompat_Button
style TextAppearance_AppCompat_Caption
style TextAppearance_AppCompat_Display1
style TextAppearance_AppCompat_Display2
style TextAppearance_AppCompat_Display3
style TextAppearance_AppCompat_Display4
style TextAppearance_AppCompat_Headline
style TextAppearance_AppCompat_Inverse
style TextAppearance_AppCompat_Large
style TextAppearance_AppCompat_Large_Inverse
style TextAppearance_AppCompat_Light_SearchResult_Subtitle
style TextAppearance_AppCompat_Light_SearchResult_Title
style TextAppearance_AppCompat_Light_Widget_PopupMenu_Large
style TextAppearance_AppCompat_Light_Widget_PopupMenu_Small
style TextAppearance_AppCompat_Medium
style TextAppearance_AppCompat_Medium_Inverse
style TextAppearance_AppCompat_Menu
style TextAppearance_AppCompat_SearchResult_Subtitle
style TextAppearance_AppCompat_SearchResult_Title
style TextAppearance_AppCompat_Small
style TextAppearance_AppCompat_Small_Inverse
style TextAppearance_AppCompat_Subhead
style TextAppearance_AppCompat_Subhead_Inverse
style TextAppearance_AppCompat_Title
style TextAppearance_AppCompat_Title_Inverse
style TextAppearance_AppCompat_Widget_ActionBar_Menu
style TextAppearance_AppCompat_Widget_ActionBar_Subtitle
style TextAppearance_AppCompat_Widget_ActionBar_Subtitle_Inverse
style TextAppearance_AppCompat_Widget_ActionBar_Title
style TextAppearance_AppCompat_Widget_ActionBar_Title_Inverse
style TextAppearance_AppCompat_Widget_ActionMode_Subtitle
style TextAppearance_AppCompat_Widget_ActionMode_Subtitle_Inverse
style TextAppearance_AppCompat_Widget_ActionMode_Title
style TextAppearance_AppCompat_Widget_ActionMode_Title_Inverse
style TextAppearance_AppCompat_Widget_Button
style TextAppearance_AppCompat_Widget_Button_Borderless_Colored
style TextAppearance_AppCompat_Widget_Button_Colored
style TextAppearance_AppCompat_Widget_Button_Inverse
style TextAppearance_AppCompat_Widget_DropDownItem
style TextAppearance_AppCompat_Widget_PopupMenu_Header
style TextAppearance_AppCompat_Widget_PopupMenu_Large
style TextAppearance_AppCompat_Widget_PopupMenu_Small
style TextAppearance_AppCompat_Widget_Switch
style TextAppearance_AppCompat_Widget_TextView_SpinnerItem

若要不跟随系统字体大小的变化而变化,增加继承函数:

    override fun getResources(): Resources {
        val res = super.getResources()
        val config = Configuration()
        config.setToDefaults()
        res.updateConfiguration(config, res.displayMetrics)
        return res

    }

今天在做一个menu的时候,需要改动item的高度和字体大小颜色。搜索了一下很多方法都不管用,最后是结合了 stack overflow 里面几个回答的方法勉强实现效果。最近比较忙,先记录下可以实现的方法,以后再找专门时间好好填坑。

首先对 Android 内部这个弹出menu似乎命名为 OverflowMenu,默认3个点的 more 按钮应该是命名为OverflowButton,但是部分属性却以PopupMenuXXX、PopupXXX、XXXPopup之类格式命名。主要改变item各种属性的方法是:自定义一个 style,然后在 Toolbar 的 Theme 里使用该 style(暂时叫主 style?),比较麻烦的是,这个主 style 的 item 主要引用其他的 style(暂时叫子 style 吧) ,而部分属性看似好像某个子 style 的子属性其实却有另外专门的 item,另外 子 style 的 parent 继承对结果也有影响(无效或者出现某些意想不到的效果)。

下面给出一段可以改动以下属性的代码,相关解释直接在注释里给出好了:

更换弹出menu图标

弹出时不遮拦Toolbar

Menu item 的高度

Menu item 的背景

Menu item 的字体大小颜色

Menu item 的分隔线


  <style name="Toolbar.TechStar" parent="ThemeOverlay.AppCompat.Dark">
    <!--修改菜单栏右边三个白点图标-->
    <item name="android:actionOverflowButtonStyle">@style/TechStarOverflowButtonStyle</item>
    <!--ToolBar右边弹出菜单背景色 不遮拦Toolbar-->
    <item name="actionOverflowMenuStyle">@style/techstar.MenuStyle</item>
    <!--Menu item 高度 这里不需要指定style 可直接指定-->
    <item name="android:listPreferredItemHeightSmall">@dimen/height_45dp</item>
    <!-- 分隔线 -->
    <item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>
    <!-- Menu item 文字样式相关 比较迷的一个点  我自己的项目是留下Large两个item其1就可以实现  可能和分辨率或者和建造menu时的选项相关???-->
    <item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
    <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
    <item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
  </style>

  <!--修改菜单栏右边三个白点图标-->
  <style name="TechStarOverflowButtonStyle"
      parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_more</item>
  </style>

  <!--ToolBar右边弹出菜单背景色  overlapAncho属性选false可不遮拦Toolbar-->
  <style name="techstar.MenuStyle" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
    <item name="overlapAnchor">false</item>
    <item name="android:popupBackground">#00FF00</item>
  </style>

  <!-- 分隔线 -->
  <style name="PopupMenuListView" parent="@android:style/Widget.Holo.ListView.DropDown">
    <item name="android:divider">#FF0000</item>
    <item name="android:dividerHeight">2dp</item>
  </style>

  <!-- Menu item 文字样式  对应item的large或者small 选择parent 否则无效-->
  <style name="myPopupMenuTextAppearanceSmall"
      parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
    <item name="android:textColor">#FF0000</item>
  </style>

  <!-- Menu item 文字样式  对应item的large或者small 选择parent 否则无效-->
  <style name="myPopupMenuTextAppearanceLarge"
      parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
    <item name="android:textColor">#FF0000</item>
  </style>

相关链接:
https://gist.github.com/mistrydarshan99/c52ba8345901bcc58442

http://stackoverflow.com/questions/31044370/reduce-menu-items-width-height-and-textview-size

作者:seven_Android
链接:https://www.jianshu.com/p/4c8f502aa378
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

关于android:如何调试签名发布的APK?
2019-10-01
androidgoogle-play
How to debug apk signed for release?
我有一个已经签名并上传到Android Market的apk,并已安装在手机上。 我想在手机上运行此发行版apk(通过Eclipse)进行调试。 我之前已经做过此事(并记得使用Android开发工具之一;也许是Dalvik Debug Monitor),但不幸的是,我不记得该怎么做,也无法在线找到任何文章。 有谁知道该怎么做?

Note: I have set android:debuggable=“true” in the manifest and have enabled USB Debugging on my phone.

相关讨论
我知道这是个老问题,但将来会参考。在带有Gradle的Android Studio中:

buildTypes {
release {
debuggable true
runProguard true
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.txt’
}
}
debuggable true行对我来说是窍门。

更新:

从gradle 1.0开始,它是minifyEnabled而不是runProguard。看这里

请注意,从gradle 1.0版本开始,它的minifyEnabled代替了runProguard-请参阅tools.android.com/tech-docs/new-build-system/migrating-to-1-0-0

Migrating Gradle Projects to version 1.0.0
Migrating Gradle Projects to version 1.2.x
No changes are necessary.
Migrating Gradle Projects to version 1.1
If your build.gradle files use
android.plugin.bootClasspath
or
android.plugin.ndkFolder
You need to replace this with
android.bootClasspath
and
android.ndkDirectory
All other access to android.plugin is no longer allowed (it’s internal code.)

Migrating Gradle Projects to version 1.0.0
The Android Gradle plugin has been in rapid development, and as features evolved the APIs and the build file description language went through several incompatible changes. If you are trying to load a project that was built with an older version of the Gradle plugin, it may not build correctly with 1.0.0.

This document describes the most common changes to help you migrate to 1.0.0. From version 1.0.0 and going forward, we will strive much harder to not make incompatible changes, and if we do, we plan to write IDE support to help migrate the projects automatically.
Update Plugin and Gradle Version Numbers
The build system knows which specific version of the Gradle plugin to use, and the version of Gradle to use, because they are listed explicitly in your project files. When you use Android Studio 1.0 and you open an older project, it will offer to automatically find and update these version numbers. You can also make these edits manually.

The Android Gradle plugin version is typically listed in the top level build.gradle file in the project, and can be updated as follows:

 dependencies {
  •    classpath 'com.android.tools.build:gradle:0.8.+'
    
  •    classpath 'com.android.tools.build:gradle:1.0.0'
    
    }

The version of Gradle to use for your project should also be updated to 2.2.1 or later. You can do that by editing the file gradle/wrapper/gradle-wrapper.properties:
zipStorePath=wrapper/dists
-distributionUrl=http://services.gradle.org/distributions/gradle-1.11-all.zip
+distributionUrl=http://services.gradle.org/distributions/gradle-2.2.1-all.zip

Again, Android Studio should offer to perform this edit automatically when you open up an older project:
Migrating from 0.9.x to 1.0.0
runProguard
The most common issue that affects users is the runProguard property changing names to minifyEnabled. If you run into the build error
Gradle DSL method not found: ‘runProguard()’
then this is the cause of your build error.

This crops up a lot because that property was inserted into all projects created by Android Studio prior to version 0.14.0.

To update your project, edit your build.gradle file as follows:

         }
         release {
-            runProguard true
+            minifyEnabled true
             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            

There are some additional other properties that were renamed as well, in both build types and product flavors.

ApplicationId in Library Projects
You cannot use applicationId to customize the package of a library project. The package name has to be fixed in library projects (and specified as packageName in the manifest). The Gradle plugin did not enforce this restriction earlier.

Renamed Properties in BuildTypes
runProguard => minifyEnabled
zipAlign => zipAlignEnabled
jniDebugBuild => jniDebuggable
renderscriptDebug => renderscriptDebuggable
Renamed Properties in ProductFlavors
flavorGroups => flavorDimensions
packageName => applicationId
testPackageName => testApplicationId
renderscriptSupportMode => renderscriptSupportModeEnabled
ProductFlavor.renderscriptNdkMode => renderscriptNdkModeEnabled
Other Name changes
InstrumentTest was renamed to androidTest.
Migrating From 0.8.x to 0.9.x

Version 0.9 of the Gradle plugin for Android made a few incompatible changes that require changes to your project. This page documents how to update your project. Note that this is not a complete list of changes in the 0.9 version of the plugin; it simply covers the changes that require updates to your source files. For a full list of changes, see the user guide.

Instrumentation Tests
If you have instrumentation tests (or other types of tests) in your project, note that we changed the name and folder from instrumentation tests to android tests to reflect the fact that this facility is not just for instrumentation tests but for plain JUnit tests (running on a device) and eventually also UI automator tests.

To update your project
Rename your instrumentTest folders to androidTest, e.g. git mv app/src/instrumentTest app/src/androidTest.
Alternatively you can tell gradle to keep using the old folder by relocating your sourcesets.
Update any test dependencies from instrumentTestCompile to androidTestCompile:
dependencies {

  • instrumentTestCompile ‘com.jayway.android.robotium:robotium-solo:4.3.1’
  • androidTestCompile ‘com.jayway.android.robotium:robotium-solo:4.3.1’
    }
    Libraries
    The DSL for the library projects is now the same as for the application projects. This means you can create more build types, and create flavors.
    You can create/configure more build types, in the buildTypes { … } container.
    You can create product flavors using the productFlavors { … } container.
    You can create signingConfigs using the signingConfigs { … } container.
    For example if you have in your library:
android {
    debug {
    }
    release {
    }
    debugSigningConfig {
    }
}

You would replace it with:

android {
    buildTypes {
        debug {
        }
        release {
        }
    }
    signingConfigs {
        debug {
        }
    }
}

感谢@ m02ph3u5,答案已更新!
作为当前答案的注释,我本人将避免将可调试的版本发送到Google Play商店,并且它可能不会像hasnain_ahmads情况那样使您过时。只是临时地测试商店发布发布下载的问题(例如inapp购买代码跟踪)的风险就较小。您也可以尝试使用Beta轨道(我不知道这是否会产生与hasnian相同的错误),或者将发行版本的apk从您的计算机转移到您的手机中,以避免安全问题(处理捆绑软件比较麻烦)。

相关讨论
确保在清单文件的application标记中设置了android:debuggable=“true”,然后:

将手机插入计算机并在手机上启用USB调试
打开eclipse和一个包含应用程序代码的工作区
在Eclipse中,转到窗口->显示视图->设备
查看现在应该可见的"设备"视图,您应该看到列出的设备
如果您的设备未列出,则必须先查找手机的ADB驱动程序,然后再继续
如果要单步执行代码,请在应用程序中的某个位置设置断点
在手机上打开应用
在"设备"视图中,展开电话的条目(如果尚未展开),然后查找应用程序的程序包名称。
单击程序包名称,然后在"设备"视图的右上角,您将看到一个绿色的bug以及许多其他小按钮。单击绿色的错误。
现在,您应该已附加/调试应用程序。
相关讨论

除了Manuel的方式,您仍然可以使用Manifest。

在Android Studio稳定版中,您必须将以下两行添加到AndroidManifest文件中的application:

1
2
android:debuggable=“true”
tools:ignore=“HardcodedDebugMode”
第一个将启用已签名APK的调试,第二个将防止编译时错误。

之后,您可以通过"将调试器附加到Android进程"按钮来附加到进程。

相关讨论
我尝试了以下方法,并且有效:

release {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
相关讨论
将以下内容添加到您的应用程序build.gradle中,然后选择指定的发布版本变体并运行

signingConfigs {
config {
keyAlias ‘keyalias’
keyPassword ‘keypwd’
storeFile file(’<>.keystore’)
storePassword ‘pwd’
}
}
buildTypes {
release {
debuggable true
signingConfig signingConfigs.config
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.txt’
}
}
如果您决定调试已上市但尚未分配为可调试的apk,并且您不想再次发布它。因此,请按照以下步骤操作;

用ApkTool反编译Apk(例如apktool d )
从反编译的文件中打开AndroidManifest.xml
在application标记中设置android:debuggable=“true”
用ApkTool编译修改后的源代码(例如apktool b )
可调试的APK就绪(未签名意味着无法发布存储)。您可以根据需要进行调试。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值